Exemple #1
0
    def get_access_token(self, app, config, extra_data):
        handler = self.context.get('handler')
        strategy = load_strategy(handler, config)

        backend_path = config.get('backend_path')
        Backend = module_member(backend_path)

        if not issubclass(Backend, BaseAuth):
            return extra_data.get('access_token')

        backend = Backend(strategy, None)

        refresh_token = extra_data.get('refresh_token') or extra_data.get(
            'access_token')

        expires_on = None
        params_expires_on = extra_data.get('expires_on')
        params_token_updated = extra_data.get('token_updated')
        params_expires_in = extra_data.get('expires') or extra_data.get(
            'expires_in')

        try:
            if params_expires_on:
                expires_on = int(params_expires_on)
            elif params_expires_in and params_token_updated:
                expires_on = int(params_token_updated) + int(params_expires_in)
        except (ValueError, TypeError):
            pass

        try:
            if refresh_token and (not expires_on
                                  or expires_on <= int(time.time())):
                response = backend.refresh_token(token=refresh_token)
                if not backend.EXTRA_DATA or len(backend.EXTRA_DATA) == 0:
                    backend.GET_ALL_EXTRA_DATA = True
                new_extra_data = backend.extra_data(user=None,
                                                    uid=None,
                                                    response=response,
                                                    details={})
                access_token = new_extra_data.get('access_token')

                new_extra_data = {
                    'expires_on': new_extra_data.get('expires_on'),
                    'access_token': new_extra_data.get('access_token'),
                    'expires': new_extra_data.get('expires'),
                    'auth_time': new_extra_data.get('auth_time'),
                    'refresh_token': new_extra_data.get('refresh_token'),
                    'token_updated': int(time.time())
                }

                request = self.context.get('request')
                extra_data_key = '_'.join(['extra_data', app])
                configuration.session_set(request, extra_data_key,
                                          json.dumps(new_extra_data))

                return access_token
        except Exception:
            pass

        return extra_data.get('access_token')
Exemple #2
0
    def init_auth(self, request, app):
        redirect_uri = self.redirect_uri(app)

        try:
            name = configuration.clean_sso_application_name(app)
            config = settings.SSO_APPLICATIONS[name]
        except KeyError:
            raise NotFound

        backend_path = config.get('backend_path')
        Backend = self.backends.get(backend_path)

        self.strategy = load_strategy(self, request, config)
        self.backend = Backend(self.strategy, redirect_uri)