Cizz22 3 months ago
parent 07c3491573
commit 67abe04ab1

@ -82,36 +82,58 @@ async def get_token(request: Request):
async def internal_key(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: if not token:
raise HTTPException( api_key = request.headers.get("X-Internal-Key")
status_code=403, detail="Invalid Key."
) if api_key != config.API_KEY:
raise HTTPException(
try: status_code=403, detail="Invalid Key."
headers = { )
'Content-Type': 'application/json'
} try:
headers = {
response = requests.post( 'Content-Type': 'application/json'
f"{config.AUTH_SERVICE_API}/sign-in", }
headers=headers,
data=json.dumps({ response = requests.post(
"username": "user10", f"{config.AUTH_SERVICE_API}/sign-in",
"password": "123456" headers=headers,
}) data=json.dumps({
) "username": "user10",
"password": "123456"
if not response.ok: })
print(str(response.json())) )
raise Exception("error auth")
if not response.ok:
user_data = response.json() print(str(response.json()))
return user_data['data']['access_token'] raise Exception("error auth")
except Exception as e: user_data = response.json()
raise Exception(str(e)) 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)

Loading…
Cancel
Save