diff --git a/src/auth/service.py b/src/auth/service.py index 047d55c..16f9c53 100644 --- a/src/auth/service.py +++ b/src/auth/service.py @@ -9,7 +9,7 @@ from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer import src.config as config from .model import UserBase - +from .util import extract_template class JWTBearer(HTTPBearer): def __init__(self, auto_error: bool = True): @@ -24,7 +24,16 @@ class JWTBearer(HTTPBearer): raise HTTPException( 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: raise HTTPException( status_code=403, detail="Invalid token or expired token." @@ -35,11 +44,11 @@ class JWTBearer(HTTPBearer): else: 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: 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}"}, ) diff --git a/src/auth/util.py b/src/auth/util.py new file mode 100644 index 0000000..1ceece4 --- /dev/null +++ b/src/auth/util.py @@ -0,0 +1,9 @@ +def extract_template(path_string, value_dict): + template = path_string + + # Replace each value in the dict with its corresponding key placeholder + for key, value in value_dict.items(): + if str(value) in template: + template = template.replace(str(value), f'<{key}>') + + return template diff --git a/src/job/router.py b/src/job/router.py index 65ceee8..da4b81e 100644 --- a/src/job/router.py +++ b/src/job/router.py @@ -17,6 +17,8 @@ async def get_activities(common: CommonParameters): # return data = await get_all(common=common) + + return StandardResponse( data=data, message="Data retrieved successfully",