1 # import the User object
2 from django.contrib.auth.models import User
4 # import the IMAP library
5 #from imaplib import IMAP4
7 # import time - this is used to create Django's internal username
10 # Name my backend 'MyCustomBackend'
11 class MyCustomBackend:
13 hard_wired_users = { 'jean': '1234',
19 # Create an authentication method
20 # This is called by the standard Django login procedure
21 def authenticate(self, token=None):
22 username=token['username']
23 password=token['password']
24 users=MyCustomBackend.hard_wired_users
25 if username not in users: return None
26 if password != users[username]: return None
28 # Check if the user exists in Django's local database
29 user = User.objects.get(email=username)
30 except User.DoesNotExist:
31 print 'creating django user',username
32 # Create a user in Django's local database
33 # warning: the trick here is pass current time as an id, and name as email
34 # create_user(username, email=None, password=None)
35 user = User.objects.create_user(time.time(), username, 'password-doesnt-matter')
39 # Required for your backend to work properly - unchanged in most scenarios
40 def get_user(self, user_id):
42 return User.objects.get(pk=user_id)
43 except User.DoesNotExist: