金字塔自定义oauth2 d

2024-03-29 14:40:03 发布

您现在位置:Python中文网/ 问答频道 /正文

目前正在使用Kevin Van Wilder oauth2服务器项目http://code.google.com/p/pyramid-oauth2/,它正在开发中,但大部分功能在我的项目中运行良好,但我与他的一个装饰师有这个问题,我是新来的装饰师,正如我所说的,是在开发中,所以我不确定它是否已经完成或实际上是一个装饰本身,我评论了一些相关的行大写我有问题,我可以修复或使用这个吗?,还是需要使用functools wrap函数。。。如有指导,将不胜感激

def oauth2(allowed_scopes=[], optional=False):
    def wrap(view_fn):
        def new_fn(incoming_request):
            # get token
            request = OAuth2Request(incoming_request)
            token = request.access_token
            # handle token
            if token:
                #oauth2_context = get_token_context.delay(token.get('token')).get() I AM NOT USING CELERY
                oauth2_context = get_token_context(token.get('token'))
                # stop if token contains no valid information
                if not oauth2_context.valid:
                    raise OAuth2ErrorHandler.error_invalid_token(token.get('type'))
                # not mandatory use of oauth, but valid token
                elif optional:
                    return view_fn(request=incoming_request, oauth2_context=oauth2_context)
                # validate scope
                elif has_valid_scope(oauth2_context.scopes, allowed_scopes):
                    return view_fn(request=incoming_request, oauth2_context=oauth2_context)
                # return oauth error for invalid token
                else:
                    return OAuth2ErrorHandler.error_invalid_token(token.get('type'))
            # no token
            else:
                if optional:
                    return view_fn(request=incoming_request, oauth2_context=None)
                else:
                    raise HTTPUnauthorized('request contained no access token.')

        new_fn.__doc__ = view_fn.__doc__
        #new_fn.__name__ = view_fn.__name__ WITH THIS IT DOESNT WORK, COMMENTED IT
        new_fn.view_name = view_fn.view_name MAYBE ITS LIKE THIS?
        #return new_fn CANT RETURN HERE, IT GOES DIRECTLY TO THE VIEW, COMMENTED IT
    return wrap

Tags: nametokenviewnewgetreturnifrequest