-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjwtvalidate.py
More file actions
28 lines (21 loc) · 997 Bytes
/
Copy pathjwtvalidate.py
File metadata and controls
28 lines (21 loc) · 997 Bytes
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
from fastapi import Request, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from jwtsign import decode
class Bearer(HTTPBearer):
def __init__(self, auto_error: bool = True):
super().__init__(auto_error=auto_error)
def validate(self, jwtoken: str):
try:
payload = decode(jwtoken)
return True
except:
return False
async def __call__(self, request: Request):
credentials: HTTPAuthorizationCredentials = await super().__call__(request)
if not credentials:
raise HTTPException(status_code=403, detail="Invalid authorization code.")
if credentials.scheme != "Bearer":
raise HTTPException(status_code=403, detail="Invalid authentication scheme.")
if not self.validate(credentials.credentials):
raise HTTPException(status_code=403, detail="Invalid token or expired token.")
return credentials.credentials