-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
58 lines (39 loc) · 1.75 KB
/
helper.py
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
from jose import JWTError, jwt
from datetime import datetime, timedelta, timezone
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from dotenv import load_dotenv
import os
load_dotenv()
SECRET_KEY = os.getenv("SECRET_KEY")
ALGORITHM = os.getenv("ALGORITHM")
ACCESS_TOKEN_EXPIRE_MINUTES = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login")
token_blacklist = set()
def create_access_token(data: dict, expires_delta: timedelta = timedelta(minutes=30)):
to_encode = data.copy()
expire = datetime.now(tz=timezone.utc) + expires_delta
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def get_current_user(token: str = Depends(oauth2_scheme)):
if token in token_blacklist:
raise HTTPException(status_code=401, detail="Token has been revoked")
try:
# Token doğrula
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
user_id: int = payload.get("user_id")
username: str = payload.get("username")
role: str = payload.get("role")
if user_id is None or username is None:
raise HTTPException(status_code=401, detail="Invalid token payload")
return {"user_id": user_id, "username": username, "role": role}
except JWTError:
raise HTTPException(status_code=401, detail="Invalid or expired token")
def admin_required(current_user: dict = Depends(get_current_user)):
if current_user["role"] != 1:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to perform this action"
)
return current_user