add transport and strategy
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
from typing import Annotated, AsyncGenerator
|
from typing import Annotated, AsyncGenerator
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from fastapi_users.authentication.strategy import AccessTokenDatabase, DatabaseStrategy
|
from fastapi_users.authentication.strategy import AccessTokenDatabase
|
||||||
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
|
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
|
||||||
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyAccessTokenDatabase
|
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyAccessTokenDatabase
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from src.db.database import async_session_maker
|
from src.db.database import async_session_maker
|
||||||
from src.models import UsersORM, AccessToken
|
from src.models import UsersORM, AccessToken
|
||||||
from src.settings import settings
|
|
||||||
|
|
||||||
|
|
||||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||||
@@ -32,9 +31,3 @@ async def get_access_token_db(
|
|||||||
|
|
||||||
|
|
||||||
ATDep = Annotated[AccessTokenDatabase[AccessToken], Depends(get_access_token_db)]
|
ATDep = Annotated[AccessTokenDatabase[AccessToken], Depends(get_access_token_db)]
|
||||||
|
|
||||||
|
|
||||||
def get_database_strategy(
|
|
||||||
access_token_db: ATDep,
|
|
||||||
) -> DatabaseStrategy:
|
|
||||||
return DatabaseStrategy(access_token_db, lifetime_seconds=settings.lifetime)
|
|
||||||
21
src/api/dependacies/strategy.py
Normal file
21
src/api/dependacies/strategy.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from fastapi import Depends
|
||||||
|
from fastapi_users.authentication import AuthenticationBackend
|
||||||
|
from fastapi_users.authentication.strategy import DatabaseStrategy, AccessTokenDatabase
|
||||||
|
|
||||||
|
from src.api.dependacies.dependancies import get_access_token_db
|
||||||
|
from src.api.dependacies.transport import bearer_transport
|
||||||
|
from src.models import AccessToken
|
||||||
|
from src.settings import settings
|
||||||
|
|
||||||
|
|
||||||
|
def get_database_strategy(
|
||||||
|
access_token_db: AccessTokenDatabase[AccessToken] = Depends(get_access_token_db),
|
||||||
|
) -> DatabaseStrategy:
|
||||||
|
return DatabaseStrategy(access_token_db, lifetime_seconds=settings.lifetime)
|
||||||
|
|
||||||
|
|
||||||
|
auth_backend = AuthenticationBackend(
|
||||||
|
name="database",
|
||||||
|
transport=bearer_transport,
|
||||||
|
get_strategy=get_database_strategy,
|
||||||
|
)
|
||||||
4
src/api/dependacies/transport.py
Normal file
4
src/api/dependacies/transport.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from fastapi_users.authentication import BearerTransport
|
||||||
|
|
||||||
|
|
||||||
|
bearer_transport = BearerTransport(tokenUrl="auth/login")
|
||||||
@@ -7,21 +7,21 @@ router = APIRouter(prefix="/tasks", tags=["Tasks"])
|
|||||||
async def get_tasks(): ...
|
async def get_tasks(): ...
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{task_id}")
|
@router.get("/{id}")
|
||||||
async def get_task_id(task_id: int): ...
|
async def get_task_id(id: int): ...
|
||||||
|
|
||||||
|
|
||||||
@router.post("/")
|
@router.post("/")
|
||||||
async def post_task(): ...
|
async def post_task(): ...
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{task_id}")
|
@router.put("/{id}")
|
||||||
async def put_task(task_id: int): ...
|
async def put_task(id: int): ...
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{task_id}")
|
@router.patch("/{id}")
|
||||||
async def patch_task(task_id: int): ...
|
async def patch_task(id: int): ...
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{task_id}")
|
@router.delete("/{id}")
|
||||||
async def delete_task(task_id: int): ...
|
async def delete_task(id: int): ...
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
|||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
|
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
|
||||||
lifetime: int
|
LIFETIME: int
|
||||||
|
SECRET: str
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
from fastapi_users.authentication import BearerTransport
|
|
||||||
|
|
||||||
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
|
|
||||||
Reference in New Issue
Block a user