目前使用 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