add transport and strategy
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/.venv/
|
||||
/.idea
|
||||
/.idea
|
||||
/src/db/*.db
|
||||
.env
|
||||
@@ -1,11 +1,14 @@
|
||||
from typing import Annotated, AsyncGenerator
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi_users.authentication.strategy import AccessTokenDatabase, DatabaseStrategy
|
||||
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
|
||||
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyAccessTokenDatabase
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.db.database import async_session_maker
|
||||
from src.models import UsersORM
|
||||
from src.models import UsersORM, AccessToken
|
||||
from src.settings import settings
|
||||
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
@@ -16,5 +19,22 @@ async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
DBDep = Annotated[AsyncSession, Depends(get_db)]
|
||||
|
||||
|
||||
async def get_user_db(session: DBDep):
|
||||
async def get_user_db(
|
||||
session: DBDep
|
||||
):
|
||||
yield SQLAlchemyUserDatabase(session, UsersORM)
|
||||
|
||||
|
||||
async def get_access_token_db(
|
||||
session: DBDep,
|
||||
):
|
||||
yield SQLAlchemyAccessTokenDatabase(session, AccessToken)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
...
|
||||
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
|
||||
lifetime: int
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
0
src/transport/__init__.py
Normal file
0
src/transport/__init__.py
Normal file
3
src/transport/transport.py
Normal file
3
src/transport/transport.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from fastapi_users.authentication import BearerTransport
|
||||
|
||||
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
|
||||
Reference in New Issue
Block a user