61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""
|
|
Dispatcher for the Vincennes authentifications
|
|
"""
|
|
import base64
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
from mandaye.dispatchers.auth import AuthFormDispatcher
|
|
from mandaye.response import _502, _302
|
|
|
|
class VincennesAuth(object):
|
|
|
|
def __init__(self, env):
|
|
self.env = env
|
|
|
|
def _parse_qs(self, query):
|
|
""" Parse query string
|
|
Return a dict
|
|
"""
|
|
res = {}
|
|
values = query.split('&')
|
|
for value in values:
|
|
keyvalue = value.split('=', 1)
|
|
res[keyvalue[0]] = keyvalue[1]
|
|
return res
|
|
|
|
def get_current_login(self):
|
|
""" Return the current Vincennes pseudo
|
|
"""
|
|
from mandaye import config
|
|
# TODO: test time validity
|
|
if not self.env['QUERY_STRING']:
|
|
return None
|
|
query = self._parse_qs(self.env['QUERY_STRING'])
|
|
if query.has_key('token'):
|
|
# TODO: catch exceptions
|
|
token = query['token']
|
|
token = base64.b64decode(token)
|
|
cipher = AES.new(config.secret, AES.MODE_CFB)
|
|
decode = cipher.decrypt(token)
|
|
info = eval(decode[16:])
|
|
return info['pseudo']
|
|
return None
|
|
|
|
class VincennesDispatcher(AuthFormDispatcher):
|
|
|
|
def init(self, env):
|
|
""" overload the init method to change the backends
|
|
"""
|
|
super(VincennesDispatcher, self).init(env)
|
|
self.local_auth = VincennesAuth(env)
|
|
|
|
def connection(self, values, request):
|
|
""" Connection to the compte citoyen
|
|
"""
|
|
location = values.get('destination')
|
|
location += "?next_url=%s&service=%s" % \
|
|
(values.get('next_url'), values.get('service_name'))
|
|
return _302(location)
|
|
|