himanshukukreja
05/15/2023, 8:59 AMrp_st
05/15/2023, 9:45 AMhimanshukukreja
05/15/2023, 9:49 AMrp_st
05/15/2023, 9:52 AMhimanshukukreja
05/15/2023, 9:57 AMrp_st
05/15/2023, 9:58 AMrp_st
05/15/2023, 9:58 AMhimanshukukreja
05/15/2023, 10:01 AMrp_st
05/15/2023, 10:06 AMrp_st
05/15/2023, 10:07 AMhimanshukukreja
05/15/2023, 10:08 AMhimanshukukreja
05/15/2023, 1:37 PMhttp://localhost:8000/auth/jwt/jwks.json
to get the rsa_key and this rsa_key is used to decode/verify the token . But somehow the /auth/jwt/jwks.json is not working . I don't know why? Is there something that I miss?himanshukukreja
05/15/2023, 1:37 PM> jwks_uri = f"{api_domain}/auth/jwt/jwks.json"
>
> def get_key(header):
> print("jwks_uri",jwks_uri)
> jwks_client = requests.get(jwks_uri).content
> jwks_client = json.loads(jwks_client)
> rsa_key = {}
> for key in jwks_client['keys']:
> if key['kid'] == header['kid']:
> rsa_key = {
> 'kty': key['kty'],
> 'kid': key['kid'],
> 'use': key['use'],
> 'n': key['n'],
> 'e': key['e']
> }
> return rsa_key
>
>
> @router.get("/microservice-auth-test")
> async def microservice_auth_test(request: Request):
> authorization: Optional[str] = request.headers.get('authorization')
> if authorization:
> parts = authorization.split()
>
> if parts[0].lower() != 'bearer':
> raise HTTPException(status_code=401, detail='Invalid token header')
> elif len(parts) == 1:
> raise HTTPException(status_code=401, detail='Token missing')
> elif len(parts) > 2:
> raise HTTPException(status_code=401, detail='Token contains spaces')
>
> jwt_token = parts[1]
> print("Received jwt token", jwt_token )
> headers = jwt.get_unverified_header(jwt_token)
> print("unverified_header ",headers)
> rsa_key = get_key(headers)
> print("rsa_key ",rsa_key)
>
> try:
> payload = jwt.decode(jwt_token, rsa_key, algorithms=['RS256'])
> user_id = payload.get('userid')
> return {"userId from auth service: ",user_id}
> except JWTError as e:
> raise HTTPException(status_code=401, detail='Invalid token') from e
> else:
> raise HTTPException(status_code=401, detail='Token missing')
rp_st
05/15/2023, 2:00 PMhimanshukukreja
05/15/2023, 3:46 PMrp_st
05/15/2023, 4:01 PM