1 # import the User object
2 from django.contrib.auth.models import User
6 # Name my backend 'MyCustomBackend'
9 hard_wired_users = { 'jean': '1234',
15 # Create an authentication method
16 # This is called by the standard Django login procedure
17 def authenticate(self, token=None):
18 username=token['username']
19 password=token['password']
20 users=MyCustomBackend.hard_wired_users
21 if username not in users: return None
22 if password != users[username]: return None
24 # Check if the user exists in Django's local database
25 user = User.objects.get(email=username)
26 except User.DoesNotExist:
27 print 'creating django user',username
28 # Create a user in Django's local database
29 # warning: the trick here is pass current time as an id, and name as email
30 # create_user(username, email=None, password=None)
31 user = User.objects.create_user(time.time(), username, 'password-doesnt-matter')
35 # Required for your backend to work properly - unchanged in most scenarios
36 def get_user(self, user_id):
38 return User.objects.get(pk=user_id)
39 except User.DoesNotExist: