0eb1f2d6e4cca7c002581fdacb0b237f6c6fb139
[myslice.git] / auth / backend.py
1 # import the User object
2 from django.contrib.auth.models import User
3
4 # import the IMAP library
5 #from imaplib import IMAP4
6
7 # import time - this is used to create Django's internal username
8 import time
9
10 # Name my backend 'MyCustomBackend'
11 class MyCustomBackend:
12
13     hard_wired_users = { 'jean': '1234',
14                          'root': '2345',
15                          'jacques': '3456',
16                          }
17
18
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
27         try:
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')
36
37         return user
38
39     # Required for your backend to work properly - unchanged in most scenarios
40     def get_user(self, user_id):
41         try:
42             return User.objects.get(pk=user_id)
43         except User.DoesNotExist:
44             return None