|
| 1 | +import urllib.parse |
| 2 | +from database.db import DB |
| 3 | +from fastapi import Request |
| 4 | +from supabase import Client |
1 | 5 | from database.db import DB |
| 6 | +from fastapi import HTTPException |
2 | 7 | from gotrue.types import AuthResponse |
3 | 8 | 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)) |
5 | 41 |
|
6 | 42 |
|
7 | | -# TODO: not complete, initial commit. |
8 | 43 | class User(): |
9 | 44 |
|
10 | 45 | def __init__(self) -> None: |
|
0 commit comments