session handling functions
authorMark Huang <mlhuang@cs.princeton.edu>
Fri, 27 Oct 2006 15:37:05 +0000 (15:37 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Fri, 27 Oct 2006 15:37:05 +0000 (15:37 +0000)
PLC/Methods/DeleteSession.py [new file with mode: 0644]
PLC/Methods/GetSession.py [new file with mode: 0644]

diff --git a/PLC/Methods/DeleteSession.py b/PLC/Methods/DeleteSession.py
new file mode 100644 (file)
index 0000000..545adfd
--- /dev/null
@@ -0,0 +1,29 @@
+import time
+
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Auth import SessionAuth
+from PLC.Sessions import Session, Sessions
+
+class DeleteSession(Method):
+    """
+    Invalidates the current session.
+
+    Returns 1 if successful.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech', 'node']
+    accepts = [SessionAuth()]
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth):
+        assert auth.has_key('session')
+
+        sessions = Sessions(self.api, [auth['session']]).values()
+        if not sessions:
+            raise PLCAPIError, "No such session"
+        session = sessions[0]
+
+        session.delete()
+
+        return 1
diff --git a/PLC/Methods/GetSession.py b/PLC/Methods/GetSession.py
new file mode 100644 (file)
index 0000000..f1263c8
--- /dev/null
@@ -0,0 +1,38 @@
+import time
+
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Auth import Auth
+from PLC.Sessions import Session, Sessions
+from PLC.Nodes import Node, Nodes
+from PLC.Persons import Person, Persons
+
+class GetSession(Method):
+    """
+    Returns a new session key if a user or node authenticated
+    successfully, faults otherwise.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech', 'node']
+    accepts = [Auth()]
+    returns = Session.fields['session_id']
+
+    def call(self, auth):
+        # Authenticated with a session key, just return it
+        if auth.has_key('session'):
+            return auth['session']
+
+        session = Session(self.api)
+
+        if isinstance(self.caller, Person):
+            # XXX Make this configurable
+            session['expires'] = int(time.time()) + (24 * 60 * 60)
+
+        session.sync(commit = False)
+
+        if isinstance(self.caller, Node):
+            session.add_node(self.caller, commit = True)
+        elif isinstance(self.caller, Person):
+            session.add_person(self.caller, commit = True)
+
+        return session['session_id']