我使用tornado和jwt装饰器,如下所示:
def jwtauth(handler_class):
"""
Tornado JWT Auth Decorator
"""
def wrap_execute(handler_execute):
def require_auth(handler, kwargs):
auth = handler.request.headers.get(AUTHORIZATION_HEADER)
if auth:
parts = auth.split()
if not is_valid_header(parts):
return_header_error(handler)
token = parts[1]
try:
result = jwt.decode(
token,
SECRET_KEY,
options=jwt_options
)
except Exception as err:
return_auth_error(handler, str(err))
else:
handler._transforms = []
handler.write(MISSING_AUTHORIZATION_KEY)
handler.finish()
return result
def _execute(self, transforms, *args, **kwargs):
try:
result = require_auth(self, kwargs)
except Exception:
return False
return handler_execute(self, transforms, *args, **kwargs)
return _execute
handler_class._execute = wrap_execute(handler_class._execute)
return handler_class
@jwtauth
class MyHandler(tornado.web.RequestHandler):
def post(self):
unit = json.loads(self.request.body.decode('utf-8'))
# TODO:
# get the result from jwtauth decorator and use it here
print(result) # The result from jwtauth
现在,我想得到jwt解码结果并将其传递给MyHandler进行进一步验证。我能做吗?我检查了大多数可以将参数传递给装饰器的注释,但无法从中获得。是否可以将jwtauth结果传递给我的函数?你知道吗
一个类装饰器带着你的类,吐出你的类的一个新版本(通常添加了一些特性)。在本例中,} internally calls ^{} 。当前的行为是,如果JWT令牌auth失败,那么
@jwtauth
修饰符获取您的类并抛出一个新类,确保每个请求都在授权头中检查有效的JWT令牌。^{post
将永远不会被调用。你知道吗简而言之,您可能只想在下面引发一个错误,而不是返回False。你知道吗
如果需要添加更多关于引发何种错误的逻辑,那么可能需要将其与类一起传递到decorator中。你知道吗
相关问题 更多 >
编程相关推荐