first commit
This commit is contained in:
BIN
app/utils/__pycache__/exceptions.cpython-312.pyc
Normal file
BIN
app/utils/__pycache__/exceptions.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/utils/__pycache__/jwt_handler.cpython-312.pyc
Normal file
BIN
app/utils/__pycache__/jwt_handler.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/utils/__pycache__/security.cpython-312.pyc
Normal file
BIN
app/utils/__pycache__/security.cpython-312.pyc
Normal file
Binary file not shown.
31
app/utils/exceptions.py
Normal file
31
app/utils/exceptions.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
class AuthenticationError(HTTPException):
|
||||
"""인증 오류"""
|
||||
def __init__(self, detail: str = "인증에 실패했습니다"):
|
||||
super().__init__(status_code=status.HTTP_401_UNAUTHORIZED, detail=detail)
|
||||
|
||||
class PermissionDeniedError(HTTPException):
|
||||
"""권한 부족 오류"""
|
||||
def __init__(self, detail: str = "권한이 없습니다"):
|
||||
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)
|
||||
|
||||
class NotFoundError(HTTPException):
|
||||
"""리소스를 찾을 수 없음"""
|
||||
def __init__(self, detail: str = "리소스를 찾을 수 없습니다"):
|
||||
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail=detail)
|
||||
|
||||
class BadRequestError(HTTPException):
|
||||
"""잘못된 요청"""
|
||||
def __init__(self, detail: str = "잘못된 요청입니다"):
|
||||
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
|
||||
|
||||
class ConflictError(HTTPException):
|
||||
"""충돌 오류 (중복 등)"""
|
||||
def __init__(self, detail: str = "이미 존재하는 리소스입니다"):
|
||||
super().__init__(status_code=status.HTTP_409_CONFLICT, detail=detail)
|
||||
|
||||
class InternalServerError(HTTPException):
|
||||
"""서버 내부 오류"""
|
||||
def __init__(self, detail: str = "서버 오류가 발생했습니다"):
|
||||
super().__init__(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=detail)
|
||||
38
app/utils/jwt_handler.py
Normal file
38
app/utils/jwt_handler.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from jose import JWTError, jwt
|
||||
from app.config import settings
|
||||
|
||||
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
||||
"""JWT Access Token 생성"""
|
||||
to_encode = data.copy()
|
||||
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=settings.JWT_ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, settings.JWT_SECRET_KEY, algorithm=settings.JWT_ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
def create_refresh_token(data: dict) -> str:
|
||||
"""JWT Refresh Token 생성"""
|
||||
to_encode = data.copy()
|
||||
expire = datetime.utcnow() + timedelta(days=settings.JWT_REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
to_encode.update({"exp": expire, "type": "refresh"})
|
||||
encoded_jwt = jwt.encode(to_encode, settings.JWT_SECRET_KEY, algorithm=settings.JWT_ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
def decode_token(token: str) -> Optional[dict]:
|
||||
"""JWT 토큰 디코드"""
|
||||
try:
|
||||
payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms=[settings.JWT_ALGORITHM])
|
||||
return payload
|
||||
except JWTError:
|
||||
return None
|
||||
|
||||
def verify_token(token: str) -> bool:
|
||||
"""토큰 유효성 검증"""
|
||||
payload = decode_token(token)
|
||||
return payload is not None
|
||||
12
app/utils/security.py
Normal file
12
app/utils/security.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from passlib.context import CryptContext
|
||||
|
||||
# bcrypt 컨텍스트
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
"""비밀번호 해시화"""
|
||||
return pwd_context.hash(password)
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
"""비밀번호 검증"""
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
Reference in New Issue
Block a user