diff --git a/src/auth/service.py b/src/auth/service.py index d68f639..4bf816c 100644 --- a/src/auth/service.py +++ b/src/auth/service.py @@ -82,36 +82,58 @@ async def get_token(request: Request): async def internal_key(request: Request): - api_key = request.headers.get("X-Internal-Key") + token = request.headers.get("Authorization") - if api_key != config.API_KEY: - raise HTTPException( - status_code=403, detail="Invalid Key." - ) + if not token: + api_key = request.headers.get("X-Internal-Key") - try: - headers = { - 'Content-Type': 'application/json' - } + if api_key != config.API_KEY: + raise HTTPException( + status_code=403, detail="Invalid Key." + ) + + try: + headers = { + 'Content-Type': 'application/json' + } + + response = requests.post( + f"{config.AUTH_SERVICE_API}/sign-in", + headers=headers, + data=json.dumps({ + "username": "user10", + "password": "123456" + }) + ) + + if not response.ok: + print(str(response.json())) + raise Exception("error auth") + + user_data = response.json() + return user_data['data']['access_token'] + + except Exception as e: + raise Exception(str(e)) + else: + try: + response = requests.get( + f"{config.AUTH_SERVICE_API}/verify-token", + headers={"Authorization": f"{token}"}, + ) + + if not response.ok: + raise HTTPException( + status_code=403, detail="Invalid token." + ) + + return token.split(" ")[1] + + except Exception as e: + print(f"Token verification error: {str(e)}") + return False, str(e) + - response = requests.post( - f"{config.AUTH_SERVICE_API}/sign-in", - headers=headers, - data=json.dumps({ - "username": "user10", - "password": "123456" - }) - ) - - if not response.ok: - print(str(response.json())) - raise Exception("error auth") - - user_data = response.json() - return user_data['data']['access_token'] - - except Exception as e: - raise Exception(str(e))