I am trying to use Django with a non-ORM data source, and while accessing other resources through my custom backend without any authentication is successful, now I need to introduce user authentication. There is no local database. When I receive a request (e.g., a cURL command made with username and password), I need to perform HTTP Basic Authentication against a remote URL and upon success, I should return a locally created user object, which only has a username, nothing fancy. So in my Tastypie resource, I wrote something like this:
class dict2obj(object):
"""
Convert dictionary to object
@source http://stackoverflow.com/a/1305561/383912
"""
def __init__(self, d):
self.__dict__['d'] = d
def __getattr__(self, key):
value = self.__dict__['d'][key]
if type(value) == type({}):
return dict2obj(value)
return value
class RemoteAuth(Authentication):
def is_authenticated(self, request, **kwargs):
username = request.user.username
password = request.user.password
r = requests.get(AUTHENTICATION_URL, auth=(username, password))
if r.status_code == 200:
return True
return False
class UserResource(Resource):
username = fields.CharField(attribute='username')
class Meta:
resource_name = 'user'
authentication = RemoteAuth()
authorization = Authorization()
def obj_get_list(self, request=None, **kwargs):
result = []
posts.append(dict2obj(
{
'username': request.POST.get('username'),
}
))
return result
But of course this does not work, because the authentication object cannot obtain password like that. Please suggest a good way of handling remove user authentication without involving any local database.