voice state - also there was no git history here so im just force pushing
This commit is contained in:
commit
9c41144985
9 changed files with 207 additions and 0 deletions
45
cef_3M/auth.py
Normal file
45
cef_3M/auth.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import time
|
||||
import jwt
|
||||
from fastapi.security import HTTPBearer
|
||||
|
||||
import config
|
||||
from fastapi import Request, HTTPException
|
||||
|
||||
JWT_PUBKEY = open(config.SECRETKEY).read()
|
||||
JWT_ALGORITHM = "RS256"
|
||||
|
||||
|
||||
def decodeJWT(token: str) -> dict:
|
||||
try:
|
||||
decoded_token = jwt.decode(token, JWT_PUBKEY, algorithms=[JWT_ALGORITHM])
|
||||
return decoded_token if decoded_token["exp"] >= time.time() else None
|
||||
except:
|
||||
return {}
|
||||
|
||||
|
||||
class JWTBearer(HTTPBearer):
|
||||
def __init__(self, auto_error: bool = True):
|
||||
super(JWTBearer, self).__init__(auto_error=auto_error)
|
||||
|
||||
async def __call__(self, request: Request):
|
||||
credentials = await super(JWTBearer, self).__call__(request)
|
||||
if credentials:
|
||||
if not credentials.scheme == "Bearer":
|
||||
raise HTTPException(status_code=403, detail="Invalid authentication scheme.")
|
||||
if not self.verify_jwt(credentials.credentials):
|
||||
raise HTTPException(status_code=403, detail="Invalid or expired token.")
|
||||
request.state.jwt = decodeJWT(credentials.credentials)
|
||||
return credentials.credentials
|
||||
else:
|
||||
raise HTTPException(status_code=403, detail="Invalid authorization code.")
|
||||
|
||||
def verify_jwt(self, jwtoken: str) -> bool:
|
||||
isTokenValid: bool = False
|
||||
|
||||
try:
|
||||
payload = decodeJWT(jwtoken)
|
||||
except:
|
||||
payload = None
|
||||
if payload:
|
||||
isTokenValid = True
|
||||
return isTokenValid
|
||||
Loading…
Add table
Add a link
Reference in a new issue