Spaces:
Sleeping
Sleeping
File size: 1,000 Bytes
b4c9cb7 |
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 |
from typing import AnyStr, Dict, Union
import os
from fastapi import HTTPException, status
from jose import jwt, JWTError
class JWTProvider:
"""
Perform JWT Encryption and Decryption
"""
def __init__(
self, secret: AnyStr = os.environ.get("JWT_SECRET"), algorithm: AnyStr = "HS256"
):
self.secret = secret
self.algorithm = algorithm
def encrypt(self, data: Dict) -> AnyStr:
"""
Encrypt the data with JWT
"""
return jwt.encode(data, self.secret, algorithm=self.algorithm)
def decrypt(self, token: AnyStr) -> Union[Dict, None]:
"""
Decrypt the token with JWT
"""
try:
return jwt.decode(token, self.secret, algorithms=[self.algorithm])
except JWTError as e:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=f"Could not validate credentials. {str(e)}",
)
jwt_provider = JWTProvider()
|