This is a memo when verifying the AWS Cognito web token.
[JSON Web Token Verification -Amazon Cognito](https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt. html)
Cognito allows you to download a JWK set for verification for each user pool
https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json
jwt jwt/ruby-jwt: A ruby implementation of the RFC 7519 OAuth JSON Web Token (JWT) standard.
Used to decode regular JWT tokens
json-jwt
Used to create a public key from a public JSON web key (JWK)
#Download JWK set
uri = "https://cognito-idp.ap-northeast-1.amazonaws.com/example/.well-known/jwks.json"
response = Net::HTTP.get_response(URI.parse(uri))
jwks = JSON.parse(response.body)
#Decode JWT without verification
token = JWT.decode(jwt, nil, false)
#Get the kid match from the JWK set
jwk = jwks["keys"].find { |obj| obj["kid"] == token[1]["kid"] }
#Create public key
public_key = JSON::JWK.new(jwk).to_key
#Exception when decoding verification using public key is invalid
JSON::JWT.decode(jwt, public_key)
If you find it helpful, please LGTM
Recommended Posts