Spaces:
Sleeping
Sleeping
Upload app/core/security.py
Browse files- app/core/security.py +66 -0
app/core/security.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timedelta, timezone
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
from jose import JWTError, jwt
|
| 5 |
+
from passlib.context import CryptContext
|
| 6 |
+
from fastapi import Depends, HTTPException, status
|
| 7 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 8 |
+
|
| 9 |
+
from app.core.config import settings
|
| 10 |
+
|
| 11 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 12 |
+
security_scheme = HTTPBearer()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def hash_password(password: str) -> str:
|
| 16 |
+
return pwd_context.hash(password)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
| 20 |
+
return pwd_context.verify(plain_password, hashed_password)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
| 24 |
+
to_encode = data.copy()
|
| 25 |
+
expire = datetime.now(timezone.utc) + (
|
| 26 |
+
expires_delta or timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
| 27 |
+
)
|
| 28 |
+
to_encode.update({"exp": expire, "type": "access"})
|
| 29 |
+
return jwt.encode(to_encode, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def create_refresh_token(data: dict) -> str:
|
| 33 |
+
to_encode = data.copy()
|
| 34 |
+
expire = datetime.now(timezone.utc) + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
|
| 35 |
+
to_encode.update({"exp": expire, "type": "refresh"})
|
| 36 |
+
return jwt.encode(to_encode, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def decode_token(token: str) -> dict:
|
| 40 |
+
try:
|
| 41 |
+
payload = jwt.decode(token, settings.JWT_SECRET, algorithms=[settings.JWT_ALGORITHM])
|
| 42 |
+
return payload
|
| 43 |
+
except JWTError:
|
| 44 |
+
raise HTTPException(
|
| 45 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 46 |
+
detail="Invalid or expired token",
|
| 47 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
async def get_current_user(
|
| 52 |
+
credentials: HTTPAuthorizationCredentials = Depends(security_scheme),
|
| 53 |
+
):
|
| 54 |
+
payload = decode_token(credentials.credentials)
|
| 55 |
+
if payload.get("type") != "access":
|
| 56 |
+
raise HTTPException(
|
| 57 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 58 |
+
detail="Invalid token type",
|
| 59 |
+
)
|
| 60 |
+
user_id = payload.get("sub")
|
| 61 |
+
if user_id is None:
|
| 62 |
+
raise HTTPException(
|
| 63 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 64 |
+
detail="Invalid token payload",
|
| 65 |
+
)
|
| 66 |
+
return int(user_id)
|