feat(infra): add persistence and SMS adapters
This commit is contained in:
60
src/gapido_auth/infrastructure/container.py
Normal file
60
src/gapido_auth/infrastructure/container.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from aio_pika.abc import AbstractChannel
|
||||
from motor.motor_asyncio import AsyncIOMotorClient
|
||||
from redis.asyncio import Redis
|
||||
|
||||
from gapido_auth.application.auth_service import AuthConfig, AuthService
|
||||
from gapido_auth.application.security import JwtTokenCodec
|
||||
from gapido_auth.config import Settings
|
||||
from gapido_auth.infrastructure.mongo_repositories import (
|
||||
MongoRefreshSessionRepository,
|
||||
MongoUserRepository,
|
||||
)
|
||||
from gapido_auth.infrastructure.rabbitmq import RabbitMqSmsPublisher
|
||||
from gapido_auth.infrastructure.redis_otp_store import RedisOtpStore
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class AppContainer:
|
||||
auth_service: AuthService
|
||||
mongo_client: AsyncIOMotorClient[Any]
|
||||
redis: Redis
|
||||
|
||||
|
||||
async def build_auth_service(settings: Settings, rabbitmq_channel: AbstractChannel) -> AppContainer:
|
||||
mongo_client: AsyncIOMotorClient[Any] = AsyncIOMotorClient(settings.mongo_uri)
|
||||
db = mongo_client[settings.mongo_db_name]
|
||||
users = MongoUserRepository(db)
|
||||
refresh_sessions = MongoRefreshSessionRepository(db)
|
||||
await users.create_indexes()
|
||||
await refresh_sessions.create_indexes()
|
||||
await users.ensure_admin_user(settings.admin_mobile)
|
||||
|
||||
redis = Redis.from_url(settings.redis_url, decode_responses=False)
|
||||
otp_store = RedisOtpStore(redis)
|
||||
sms_publisher = RabbitMqSmsPublisher(rabbitmq_channel)
|
||||
token_codec = JwtTokenCodec(
|
||||
secret_key=settings.jwt_secret_key,
|
||||
issuer=settings.jwt_issuer,
|
||||
access_ttl_seconds=settings.access_token_ttl_seconds,
|
||||
)
|
||||
auth_service = AuthService(
|
||||
users=users,
|
||||
refresh_sessions=refresh_sessions,
|
||||
otp_store=otp_store,
|
||||
sms_publisher=sms_publisher,
|
||||
token_codec=token_codec,
|
||||
config=AuthConfig(
|
||||
otp_secret=settings.jwt_secret_key,
|
||||
otp_ttl_seconds=settings.otp_ttl_seconds,
|
||||
otp_max_attempts=settings.otp_max_attempts,
|
||||
otp_request_limit=settings.otp_request_limit,
|
||||
otp_request_window_seconds=settings.otp_request_window_seconds,
|
||||
refresh_token_ttl_seconds=settings.refresh_token_ttl_seconds,
|
||||
sms_template=settings.kavenegar_login_template,
|
||||
),
|
||||
)
|
||||
return AppContainer(auth_service=auth_service, mongo_client=mongo_client, redis=redis)
|
||||
|
||||
Reference in New Issue
Block a user