-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.py
More file actions
23 lines (17 loc) · 807 Bytes
/
security.py
File metadata and controls
23 lines (17 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from datetime import UTC, datetime, timedelta
from typing import Optional, Union
from fastapi.security import OAuth2PasswordBearer
from jose import jwt
from app.core.config import settings
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/auth/login")
def create_access_token(subject: Union[str, int], expires_delta: Optional[timedelta] = None) -> str:
"""
Create a JWT token with the provided subject (typically user ID)
"""
if expires_delta:
expire = datetime.now(UTC) + expires_delta
else:
expire = datetime.now(UTC) + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode = {"exp": expire, "sub": str(subject)}
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
return encoded_jwt