如何从url请求验证令牌?

2024-04-26 03:41:41 发布

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

我使用以下算法检查请求是否正确:

from aiohttp import BasicAuth

class A:
    def __init__(
        self,
        user_name: str,
        password: str,
        ...
    ):
        self._auth_header = BasicAuth(login=user_name, password=password).encode()

    async def handle_request(self, request):
        if not self.is_authorized_request(request):
            return Response(status=HTTPUnauthorized.status_code)

        ...
        return Response(status=HTTPOk.status_code)

    def is_authorized_request(self, request) -> bool:
        auth = request.headers.get("Authorization")
        return auth == self._auth_header

但是现在我需要将身份验证改为基于令牌的身份验证,而不是登录凭据。因为客户无法使用登录凭据。你知道吗

所以我需要将令牌身份验证配置为URL的一部分。 最好的办法是什么?你知道吗

我正在考虑从url中提取api_token,然后检查:

if my_token != api_token: 
   return Response(status=HTTPUnauthorized.status_code)

但我不确定这个解决方案。你知道吗


Tags: nameselftokenauth身份验证returnresponserequest