Skip to content

Commit

Permalink
(backend): added auth backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Imanuel Febie committed Oct 26, 2024
1 parent 469c28d commit d24ac5f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

# All 4 One
# One4All

</div>

Expand Down
Empty file added o4a-api/o4a/__init__.py
Empty file.
Empty file added o4a-api/o4a/api/routes.py
Empty file.
Empty file added o4a-api/o4a/auth/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions o4a-api/o4a/auth/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import uuid

import redis.asyncio
from fastapi import Depends, Request, Response
from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin
from fastapi_users.authentication import (
AuthenticationBackend,
BearerTransport,
RedisStrategy,
)

from o4a.lib.config.settings import jwt_settings
from o4a.lib.dependencies.db import get_user_db
from o4a.types.models import User


class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
reset_password_token_secret = jwt_settings.jwt_secret
verification_token_secret = jwt_settings.jwt_secret

async def on_after_login(
self,
user: User,
request: Request | None = None,
response: Response | None = None,
):
print(f"User with email {user.email} has logged in")

async def on_after_register(self, user: User, request: Request | None = None):
print(f"User with email {user.email} has successfully been registered")

async def on_after_request_verify(
self, user: User, token: str, request: Request | None = None
):
print(f"Verification token: {token} for user with mail ${user.email}")


async def get_user_manager(user_db=Depends(get_user_db)):
yield UserManager(user_db)


bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")

redis = redis.asyncio.from_url("redis://pinnacle-redis:6379", decode_responses=True)


def get_redis_strategy() -> RedisStrategy:
return RedisStrategy(redis, lifetime_seconds=None)


auth_backend = AuthenticationBackend(
name="redis", transport=bearer_transport, get_strategy=get_redis_strategy
)

users = FastAPIUsers[User, uuid.UUID](get_user_manager, [auth_backend])

get_current_user = users.current_user()
8 changes: 8 additions & 0 deletions o4a-api/o4a/lib/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ class Config:
env_file = str(Path(__file__).resolve().parent.parent.parent.parent / ".env")


class JWTSettings(BaseSettings):
jwt_secret: str

class Config:
env_file = str(Path(__file__).resolve().parent.parent.parent.parent / ".env")


pg_settings = PostgresSettings()
jwt_settings = JWTSettings()
15 changes: 12 additions & 3 deletions o4a-api/o4a/lib/dependencies/db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from collections.abc import AsyncGenerator

# from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from fastapi import Depends
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker,
create_async_engine)

DB_URL = f""
from o4a.lib.config.settings import pg_settings
from o4a.types.models import User

DB_URL = f"postgresql+asyncpg://{pg_settings.postgres_user}:{pg_settings.postgres_password}@{pg_settings.postgres_host}:{pg_settings.postgres_port}/{pg_settings.postgres_db}"

engine = create_async_engine(DB_URL)
session_maker = async_sessionmaker(engine, expire_on_commit=False)
Expand All @@ -12,3 +17,7 @@
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
async with session_maker() as session:
yield session


async def get_user_db(session: AsyncSession = Depends(get_async_session)):
yield SQLAlchemyUserDatabase(session, User)

0 comments on commit d24ac5f

Please sign in to comment.