Initial commit: OAuth 2.0 인증 시스템 with APISIX API Gateway

- FastAPI 백엔드 + MongoDB + Redis 구성
- React + Vite + TypeScript + shadcn/ui 프론트엔드
- Apache APISIX API Gateway 통합
- Docker Compose 기반 개발 환경
- 3단계 권한 체계 (System Admin, Group Admin, User)
- 동적 테마 지원
- 환경별 설정 (dev/vei/prod)

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude
2025-08-31 10:16:41 +09:00
commit f53d55e712
55 changed files with 6798 additions and 0 deletions

View File

View File

@ -0,0 +1,9 @@
from fastapi import APIRouter
from app.api.v1.endpoints import auth, users, applications, admin
api_router = APIRouter()
api_router.include_router(auth.router, prefix="/auth", tags=["authentication"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(applications.router, prefix="/applications", tags=["applications"])
api_router.include_router(admin.router, prefix="/admin", tags=["admin"])

View File

@ -0,0 +1,49 @@
from typing import List, Union
from pydantic_settings import BaseSettings
from pydantic import field_validator
import os
class Settings(BaseSettings):
PROJECT_NAME: str = "OAuth Authentication System"
VERSION: str = "1.0.0"
API_V1_STR: str = "/api/v1"
SECRET_KEY: str = os.getenv("SECRET_KEY", "0198fda4-294e-77b0-a95d-2b601d2c594d")
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
MONGODB_URL: str = os.getenv("MONGODB_URL", "mongodb://localhost:27017")
DATABASE_NAME: str = os.getenv("DATABASE_NAME", "oauth_db")
REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379")
BACKEND_CORS_ORIGINS: List[str] = ["http://localhost:3000", "http://localhost:5173"]
ENVIRONMENT: str = os.getenv("ENVIRONMENT", "dev")
BACKUP_PATH: str = os.getenv("BACKUP_PATH", "/var/backups/oauth")
ARCHIVE_PATH: str = os.getenv("ARCHIVE_PATH", "/var/archives/oauth")
SMTP_HOST: str = os.getenv("SMTP_HOST", "")
SMTP_PORT: int = int(os.getenv("SMTP_PORT", "587"))
SMTP_USER: str = os.getenv("SMTP_USER", "")
SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
NEXUS_URL: str = os.getenv("NEXUS_URL", "")
NEXUS_REPOSITORY: str = os.getenv("NEXUS_REPOSITORY", "")
@field_validator("BACKEND_CORS_ORIGINS", mode="before")
@classmethod
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
if isinstance(v, str) and not v.startswith("["):
return [i.strip() for i in v.split(",")]
elif isinstance(v, (list, str)):
return v
raise ValueError(v)
class Config:
env_file = ".env"
case_sensitive = True
settings = Settings()

View File

@ -0,0 +1,38 @@
from motor.motor_asyncio import AsyncIOMotorClient
from app.core.config import settings
import redis.asyncio as redis
from typing import Optional
class Database:
client: Optional[AsyncIOMotorClient] = None
database = None
redis_client: Optional[redis.Redis] = None
db = Database()
async def init_db():
db.client = AsyncIOMotorClient(settings.MONGODB_URL)
db.database = db.client[settings.DATABASE_NAME]
db.redis_client = await redis.from_url(settings.REDIS_URL, decode_responses=True)
await create_indexes()
async def close_db():
if db.client:
db.client.close()
if db.redis_client:
await db.redis_client.close()
async def create_indexes():
await db.database.users.create_index("email", unique=True)
await db.database.users.create_index("username", unique=True)
await db.database.applications.create_index("client_id", unique=True)
await db.database.applications.create_index("app_name", unique=True)
await db.database.auth_history.create_index([("user_id", 1), ("created_at", -1)])
await db.database.auth_history.create_index("created_at")
def get_database():
return db.database
def get_redis():
return db.redis_client

38
oauth/backend/app/main.py Normal file
View File

@ -0,0 +1,38 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from app.core.config import settings
from app.core.database import init_db, close_db
from app.api.v1.router import api_router
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
await init_db()
logger.info("Database initialized")
yield
await close_db()
logger.info("Database connection closed")
app = FastAPI(
title=settings.PROJECT_NAME,
version=settings.VERSION,
lifespan=lifespan
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.BACKEND_CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=settings.API_V1_STR)
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "OAuth Authentication System"}

View File

@ -0,0 +1,54 @@
from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional, Dict, Any
class ApplicationTheme(BaseModel):
primary_color: str = "#1976d2"
secondary_color: str = "#dc004e"
background_color: str = "#ffffff"
text_color: str = "#000000"
logo_url: Optional[str] = None
background_image_url: Optional[str] = None
font_family: str = "Roboto, sans-serif"
border_radius: str = "8px"
custom_css: Optional[str] = None
class ApplicationBase(BaseModel):
app_name: str
description: str
redirect_uris: list[str]
allowed_origins: list[str]
theme: ApplicationTheme = ApplicationTheme()
is_active: bool = True
allow_registration: bool = True
require_email_verification: bool = False
class ApplicationCreate(ApplicationBase):
pass
class ApplicationUpdate(BaseModel):
app_name: Optional[str] = None
description: Optional[str] = None
redirect_uris: Optional[list[str]] = None
allowed_origins: Optional[list[str]] = None
theme: Optional[ApplicationTheme] = None
is_active: Optional[bool] = None
allow_registration: Optional[bool] = None
require_email_verification: Optional[bool] = None
class ApplicationInDB(ApplicationBase):
id: str = Field(alias="_id")
client_id: str
client_secret: str
created_at: datetime
updated_at: datetime
created_by: str
class Config:
populate_by_name = True
class Application(ApplicationBase):
id: str
client_id: str
created_at: datetime
updated_at: datetime

View File

@ -0,0 +1,54 @@
from pydantic import BaseModel, EmailStr, Field
from datetime import datetime
from typing import Optional, List
from enum import Enum
class UserRole(str, Enum):
SYSTEM_ADMIN = "system_admin"
GROUP_ADMIN = "group_admin"
USER = "user"
class UserBase(BaseModel):
email: EmailStr
username: str
full_name: str
role: UserRole = UserRole.USER
is_active: bool = True
phone_number: Optional[str] = None
birth_date: Optional[str] = None
gender: Optional[str] = None
profile_picture: Optional[str] = None
class UserCreate(UserBase):
password: str
class UserUpdate(BaseModel):
full_name: Optional[str] = None
phone_number: Optional[str] = None
birth_date: Optional[str] = None
gender: Optional[str] = None
profile_picture: Optional[str] = None
class UserInDB(UserBase):
id: str = Field(alias="_id")
hashed_password: str
created_at: datetime
updated_at: datetime
last_login: Optional[datetime] = None
class Config:
populate_by_name = True
class User(UserBase):
id: str
created_at: datetime
updated_at: datetime
last_login: Optional[datetime] = None
class UserPermissions(BaseModel):
single_sign_on: bool = True
share_name: bool = True
share_gender: bool = False
share_birth_date: bool = False
share_email: bool = True
share_phone: bool = False