Я разрабатываю приложение Flutter, которое использует Auth0 для аутентификации. Я создал собственное приложение в Auth0. После успешной аутентификации приложение Flutter получает токен от Auth0. Теперь мне нужно проверить этот токен на моем сервере FastAPI.
Как я могу проверить, действителен ли этот токен и правильно ли он выдан Auth0? Я не уверен, как реализовать эту часть процесса. Может ли кто-нибудь помочь мне выполнить действия или поделиться примерами?
Буду очень признателен за любую помощь!
Вот мой код
from typing import Optional
import jwt
from fastapi import Depends, HTTPException, status
from fastapi.security import SecurityScopes, HTTPAuthorizationCredentials, HTTPBearer
class UnauthorizedException(HTTPException):
def __init__(self, detail: str, **kwargs):
super().__init__(status.HTTP_403_FORBIDDEN, detail=detail)
class UnauthenticatedException(HTTPException):
def __init__(self):
super().__init__(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Requires authentication"
)
class VerifyToken:
"""Does all the token verification using PyJWT"""
def __init__(self):
self.auth0_domain = 'dev-5c7****.com'
self.auth0_algorithms = 'RS256'
self.auth0_api_audience = '' #The problem is that auth0's native app doesn't have this
self.auth0_issuer = f'https://{self.auth0_domain}/'
jwks_url = f'https://{self.auth0_domain}/.well-known/jwks.json'
self.jwks_client = jwt.PyJWKClient(jwks_url)
async def verify(self,
security_scopes: SecurityScopes,
token: Optional[HTTPAuthorizationCredentials] = Depends(HTTPBearer())
):
if token is None:
raise UnauthenticatedException
try:
signing_key = self.jwks_client.get_signing_key_from_jwt(
token.credentials
).key
except jwt.exceptions.PyJWKClientError as error:
raise UnauthorizedException(str(error))
except jwt.exceptions.DecodeError as error:
raise UnauthorizedException(str(error))
except Exception as error:
raise UnauthorizedException(str(error))
try:
payload = jwt.decode(
token.credentials,
signing_key,
algorithms=self.auth0_algorithms,
audience=self.auth0_api_audience,
issuer=self.auth0_issuer,
)
except Exception as error:
raise UnauthorizedException(str(error))
return payload
Подробнее здесь: https://stackoverflow.com/questions/792 ... lutter-app
Как проверить токен Auth0 в бэкэнде FastAPI для приложения Flutter? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Handleauth-это не функция с @auth0 / nextjs-auth0 v4.x в next.js app Router / Page.
Anonymous » » в форуме Javascript - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-