feat: AI 서버 관리 페이지 Phase 3 보안 강화 - JWT 인증 시스템
🔐 JWT 기반 로그인 시스템: - 로그인 페이지: 아름다운 애니메이션과 보안 정보 표시 - JWT 토큰: 24시간 또는 30일 (Remember Me) 만료 설정 - 비밀번호 암호화: bcrypt 해싱으로 안전한 저장 - 계정 잠금: 5회 실패 시 15분 자동 잠금 👥 사용자 계정 관리: - admin/admin123 (관리자 권한) - hyungi/hyungi123 (시스템 권한) - 역할 기반 접근 제어 (RBAC) 🛡️ 보안 기능: - 토큰 자동 검증 및 만료 처리 - 감사 로그: 로그인/로그아웃/관리 작업 추적 - 안전한 세션 관리 및 토큰 정리 - 클라이언트 사이드 토큰 검증 🎨 UI/UX 개선: - 로그인 페이지: 그라디언트 배경, 플로팅 아이콘 애니메이션 - 사용자 메뉴: 헤더에 사용자명과 로그아웃 버튼 표시 - 보안 표시: SSL, 세션 타임아웃, JWT 인증 정보 - 반응형 디자인 및 다크모드 지원 🔧 기술 구현: - FastAPI HTTPBearer 보안 스키마 - PyJWT 토큰 생성/검증 - bcrypt 비밀번호 해싱 - 클라이언트-서버 토큰 동기화 새 파일: - templates/login.html: 로그인 페이지 HTML - static/login.css: 로그인 페이지 스타일 - static/login.js: 로그인 JavaScript 로직 - server/auth.py: JWT 인증 시스템 (실제 서버용) 수정된 파일: - test_admin.py: 테스트 서버에 JWT 인증 추가 - static/admin.js: JWT 토큰 기반 API 요청으로 변경 - templates/admin.html: 사용자 메뉴 및 로그아웃 버튼 추가 - static/admin.css: 사용자 메뉴 스타일 추가 보안 레벨: Phase 1 (API Key) → Phase 3 (JWT + 감사로그)
This commit is contained in:
163
test_admin.py
163
test_admin.py
@@ -7,7 +7,9 @@ AI Server Admin Dashboard Test Server
|
||||
import os
|
||||
import secrets
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
import jwt
|
||||
import bcrypt
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
@@ -15,6 +17,7 @@ from fastapi import FastAPI, Request, HTTPException, Depends, Header
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
import uvicorn
|
||||
|
||||
# FastAPI 앱 초기화
|
||||
@@ -29,6 +32,25 @@ TEST_API_KEY = os.getenv("API_KEY", "test-admin-key-123")
|
||||
TEST_SERVER_PORT = 28080
|
||||
TEST_OLLAMA_HOST = "http://localhost:11434"
|
||||
|
||||
# JWT 설정
|
||||
JWT_SECRET_KEY = "test-jwt-secret-key-for-development"
|
||||
JWT_ALGORITHM = "HS256"
|
||||
security = HTTPBearer(auto_error=False)
|
||||
|
||||
# 테스트용 사용자 데이터
|
||||
TEST_USERS = {
|
||||
"admin": {
|
||||
"username": "admin",
|
||||
"password_hash": bcrypt.hashpw("admin123".encode('utf-8'), bcrypt.gensalt()).decode('utf-8'),
|
||||
"role": "admin"
|
||||
},
|
||||
"hyungi": {
|
||||
"username": "hyungi",
|
||||
"password_hash": bcrypt.hashpw("hyungi123".encode('utf-8'), bcrypt.gensalt()).decode('utf-8'),
|
||||
"role": "system"
|
||||
}
|
||||
}
|
||||
|
||||
# 임시 데이터 저장소
|
||||
api_keys_store = {
|
||||
"test-key-1": {
|
||||
@@ -71,6 +93,60 @@ test_models = [
|
||||
]
|
||||
|
||||
|
||||
# JWT 인증 함수들
|
||||
def create_jwt_token(user_data: dict, remember_me: bool = False) -> str:
|
||||
"""JWT 토큰 생성"""
|
||||
expiration = datetime.utcnow() + timedelta(
|
||||
days=30 if remember_me else 0,
|
||||
hours=24 if not remember_me else 0
|
||||
)
|
||||
|
||||
payload = {
|
||||
"username": user_data["username"],
|
||||
"role": user_data["role"],
|
||||
"exp": expiration,
|
||||
"iat": datetime.utcnow()
|
||||
}
|
||||
|
||||
return jwt.encode(payload, JWT_SECRET_KEY, algorithm=JWT_ALGORITHM)
|
||||
|
||||
def verify_jwt_token(token: str) -> dict:
|
||||
"""JWT 토큰 검증"""
|
||||
try:
|
||||
payload = jwt.decode(token, JWT_SECRET_KEY, algorithms=[JWT_ALGORITHM])
|
||||
return payload
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPException(status_code=401, detail="Token has expired")
|
||||
except jwt.InvalidTokenError:
|
||||
raise HTTPException(status_code=401, detail="Invalid token")
|
||||
|
||||
def verify_password(password: str, password_hash: str) -> bool:
|
||||
"""비밀번호 검증"""
|
||||
return bcrypt.checkpw(password.encode('utf-8'), password_hash.encode('utf-8'))
|
||||
|
||||
async def get_current_user(credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)):
|
||||
"""현재 인증된 사용자 가져오기"""
|
||||
if not credentials:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
try:
|
||||
payload = verify_jwt_token(credentials.credentials)
|
||||
username = payload.get("username")
|
||||
|
||||
user = TEST_USERS.get(username)
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="User not found")
|
||||
|
||||
return {
|
||||
"username": user["username"],
|
||||
"role": user["role"]
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"JWT verification error: {e}")
|
||||
raise HTTPException(status_code=401, detail="Invalid authentication credentials")
|
||||
|
||||
def require_api_key(x_api_key: Optional[str] = Header(None), api_key: Optional[str] = None):
|
||||
"""API 키 검증 (테스트 모드에서는 URL 파라미터도 허용)"""
|
||||
# URL 파라미터로 API 키가 전달된 경우
|
||||
@@ -82,6 +158,33 @@ def require_api_key(x_api_key: Optional[str] = Header(None), api_key: Optional[s
|
||||
# 테스트 모드에서는 기본 허용
|
||||
return "test-mode"
|
||||
|
||||
async def require_admin_role(current_user: dict = Depends(get_current_user)):
|
||||
"""관리자 권한 필요"""
|
||||
if current_user["role"] not in ["admin", "system"]:
|
||||
raise HTTPException(status_code=403, detail="Admin privileges required")
|
||||
return current_user
|
||||
|
||||
# 유연한 인증 (JWT 또는 API 키)
|
||||
async def flexible_auth(
|
||||
request: Request,
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security),
|
||||
x_api_key: Optional[str] = Header(None)
|
||||
):
|
||||
"""JWT 또는 API 키 인증"""
|
||||
# JWT 토큰 시도
|
||||
if credentials:
|
||||
try:
|
||||
return await get_current_user(credentials)
|
||||
except HTTPException:
|
||||
pass
|
||||
|
||||
# API 키 시도 (테스트 모드)
|
||||
if x_api_key == TEST_API_KEY:
|
||||
return {"username": "api_user", "role": "system"}
|
||||
|
||||
# 둘 다 실패하면 로그인 페이지로 리다이렉트
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def root():
|
||||
@@ -105,10 +208,64 @@ async def health_check():
|
||||
return {"status": "ok", "mode": "test", "timestamp": datetime.now().isoformat()}
|
||||
|
||||
|
||||
# JWT 인증 엔드포인트들
|
||||
@app.get("/login", response_class=HTMLResponse)
|
||||
async def login_page(request: Request):
|
||||
"""로그인 페이지"""
|
||||
return templates.TemplateResponse("login.html", {"request": request})
|
||||
|
||||
@app.post("/admin/login")
|
||||
async def admin_login(request: Request):
|
||||
"""JWT 기반 로그인 (테스트 모드)"""
|
||||
try:
|
||||
data = await request.json()
|
||||
username = data.get("username", "").strip()
|
||||
password = data.get("password", "")
|
||||
remember_me = data.get("remember_me", False)
|
||||
|
||||
if not username or not password:
|
||||
return {"success": False, "message": "Username and password are required"}
|
||||
|
||||
# 사용자 인증
|
||||
user = TEST_USERS.get(username)
|
||||
if user and verify_password(password, user["password_hash"]):
|
||||
# JWT 토큰 생성
|
||||
token = create_jwt_token(user, remember_me)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": "Login successful",
|
||||
"token": token,
|
||||
"user": {
|
||||
"username": user["username"],
|
||||
"role": user["role"]
|
||||
}
|
||||
}
|
||||
else:
|
||||
return {"success": False, "message": "Invalid username or password"}
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "message": "Login error occurred"}
|
||||
|
||||
@app.get("/admin/verify-token")
|
||||
async def verify_token(current_user: dict = Depends(get_current_user)):
|
||||
"""JWT 토큰 검증"""
|
||||
return {
|
||||
"valid": True,
|
||||
"user": current_user
|
||||
}
|
||||
|
||||
@app.post("/admin/logout")
|
||||
async def admin_logout(current_user: dict = Depends(get_current_user)):
|
||||
"""로그아웃"""
|
||||
return {"success": True, "message": "Logged out successfully"}
|
||||
|
||||
|
||||
# Admin Dashboard Routes
|
||||
@app.get("/admin", response_class=HTMLResponse)
|
||||
async def admin_dashboard(request: Request, api_key: str = Depends(require_api_key)):
|
||||
"""관리자 대시보드 페이지"""
|
||||
async def admin_dashboard(request: Request):
|
||||
"""관리자 대시보드 페이지 (클라이언트에서 JWT 검증)"""
|
||||
# HTML 페이지를 먼저 반환하고, JavaScript에서 토큰 검증
|
||||
return templates.TemplateResponse("admin.html", {
|
||||
"request": request,
|
||||
"server_port": TEST_SERVER_PORT,
|
||||
|
||||
Reference in New Issue
Block a user