forked from mtusi2026/mtusi2026lab2
add migrations, create domain and crud layers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import base64
|
||||
import binascii
|
||||
from collections.abc import AsyncIterator
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, Header, Request
|
||||
from redis.asyncio import Redis
|
||||
@@ -38,7 +39,9 @@ def _split_authorization(authorization: str | None) -> tuple[str, str] | None:
|
||||
return scheme.lower(), value.strip()
|
||||
|
||||
|
||||
async def get_bearer_token(authorization: str | None = Header(default=None)) -> str:
|
||||
async def get_bearer_token(
|
||||
authorization: Annotated[str | None, Header()] = None,
|
||||
) -> str:
|
||||
parts = _split_authorization(authorization)
|
||||
if parts is None or parts[0] != "bearer":
|
||||
raise unauthorized_bearer()
|
||||
@@ -46,7 +49,7 @@ async def get_bearer_token(authorization: str | None = Header(default=None)) ->
|
||||
|
||||
|
||||
async def get_basic_credentials(
|
||||
authorization: str | None = Header(default=None),
|
||||
authorization: Annotated[str | None, Header()] = None,
|
||||
) -> tuple[str, str]:
|
||||
"""(username, password) из `Authorization: Basic`; пароль может содержать ':'."""
|
||||
parts = _split_authorization(authorization)
|
||||
@@ -64,9 +67,17 @@ async def get_basic_credentials(
|
||||
return username, password
|
||||
|
||||
|
||||
DbSession = Annotated[AsyncSession, Depends(get_db)]
|
||||
RedisClient = Annotated[Redis, Depends(get_redis)]
|
||||
AppSettings = Annotated[Settings, Depends(get_settings)]
|
||||
BearerToken = Annotated[str, Depends(get_bearer_token)]
|
||||
BasicCredentials = Annotated[tuple[str, str], Depends(get_basic_credentials)]
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
token: str = Depends(get_bearer_token),
|
||||
session: AsyncSession = Depends(get_db),
|
||||
redis: Redis = Depends(get_redis),
|
||||
token: BearerToken, session: DbSession, redis: RedisClient
|
||||
) -> User:
|
||||
return await authenticate_token(session, redis, token)
|
||||
|
||||
|
||||
CurrentUser = Annotated[User, Depends(get_current_user)]
|
||||
|
||||
Reference in New Issue
Block a user