From: Mohamed Larabi Date: Tue, 12 Feb 2013 12:02:46 +0000 (+0100) Subject: Authentication with manifold api backend X-Git-Tag: myslice-django-0.1-1~93 X-Git-Url: http://git.onelab.eu/?p=myslice.git;a=commitdiff_plain;h=7b569e0b595b4be76de46385536a22e234dae9e4 Authentication with manifold api backend --- diff --git a/auth/manifold_backend.py b/auth/manifold_backend.py new file mode 100644 index 00000000..67ab89f4 --- /dev/null +++ b/auth/manifold_backend.py @@ -0,0 +1,57 @@ +# import the User object +from django.contrib.auth.models import User +from engine.manifold_api import ManifoldAPI + + +# import time - this is used to create Django's internal username +import time + +# Name my backend 'ManifoldBackend' +class ManifoldBackend: + + # Create an authentication method + # This is called by the standard Django login procedure + def authenticate(self, username=None, password=None): + if not username or not password: + return None + print username + + try: + auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password} + api = ManifoldAPI(auth) + # Authenticate user and get session key + session = api.GetSession() + if not session : + return None + + #self.session = session + # Change GetSession() at some point to return expires as well + expires = time.time() + (24 * 60 * 60) + + # Change to session authentication + api.auth = {'AuthMethod': 'session', 'session': session} + #self.api = api + + # Get account details + person = api.GetPersons(auth) + #self.person = person[0] + except: + return None + + try: + # Check if the user exists in Django's local database + user = User.objects.get(email=username) + except User.DoesNotExist: + # Create a user in Django's local database + user = User.objects.create_user(time.time(), username, 'passworddoesntmatter') + + return user + + # Required for your backend to work properly - unchanged in most scenarios + def get_user(self, user_id): + try: + return User.objects.get(pk=user_id) + except User.DoesNotExist: + return None + + diff --git a/engine/manifold_api.py b/engine/manifold_api.py new file mode 100644 index 00000000..63487b99 --- /dev/null +++ b/engine/manifold_api.py @@ -0,0 +1,30 @@ +# Manifold API Python interface +import xmlrpclib +#from util.config import Config + +class ManifoldAPI: + + def __init__(self, auth=None, cainfo=None): + + #config = Config() + self.auth = auth + #self.server = config.server + #self.port = config.port + #self.path = config.path + self.server = 'demo.myslice.info' + self.port = '7080' + self.path = '/' + self.cainfo = cainfo + self.errors = [] + self.trace = [] + self.calls = {} + self.multicall = False + self.url = "http://"+self.server+":"+self.port+"/" + self.proxy = xmlrpclib.Server(self.url, verbose=False, allow_none=True) + + def __getattr__(self, methodName): + def func(*args, **kwds): + print methodName, self.auth, self.url + result=getattr(self.proxy, methodName)(self.auth, *args, **kwds) + return result + return func \ No newline at end of file