创建一个decorator来处理flas中的令牌验证

2024-04-23 09:47:24 发布

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

我在flask服务python文件中定义了两个端点。你知道吗

  1. 第一个端点从它解析的mmap json文件返回父节点和子节点的列表。你知道吗
  2. 第二个端点从它解析的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.

我不知道装潢机里装的是什么。我对这些概念真的很陌生。感谢您的帮助。你知道吗


Tags: andthetofromselftokenjsonget
1条回答
网友
1楼 · 发布于 2024-04-23 09:47:24

您可以使用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

相关问题 更多 >