3 from django.contrib.auth.models import User
5 from manifoldapi.manifoldapi import ManifoldAPI, ManifoldException, ManifoldResult
6 from manifold.core.query import Query
8 from myslice.settings import config, logger, DEBUG
10 # Name my backend 'ManifoldBackend'
11 class ManifoldBackend:
13 # Create an authentication method
14 # This is called by the standard Django login procedure
15 def authenticate(self, token=None):
22 username = token['username']
23 password = token['password']
24 request = token['request']
26 auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
27 api = ManifoldAPI(auth)
28 sessions_result = api.forward(Query.create('local:session').to_dict())
29 sessions = sessions_result.ok_value()
31 logger.error("GetSession failed: {}".format(sessions_result.error()))
34 logger.debug("SESSION : {}".format(session))
36 # Change to session authentication
37 api.auth = {'AuthMethod': 'session', 'session': session['session']}
41 # the new API would expect Get('local:user') instead
42 persons_result = api.forward(Query.get('local:user').to_dict())
43 persons = persons_result.ok_value()
45 logger.error("GetPersons failed: {}".format(persons_result.error()))
48 logger.debug("PERSON : {}".format(person))
49 #logger.info("{} {} <{}> logged in"\
50 # .format(person['config']['first_name'], person['config']['last_name'], person['config']['email']))
52 request.session['manifold'] = {'auth': api.auth, 'person': person, 'expires': session['expires']}
53 except ManifoldException as e:
54 logger.error("ManifoldException in Auth Backend: {}".format(e.manifold_result))
55 except Exception as e:
56 logger.error("Exception in Manifold Auth Backend: {}".format(e))
62 # Check if the user exists in Django's local database
63 user = User.objects.get(username=username)
64 except User.DoesNotExist:
65 # Create a user in Django's local database
66 user = User.objects.create_user(username, username, 'passworddoesntmatter')
67 user.email = person['email']
69 if 'firstname' in person:
70 user.first_name = person['firstname']
71 if 'lastname' in person:
72 user.last_name = person['lastname']
76 # Required for your backend to work properly - unchanged in most scenarios
77 def get_user(self, user_id):
79 return User.objects.get(pk=user_id)
80 except User.DoesNotExist: