Skip to content

Commit 9199933

Browse files
committed
Add jwtBearer
1 parent 6623bf7 commit 9199933

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

database/auth.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1+
import urllib.parse
2+
from database.db import DB
3+
from fastapi import Request
4+
from supabase import Client
15
from database.db import DB
6+
from fastapi import HTTPException
27
from gotrue.types import AuthResponse
38
from gotrue.types import OAuthResponse
4-
import urllib.parse
9+
from gotrue.errors import AuthApiError
10+
from fastapi.security import HTTPBearer
11+
from fastapi.security import HTTPAuthorizationCredentials
12+
13+
14+
class jwtBearer(HTTPBearer):
15+
16+
def __init__(self, auto_error: bool = True) -> None:
17+
super(jwtBearer, self).__init__(auto_error=auto_error)
18+
19+
async def __call__(self, request: Request):
20+
credentials: HTTPAuthorizationCredentials = await super(
21+
jwtBearer, self).__call__(request)
22+
if credentials:
23+
if not credentials.scheme == "Bearer":
24+
raise HTTPException(
25+
status_code=403, detail="Invalid or Expired Token!")
26+
return self.verify_jwt(credentials.credentials)
27+
else:
28+
raise HTTPException(
29+
status_code=403, detail="Invalid or Expired Token!")
30+
31+
def verify_jwt(self, jwt_token: str) -> Client:
32+
try:
33+
supabase = DB().supabase
34+
# supabase.auth.get_user(jwt_token)
35+
supabase.postgrest.auth(jwt_token)
36+
return supabase
37+
except AuthApiError as err:
38+
raise HTTPException(
39+
status_code=400,
40+
detail=str(err))
541

642

7-
# TODO: not complete, initial commit.
843
class User():
944

1045
def __init__(self) -> None:

0 commit comments

Comments
 (0)