67ab89f4e713f5836e3cd5cdc8d46d67f9ae550e
[unfold.git] / auth / manifold_backend.py
1 # import the User object
2 from django.contrib.auth.models import User
3 from engine.manifold_api import ManifoldAPI
4
5
6 # import time - this is used to create Django's internal username
7 import time
8
9 # Name my backend 'ManifoldBackend'
10 class ManifoldBackend:
11
12     # Create an authentication method
13     # This is called by the standard Django login procedure
14     def authenticate(self, username=None, password=None):
15         if not username or not password:
16             return None
17         print username
18
19         try:
20             auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
21             api = ManifoldAPI(auth)
22             # Authenticate user and get session key
23             session = api.GetSession()
24             if not session : 
25                 return None
26
27             #self.session = session
28             # Change GetSession() at some point to return expires as well
29             expires = time.time() + (24 * 60 * 60)
30
31             # Change to session authentication
32             api.auth = {'AuthMethod': 'session', 'session': session}
33             #self.api = api
34
35             # Get account details
36             person = api.GetPersons(auth)
37             #self.person = person[0]
38         except:
39             return None
40
41         try:
42             # Check if the user exists in Django's local database
43             user = User.objects.get(email=username)
44         except User.DoesNotExist:
45             # Create a user in Django's local database
46             user = User.objects.create_user(time.time(), username, 'passworddoesntmatter')
47
48         return user
49
50     # Required for your backend to work properly - unchanged in most scenarios
51     def get_user(self, user_id):
52         try:
53             return User.objects.get(pk=user_id)
54         except User.DoesNotExist:
55             return None
56
57