|
|
|
@ -9,7 +9,7 @@ from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
|
|
import src.config as config
|
|
|
|
import src.config as config
|
|
|
|
|
|
|
|
|
|
|
|
from .model import UserBase
|
|
|
|
from .model import UserBase
|
|
|
|
|
|
|
|
from .util import extract_template
|
|
|
|
|
|
|
|
|
|
|
|
class JWTBearer(HTTPBearer):
|
|
|
|
class JWTBearer(HTTPBearer):
|
|
|
|
def __init__(self, auto_error: bool = True):
|
|
|
|
def __init__(self, auto_error: bool = True):
|
|
|
|
@ -24,7 +24,16 @@ class JWTBearer(HTTPBearer):
|
|
|
|
raise HTTPException(
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=403, detail="Invalid authentication scheme."
|
|
|
|
status_code=403, detail="Invalid authentication scheme."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
user_info = self.verify_jwt(credentials.credentials)
|
|
|
|
method = request.method
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if method == "OPTIONS":
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path = extract_template(request.url.path, request.path_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
endpoint = f"/optimumoh/{path}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user_info = self.verify_jwt(credentials.credentials, method, endpoint)
|
|
|
|
if not user_info:
|
|
|
|
if not user_info:
|
|
|
|
raise HTTPException(
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=403, detail="Invalid token or expired token."
|
|
|
|
status_code=403, detail="Invalid token or expired token."
|
|
|
|
@ -35,11 +44,11 @@ class JWTBearer(HTTPBearer):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise HTTPException(status_code=403, detail="Invalid authorization code.")
|
|
|
|
raise HTTPException(status_code=403, detail="Invalid authorization code.")
|
|
|
|
|
|
|
|
|
|
|
|
def verify_jwt(self, jwtoken: str) -> Optional[UserBase]:
|
|
|
|
def verify_jwt(self, jwtoken: str, method: str, endpoint: str) -> Optional[UserBase]:
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = requests.get(
|
|
|
|
response = requests.get(
|
|
|
|
f"{config.AUTH_SERVICE_API}/verify-token?url=http://localhost:8000",
|
|
|
|
f"{config.AUTH_SERVICE_API}/verify-token",
|
|
|
|
headers={"Authorization": f"Bearer {jwtoken}"},
|
|
|
|
headers={"Authorization": f"Bearer {jwtoken}"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|