python - 创建一个装饰器来处理烧瓶中的令牌验证
问题描述
我在烧瓶服务 python 文件中定义了两个端点。
- 第一个端点从它解析的 mmap json 文件返回父节点和子节点的列表。
- 第二个端点从它解析的 mmap json 文件中返回一个特定的子字段。
这些端点中的每一个都只能在验证令牌时使用。因此我有以下实现。
from flask import request
import requests
def check_token(self):
# Method to verify the token from the another python Service
token_header = request.headers['authorization']
# Remove the 'Basic" part from the token
auth_token = token_header.split(maxsplit=1)[1]
self.__payload = {'token' : auth_token}
# Append the token to the header by using the payload
response = requests.get(self.__url, params=self.__payload, verify=False)
return response
# 1. This endpoint returns a list of parent and child nodes from a mmap json file which it parses.
class SystemList(Resource):
def get(self, systemname):
token = check_token()
if token.ok:
# Open the mmap file, parse it, and jsonify the result and return it
# Invalid token present
else:
return make_response(
jsonify("Invalid access as token invalid.")
)
# 2. The endpoint returns a specific child field from a mmap json file which it parses.
class SystemChildList(Resource):
def get(self, systemname, id):
token = check_token()
if token.ok:
# Open the mmap file, parse it, and jsonify the result and return it
# Invalid token present
else:
return make_response(
jsonify("Invalid access as token invalid.")
)
我遇到的问题是我想使用装饰器来处理令牌的验证。
我希望能够在get()
方法之前添加它,如下所示。
@validatetoken
def get(self, designation):
# I am not sure what goes here and what do I need to put here?
# I want to be able to have one decorator for both of the SystemList and SystemChildList
# resource shown above.
我不确定装饰器中的内容。我对这些概念真的很陌生。任何帮助表示赞赏。
解决方案
您可以使用method_decorators参数来实现这一点
尝试,
from flask import request
from functools import wraps
import requests
def check_token(f):
@wraps(f)
def decorated(*args, **kwargs):
token_header = request.headers['authorization']
# Remove the 'Basic" part from the token
auth_token = token_header.split(maxsplit=1)[1]
__url = "url_for_token_validation"
__payload = {'token' : auth_token}
# Append the token to the header by using the payload
response = requests.get(__url, params=__payload, verify=False)
if response.status_code != requests.codes.ok:
return make_response(
jsonify("Invalid access as token invalid.")
)
return f(*args, **kwargs)
return decorated
# 1. This endpoint returns a list of parent and child nodes from a mmap json file which it parses.
class SystemList(Resource):
@check_token
def get(self, systemname):
# Open the mmap file, parse it, and jsonify the result and return it
# 2. The endpoint returns a specific child field from a mmap json file which it parses.
class SystemChildList(Resource):
@check_token
def get(self, systemname, id):
# Open the mmap file, parse it, and jsonify the result and return it
推荐阅读
- android-studio - Flutter:调试集成测试
- r - 使用 R 中的插入符号包绘制具有交叉验证的多类分类的学习曲线
- groovy - 如何使用 Ant Build 脚本打包 Groovy 类或 Groovy 项目?
- javascript - 在我的 fish / cygwin shell 上使用 Node.js
- c++ - 在 c++ 中给出意外输出的简单代码
- java - Java中的值匹配和降维
- android - 旋转视频 - 无法为输出文件 #0 写入标头(编解码器参数不正确?)
- node.js - Socket.io 在 https://domain 上建立连接,但在 https://domain/route 上没有建立任何连接
- javascript - 方法表达式不是函数类型(等于状态属性/值) - React JS
- docker - Confluent 平台和 java.nio.file.DirectoryNotEmptyException