mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-20 15:55:42 +02:00
making progress
This commit is contained in:
@@ -10,3 +10,12 @@ class AuthConfig(BaseSettings):
|
||||
@property
|
||||
def jwt_signing_key(self):
|
||||
return self._jwt_signing_key
|
||||
|
||||
|
||||
class OAuth2Config(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_prefix='OAUTH_')
|
||||
client_id: str
|
||||
client_secret: str
|
||||
authorize_endpoint: str
|
||||
access_token_endpoint: str
|
||||
name: str = "OAuth2"
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi_users.db import SQLAlchemyBaseUserTableUUID, SQLAlchemyUserDatabase
|
||||
from fastapi_users.db import SQLAlchemyBaseUserTableUUID, SQLAlchemyUserDatabase, SQLAlchemyBaseOAuthAccountTableUUID
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.orm import Mapped, relationship
|
||||
|
||||
import database
|
||||
import backend.src.database as database
|
||||
from backend.src.database import Base
|
||||
|
||||
|
||||
class User(SQLAlchemyBaseUserTableUUID, database.Base):
|
||||
class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):
|
||||
pass
|
||||
|
||||
|
||||
class User(SQLAlchemyBaseUserTableUUID, Base):
|
||||
oauth_accounts: Mapped[list[OAuthAccount]] = relationship(
|
||||
"OAuthAccount", lazy="joined"
|
||||
)
|
||||
|
||||
|
||||
engine = create_async_engine(database.db_url, echo=False)
|
||||
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
@@ -21,4 +29,4 @@ async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
|
||||
|
||||
async def get_user_db(session: AsyncSession = Depends(get_async_session)):
|
||||
yield SQLAlchemyUserDatabase(session, User)
|
||||
yield SQLAlchemyUserDatabase(session, User, OAuthAccount)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
@@ -9,6 +10,7 @@ from fastapi_users.authentication import (
|
||||
CookieTransport, JWTStrategy,
|
||||
)
|
||||
from fastapi_users.db import SQLAlchemyUserDatabase
|
||||
from httpx_oauth.oauth2 import OAuth2
|
||||
|
||||
import auth.config
|
||||
from auth.db import User, get_user_db
|
||||
@@ -17,6 +19,19 @@ config = auth.config.AuthConfig()
|
||||
SECRET = config.token_secret
|
||||
LIFETIME = config.session_lifetime
|
||||
|
||||
if os.getenv("OAUTH_ENABLED") == "True":
|
||||
oauth2_config = auth.config.OAuth2Config()
|
||||
|
||||
oauth_client = OAuth2(
|
||||
client_id=oauth2_config.client_id,
|
||||
client_secret=oauth2_config.client_secret,
|
||||
name=oauth2_config.name,
|
||||
authorize_endpoint=oauth2_config.authorize_endpoint,
|
||||
access_token_endpoint=oauth2_config.access_token_endpoint,
|
||||
)
|
||||
else:
|
||||
oauth_client = None
|
||||
|
||||
|
||||
# TODO: implement on_xxx methods
|
||||
class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
|
||||
@@ -31,11 +46,19 @@ class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
|
||||
):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
|
||||
async def on_after_reset_password(self, user: User, request: Optional[Request] = None):
|
||||
print(f"User {user.id} has reset their password.")
|
||||
|
||||
async def on_after_request_verify(
|
||||
self, user: User, token: str, request: Optional[Request] = None
|
||||
):
|
||||
print(f"Verification requested for user {user.id}. Verification token: {token}")
|
||||
|
||||
async def on_after_verify(
|
||||
self, user: User, request: Optional[Request] = None
|
||||
):
|
||||
print(f"User {user.id} has been verified")
|
||||
|
||||
|
||||
async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)):
|
||||
yield UserManager(user_db)
|
||||
|
||||
Reference in New Issue
Block a user