-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt.py
More file actions
66 lines (56 loc) · 2.16 KB
/
Copy pathjwt.py
File metadata and controls
66 lines (56 loc) · 2.16 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
from datetime import datetime, timedelta, timezone
from uuid import uuid4
from jose import JWTError, jwt
from src.core.config.setting import get_settings
from src.shared.exceptions.credential_exception import InvalidCredentialsError
settings = get_settings()
class JWTService:
ACCESS_TOKEN_TYPE = "access"
REFRESH_TOKEN_TYPE = "refresh"
@staticmethod
def _create_token(data: dict, token_type: str, expires_delta: timedelta) -> str:
now = datetime.now(timezone.utc)
to_encode = data.copy()
to_encode.update(
{
"iss": settings.JWT_ISSUER,
"sub": str(to_encode["sub"]),
"aud": settings.JWT_AUDIENCE,
"exp": now + expires_delta,
"nbf": now,
"iat": now,
"jti": str(uuid4()),
"token_type": token_type,
}
)
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
@staticmethod
def create_access_token(data: dict) -> str:
return JWTService._create_token(
data=data,
token_type=JWTService.ACCESS_TOKEN_TYPE,
expires_delta=timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES),
)
@staticmethod
def create_refresh_token(data: dict) -> str:
return JWTService._create_token(
data=data,
token_type=JWTService.REFRESH_TOKEN_TYPE,
expires_delta=timedelta(minutes=settings.REFRESH_TOKEN_EXPIRE_MINUTES),
)
@staticmethod
def decode_token(token: str) -> dict:
try:
return jwt.decode(
token,
settings.SECRET_KEY,
algorithms=[settings.ALGORITHM],
issuer=settings.JWT_ISSUER,
audience=settings.JWT_AUDIENCE,
)
except JWTError:
raise InvalidCredentialsError("Invalid or expired token")
@staticmethod
def require_token_type(payload: dict, expected_token_type: str) -> None:
if payload.get("token_type") != expected_token_type:
raise InvalidCredentialsError(f"{expected_token_type.title()} token required")