-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
72 lines (60 loc) · 2.87 KB
/
Copy pathauth.py
File metadata and controls
72 lines (60 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import re
from dotenv import load_dotenv
from fastapi import HTTPException, Depends
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from sqlalchemy.orm import Session
from datetime import datetime, timedelta, timezone
from database import get_db
from models import User
from passlib.context import CryptContext
load_dotenv()
# 비밀번호 해싱을 위한 CryptContext 객체 생성
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# 비밀번호 해싱 함수
def hash_password(password: str) -> str:
return pwd_context.hash(password)
def is_valid_email(email: str) -> bool:
email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zAZ0-9-]+\.[a-zA-Z0-9-.]+$'
return re.match(email_regex, email) is not None
# 비밀번호 확인 함수
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
SECRET_KEY = os.getenv("SECRET_KEY", "default_key")
ALGORITHM = os.getenv("ALGORITHM", "HS256")
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 60))
# JWT 생성 함수
def create_access_token(data: dict, expires_delta: timedelta = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)):
to_encode = data.copy()
expire = datetime.utcnow() + expires_delta
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="users/login") # 토큰을 가져올 경로
# JWT 토큰 검증 함수
def verify_token(token: str):
credentials_exception = HTTPException(
status_code=401,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
exp_timestamp = payload.get("exp")
print("Decoded payload:", payload)
if exp_timestamp:
exp_datetime = datetime.fromtimestamp(exp_timestamp, tz=timezone.utc)
if datetime.now(timezone.utc) > exp_datetime:
raise credentials_exception # 만료된 토큰일 경우
return payload # payload는 토큰에서 디코딩된 사용자 정보
except JWTError:
raise credentials_exception
def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)) -> dict:
payload = verify_token(token) # verify_token은 토큰을 검증하고 payload를 반환
user_email = payload.get("sub") # "sub"는 이메일에 해당할 수 있음 (다른 방식으로 설정했으면 그에 맞게 변경)
# 이메일을 통해 사용자를 찾음
user = db.query(User).filter(User.email == user_email).first()
if not user:
raise HTTPException(status_code=401, detail="User not found")
return {"userId": user.userId, "email": user.email, "nickname": user.nickname}