450974e0e4f04c37c39df7991b4a086f4f05a6cf
[unfold.git] / auth / manifoldbackend.py
1 # import the User object
2 from django.contrib.auth.models import User
3 from engine.manifoldapi 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, token=None):
15         if not token:
16             return None
17
18         try:
19             username = token['username']
20             password = token['password']
21             request = token['request']
22
23             auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
24             api = ManifoldAPI(auth)
25             # Authenticate user and get session key
26             session = api.GetSession()
27             if not session : 
28                 return None
29             
30             request.session['manifold_session'] = session
31             #self.session = session
32             # Change GetSession() at some point to return expires as well
33             expires = time.time() + (24 * 60 * 60)
34
35             # Change to session authentication
36             api.auth = {'AuthMethod': 'session', 'session': session}
37             self.api = api
38
39             # Get account details
40             person = api.GetPersons(auth)[0]
41             self.person = person
42
43             request.session['manifold'] = {'auth': api.auth, 'person': person, 'expires': expires}
44         except:
45             return None
46
47         try:
48             # Check if the user exists in Django's local database
49             user = User.objects.get(username=username)
50         except User.DoesNotExist:
51             # Create a user in Django's local database
52             user = User.objects.create_user(username, username, 'passworddoesntmatter')
53             user.first_name = person['first_name']
54             user.last_name = person['last_name']
55             user.email = person['email']
56         return user
57
58     # Required for your backend to work properly - unchanged in most scenarios
59     def get_user(self, user_id):
60         try:
61             return User.objects.get(pk=user_id)
62         except User.DoesNotExist:
63             return None
64
65