From 6fa0b07927a3e5f2f94e0838942e0a503ed0b3ae Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Fri, 27 Oct 2006 15:37:05 +0000 Subject: [PATCH] session handling functions --- PLC/Methods/DeleteSession.py | 29 +++++++++++++++++++++++++++ PLC/Methods/GetSession.py | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 PLC/Methods/DeleteSession.py create mode 100644 PLC/Methods/GetSession.py diff --git a/PLC/Methods/DeleteSession.py b/PLC/Methods/DeleteSession.py new file mode 100644 index 00000000..545adfd6 --- /dev/null +++ b/PLC/Methods/DeleteSession.py @@ -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 index 00000000..f1263c80 --- /dev/null +++ b/PLC/Methods/GetSession.py @@ -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'] -- 2.47.0