예제 #1
0
def make_sso_client():
    return SSOAuthentication(
        auth_url=settings.SSO['AUTH_URL'],
        api_key=settings.SSO['API_KEY'],
        callback_url=settings.SSO['CALLBACK_URL'],
        request_token_url=settings.SSO['REQUEST_TOKEN_URL'],
        access_url=settings.SSO['ACCESS_URL'],
        auth_token_url=settings.SSO['AUTH_TOKEN_URL'],
        redirect_handler=redirect_handler,
        error_handler=error_handler)
예제 #2
0
    def init_app(self, app):
        self.config = {
            'auth_url': app.config['SSO']['AUTH_URL'],
            'api_key': app.config['SSO']['API_KEY'],
            'callback_url': app.config['SSO']['CALLBACK_URL'],
            'request_token_url': app.config['SSO']['REQUEST_TOKEN_URL'],
            'access_url': app.config['SSO']['ACCESS_URL'],
            'auth_token_url': app.config['SSO']['AUTH_TOKEN_URL'],
            'redirect_handler': self.redirect_handler,
            'error_handler': self.error_handler
        }

        self.sso = BaseSSOAuthentication(**self.config)

        app.sso_manager = self
        app.context_processor(_user_context_processor)
예제 #3
0
    def init_app(self, app):
        self.config = {
            'auth_url': app.config['SSO']['AUTH_URL'],
            'api_key': app.config['SSO']['API_KEY'],
            'callback_url': app.config['SSO']['CALLBACK_URL'],
            'request_token_url': app.config['SSO']['REQUEST_TOKEN_URL'],
            'access_url': app.config['SSO']['ACCESS_URL'],
            'auth_token_url': app.config['SSO']['AUTH_TOKEN_URL'],
            'redirect_handler': self.redirect_handler,
            'error_handler': self.error_handler
        }

        self.sso = BaseSSOAuthentication(**self.config)

        app.sso_manager = self
        app.context_processor(_user_context_processor)
예제 #4
0
class SSOAuthentication(object):
    def __init__(self, app=None):
        self.app = app
        self.sso = None
        self.config = None
        self.user_callback = None
        if self.app is not None:
            self.init_app(app)

    def init_app(self, app):
        self.config = {
            'auth_url': app.config['SSO']['AUTH_URL'],
            'api_key': app.config['SSO']['API_KEY'],
            'callback_url': app.config['SSO']['CALLBACK_URL'],
            'request_token_url': app.config['SSO']['REQUEST_TOKEN_URL'],
            'access_url': app.config['SSO']['ACCESS_URL'],
            'auth_token_url': app.config['SSO']['AUTH_TOKEN_URL'],
            'redirect_handler': self.redirect_handler,
            'error_handler': self.error_handler
        }

        self.sso = BaseSSOAuthentication(**self.config)

        app.sso_manager = self
        app.context_processor(_user_context_processor)

    def redirect_handler(self, url):
        return redirect(url)

    def error_handler(self, status_code, msg):
        return abort(status_code)

    def user_loader(self, func):
        self.user_callback = func
        return func

    @property
    def user(self):
        return _get_user()

    def sso_access(self, func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            request_token = request.args.get(
                'request_token', request.cookies.get('request_token'))
            auth_token = request.args.get('auth_token',
                                          request.cookies.get('auth_token'))
            user_id = request.cookies.get('user_id')
            redirect_to = request.path
            if request.args:
                redirect_to = "{}?{}".format(redirect_to,
                                             urllib.urlencode(request.args))
            result, status = self.sso.check_authentication(
                request_token, auth_token, user_id, redirect_to)
            if status:
                ctx = stack.top
                if self.user_callback:
                    loaded_user = self.user_callback(result)
                    ctx.sso_user = loaded_user
                else:
                    ctx.sso_user = result
                view_result = func(*args, **kwargs)
                response = current_app.make_response(view_result)
                response.set_cookie('auth_token', auth_token)
                response.set_cookie('user_id', str(result['user']))
                response.set_cookie('request_token', request_token)
                return response
            return result

        return wrapped
예제 #5
0
class SSOAuthentication(object):

    def __init__(self, app=None):
        self.app = app
        self.sso = None
        self.config = None
        self.user_callback = None
        if self.app is not None:
            self.init_app(app)

    def init_app(self, app):
        self.config = {
            'auth_url': app.config['SSO']['AUTH_URL'],
            'api_key': app.config['SSO']['API_KEY'],
            'callback_url': app.config['SSO']['CALLBACK_URL'],
            'request_token_url': app.config['SSO']['REQUEST_TOKEN_URL'],
            'access_url': app.config['SSO']['ACCESS_URL'],
            'auth_token_url': app.config['SSO']['AUTH_TOKEN_URL'],
            'redirect_handler': self.redirect_handler,
            'error_handler': self.error_handler
        }

        self.sso = BaseSSOAuthentication(**self.config)

        app.sso_manager = self
        app.context_processor(_user_context_processor)

    def redirect_handler(self, url):
        return redirect(url)

    def error_handler(self, status_code, msg):
        return abort(status_code)

    def user_loader(self, func):
        self.user_callback = func
        return func

    @property
    def user(self):
        return _get_user()

    def sso_access(self, func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            request_token = request.args.get('request_token', request.cookies.get('request_token'))
            auth_token = request.args.get('auth_token', request.cookies.get('auth_token'))
            user_id = request.cookies.get('user_id')
            redirect_to = request.path
            if request.args:
                redirect_to = "{}?{}".format(redirect_to, urllib.urlencode(request.args))
            result, status = self.sso.check_authentication(request_token, auth_token, user_id, redirect_to)
            if status:
                ctx = stack.top
                if self.user_callback:
                    loaded_user = self.user_callback(result)
                    ctx.sso_user = loaded_user
                else:
                    ctx.sso_user = result
                view_result = func(*args, **kwargs)
                response = current_app.make_response(view_result)
                response.set_cookie('auth_token', auth_token)
                response.set_cookie('user_id', str(result['user']))
                response.set_cookie('request_token', request_token)
                return response
            return result
        return wrapped