Files
labs/oauth/backend/app/utils/database.py
jungwoo choi 6c21809a24 feat: OAuth 2.0 백엔드 시스템 구현 완료
Phase 1 & 2 완료:
- 프로젝트 기본 구조 설정
- Docker Compose 환경 구성 (MongoDB, Redis, Backend, Frontend)
- FastAPI 기반 OAuth 2.0 백엔드 구현

주요 기능:
- JWT 기반 인증 시스템
- 3단계 권한 체계 (System Admin/Group Admin/User)
- 사용자 관리 CRUD API
- 애플리케이션 관리 CRUD API
- OAuth 2.0 Authorization Code Flow
- Refresh Token 관리
- 인증 히스토리 추적

API 엔드포인트:
- /auth/* - 인증 관련 (register, login, logout, refresh)
- /users/* - 사용자 관리
- /applications/* - 애플리케이션 관리
- /oauth/* - OAuth 2.0 플로우

보안 기능:
- bcrypt 비밀번호 해싱
- JWT 토큰 인증
- CORS 설정
- Rate limiting 준비

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-05 14:56:02 +09:00

87 lines
2.7 KiB
Python

"""MongoDB database connection and utilities"""
from motor.motor_asyncio import AsyncIOMotorClient
from typing import Optional
from app.config import settings
import logging
logger = logging.getLogger(__name__)
class Database:
client: Optional[AsyncIOMotorClient] = None
database = None
db = Database()
async def connect_database():
"""Create database connection"""
try:
db.client = AsyncIOMotorClient(settings.mongodb_url)
db.database = db.client[settings.database_name]
# Test connection
await db.client.server_info()
logger.info("Successfully connected to MongoDB")
# Create indexes
await create_indexes()
except Exception as e:
logger.error(f"Failed to connect to MongoDB: {e}")
raise
async def disconnect_database():
"""Close database connection"""
if db.client:
db.client.close()
logger.info("Disconnected from MongoDB")
async def create_indexes():
"""Create database indexes for better performance"""
try:
# Users collection indexes
users_collection = db.database["users"]
await users_collection.create_index("email", unique=True)
await users_collection.create_index("username", unique=True)
await users_collection.create_index("created_at")
# Applications collection indexes
apps_collection = db.database["applications"]
await apps_collection.create_index("client_id", unique=True)
await apps_collection.create_index("created_by")
await apps_collection.create_index("created_at")
# Auth history collection indexes
history_collection = db.database["auth_history"]
await history_collection.create_index("user_id")
await history_collection.create_index("application_id")
await history_collection.create_index("created_at")
await history_collection.create_index(
[("created_at", 1)],
expireAfterSeconds=2592000 # 30 days
)
# Refresh tokens collection indexes
tokens_collection = db.database["refresh_tokens"]
await tokens_collection.create_index("token", unique=True)
await tokens_collection.create_index("user_id")
await tokens_collection.create_index("expires_at")
await tokens_collection.create_index(
[("expires_at", 1)],
expireAfterSeconds=0
)
logger.info("Database indexes created successfully")
except Exception as e:
logger.error(f"Failed to create indexes: {e}")
def get_database():
"""Get database instance"""
return db.database