Authentication with manifold api backend
authorMohamed Larabi <aminelarabi@vis067b.sophia.inria.fr>
Tue, 12 Feb 2013 12:02:46 +0000 (13:02 +0100)
committerMohamed Larabi <aminelarabi@vis067b.sophia.inria.fr>
Tue, 12 Feb 2013 12:02:46 +0000 (13:02 +0100)
auth/manifold_backend.py [new file with mode: 0644]
engine/manifold_api.py [new file with mode: 0644]

diff --git a/auth/manifold_backend.py b/auth/manifold_backend.py
new file mode 100644 (file)
index 0000000..67ab89f
--- /dev/null
@@ -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 (file)
index 0000000..63487b9
--- /dev/null
@@ -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