mirror of
https://github.com/ijaric/voice_assistant.git
synced 2025-12-16 01:26:17 +00:00
Версионирование API
This commit is contained in:
28
src/fastapi_app/lib/api/v1/services/token.py
Normal file
28
src/fastapi_app/lib/api/v1/services/token.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from jose import JWTError, jwt
|
||||
from lib.api.v1 import schemas as app_schemas
|
||||
from lib.app import settings as app_settings
|
||||
from pydantic import ValidationError
|
||||
|
||||
import fastapi
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
settings = app_settings.get_settings()
|
||||
|
||||
security = fastapi.security.HTTPBearer()
|
||||
|
||||
|
||||
def get_token_data(
|
||||
authorization: fastapi.security.HTTPAuthorizationCredentials = fastapi.Security(
|
||||
security
|
||||
),
|
||||
) -> app_schemas.entity.Token:
|
||||
token = authorization.credentials
|
||||
try:
|
||||
secret_key = settings.jwt_secret_key
|
||||
payload = jwt.decode(token, secret_key, algorithms=["HS256"])
|
||||
return app_schemas.entity.Token(**payload)
|
||||
except (JWTError, ValidationError):
|
||||
raise fastapi.HTTPException(
|
||||
status_code=fastapi.status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
)
|
||||
Reference in New Issue
Block a user