Esempio n. 1
0
    def create_tmpl_context(self, **kwargs):
        other_stacks = None
        other_login_stacks = self.lm.other_login_stacks()
        if other_login_stacks:
            other_stacks = list()
            for ls in other_login_stacks:
                url = '%s/login/%s?%s' % (
                    self.basepath, ls.path, self.trans.get_GET_arg()
                )
                name = ls.name
                other_stacks.append({'url': url, 'name': name})

        cookie = SecureCookie(USERNAME_COOKIE)
        cookie.receive()
        username = cookie.value

        target = None
        if self.trans is not None:
            tid = self.trans.transaction_id
            target = self.trans.retrieve().get('login_target')
            username = self.trans.retrieve().get('login_username')
        if tid is None:
            tid = ''

        if username is None:
            username = ''

        context = {
            "title": 'Login',
            "action": '%s/%s' % (self.basepath, self.formpage),
            "service_name": self.lm.service_name,
            "username_text": self.lm.username_text,
            "password_text": self.lm.password_text,
            "description": self.lm.help_text,
            "other_stacks": other_stacks,
            "username": username,
            "login_target": target,
            "cancel_url": '%s/login/cancel?%s' % (self.basepath,
                                                  self.trans.get_GET_arg()),
        }
        context.update(kwargs)
        if self.trans is not None:
            t = self.trans.get_POST_tuple()
            context.update({t[0]: t[1]})

        return context
Esempio n. 2
0
 def _get_cookie(self, data=None):
     if data is None:
         data = self.retrieve()
     if data is None or 'cookie' not in data:
         raise ValueError('Cookie name not available')
     self.cookie = SecureCookie(data['cookie'])
     self.cookie.receive()
     if self.cookie.value is None:
         raise ValueError('Missing or invalid cookie')
Esempio n. 3
0
    def auth_successful(self, trans, username, auth_type=None, userdata=None):
        self.initialize_login_session(username, self.info, auth_type, userdata)

        # save username into a cookie if parent was form base auth
        if auth_type == 'password':
            cookie = SecureCookie(USERNAME_COOKIE, username)
            # 15 days
            cookie.maxage = 1296000
            cookie.send()

        transdata = trans.retrieve()
        self.debug(transdata)
        redirect = transdata.get('login_return',
                                 cherrypy.config.get('base.mount', "") + '/')
        self.debug('Redirecting back to: %s' % redirect)

        # on direct login the UI (ie not redirected by a provider) we ned to
        # remove the transaction cookie as it won't be needed anymore
        if trans.provider == 'login':
            self.debug('Wiping transaction data')
            trans.wipe()
        raise cherrypy.HTTPRedirect(redirect)
Esempio n. 4
0
 def _set_cookie(self):
     self.cookie = SecureCookie(name=None, value=self.provider)
     self.cookie.send()
     cookiedata = {'cookie': self.cookie.name}
     data = {self.transaction_id: cookiedata}
     self._ts.save_unique_data(TRANSTABLE, data)
Esempio n. 5
0
class Transaction(Log):

    def __init__(self, provider, **kwargs):
        self.provider = provider
        self.transaction_id = None
        self._ts = TranStore()
        self.cookie = None

        if kwargs:
            self.find_tid(kwargs)

        # If tid isn't found create a new one
        if not self.transaction_id:
            self.create_tid()

    def find_tid(self, kwargs):
        tid = kwargs.get(TRANSID)
        if tid:
            # If more than one only pick the first
            if isinstance(tid, list):
                tid = tid[0]
            self.transaction_id = tid
            data = self.retrieve()
            if data and 'provider' in data:
                self.provider = data['provider']
            self._get_cookie(data)
            self.debug('Transaction: %s %s' % (self.provider,
                                               self.transaction_id))
            return tid

        return None

    def create_tid(self):
        data = {'provider': self.provider,
                'origintime': str(datetime.now())}
        self.transaction_id = self._ts.new_unique_data(TRANSTABLE, data)
        self._set_cookie()
        self.debug('Transaction: %s %s' % (self.provider,
                                           self.transaction_id))

    def _set_cookie(self):
        self.cookie = SecureCookie(name=None, value=self.provider)
        self.cookie.send()
        cookiedata = {'cookie': self.cookie.name}
        data = {self.transaction_id: cookiedata}
        self._ts.save_unique_data(TRANSTABLE, data)

    def _get_cookie(self, data=None):
        if data is None:
            data = self.retrieve()
        if data is None or 'cookie' not in data:
            raise ValueError('Cookie name not available')
        self.cookie = SecureCookie(data['cookie'])
        self.cookie.receive()
        if self.cookie.value is None:
            raise ValueError('Missing or invalid cookie')

    def _del_cookie(self):
        if self.cookie:
            self.cookie.delete()

    def wipe(self):
        if not self.transaction_id:
            return
        self._ts.del_unique_data(TRANSTABLE, self.transaction_id)
        self._del_cookie()
        self.transaction_id = None

    def store(self, data):
        savedata = {self.transaction_id: data}
        self._ts.save_unique_data(TRANSTABLE, savedata)

    def retrieve(self):
        data = self._ts.get_unique_data(TRANSTABLE,
                                        uuidval=self.transaction_id)
        return data.get(self.transaction_id) or dict()

    def get_GET_arg(self):
        return "%s=%s" % (TRANSID, self.transaction_id)

    def get_POST_tuple(self):
        return (TRANSID, self.transaction_id)