Skip to content

Using HttpJwtAuthorizer attached to a UserPool using CDK

0

If I use HttpJwtAuthorizer step 3 tries to validate the token's siganture using the jwks_uri. The docs don't explain where that comes from, but I think it attempts to use

issuer + "/.well-known/jwks.json"

In CDK land, you have to explicitly tell it who the issuer is:

// the issuer is like: https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxtttttz
const issuer = `https://cognito-idp.${env.region}/${userPoolId}`
return new HttpJwtAuthorizer("rocnrebel-api-authorizer",
    issuer,  
   { jwtAudience:  userPoolClientId   }
);

Cognito access tokens don't include an 'aud' but they do include 'client_id' so that's the reason for the jwtAudience being set that way.

Do I have all of this right? And is there a way to get that issuer from the cdk UserPool object without me having to piece together a string like that? The bot suggested userPool.userPoolProviderUrl but I don't see any such property.

Are there any other downsides to using the JWT authorizer with Cognito that I should know about?

How is token revocation supported? Can I access something like https://your-domain.auth.us-east-1.amazoncognito.com/oauth2/introspect in my lambda to check the status of a token? I feel like the online help in the console suggests that should work, but when I POST to it, it always returns 405 method not allowed.

Update: /oauth2/userInfo does work, and it says it will return an error if the access token you pass it is expired, revoked, malformed, or it's invalid. DOCS

2 Answers
0
Accepted Answer

Hello,

Thank you for writing on re:Post.

From the case description, I understand that you have some queries related to HttpJwtAuthorizer attached to a UserPool using CDK.

As you mentioned that you are using JWT for authorization which means that you would actually want to use the HttpJwtAuthorizer construct [1]. This construct also authorizes requests based on whether or not requesters are registered with a Cognito UserPool, while using JWT specifically.

For example, the HttpJwtAuthorizer construct could be defined like this:

const issuer = 'https://test.us.auth0.com';
const authorizer = new HttpJwtAuthorizer('TestAuthorizer', issuer, {
  jwtAudience: ['<app_client_id_of_userpool>'],
  identitySource: ['$request.header.Authorization']
});

There is also a public blog post, where you can find how to secure API Gateway HTTP endpoints with JWT authorizer in detail with steps[2]

Do I have all of this right? And is there a way to get that issuer from the cdk UserPool object without me having to piece together a string like that? The bot suggested userPool.userPoolProviderUrl but I don't see any such property.

You are right that there is no such property 'userPoolProviderUrl' as of now in this construct. Therefore, one way is to construct the URL manually and other way is that you can use L1 constructs like cfnUserPool[3]. However, issuer URl you still need to create.

You can use JSON Web Tokens (JWTs) as a part of OpenID Connect (OIDC) and OAuth 2.0 frameworks to restrict client access to your APIs. Enabling Token Revocation[4]: In CDK, you can enable token revocation when creating a User Pool Client[5].

Reasons for returns 405 method:

i. Check if there is any WAF blocking the request
ii. You can check the HAR file also
iii. Please make sure that header is correct
iv. In case you are using Lambda function can check the associate CloudWatch logs

Kindly try the above suggestions. Hope they will improve your experience. However, if the issue persists and you would like us to investigate the issue further then please create a technical support case with Amazon CDK team or AWS Cognito team.

In order to open a Technical Support Case, you will need to have a Developer, Business or Enterprise Support Plan. If you do not have a Support Plan in place, please kindly visit the link provided below on how to sign up for one[6]

References:

[1] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigatewayv2_authorizers.HttpJwtAuthorizer.html
[2] https://aws.amazon.com/blogs/security/how-to-secure-api-gateway-http-endpoints-with-jwt-authorizer/
[3] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cognito.CfnUserPool.html
[4] https://repost.aws/knowledge-center/cognito-revoke-refresh-tokens
[5] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cognito.UserPoolClient.html
[6] https://aws.amazon.com/premiumsupport/knowledge-center/sign-up-support/

AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

-1

You're on the right track with your understanding of the HttpJwtAuthorizer and its integration with Amazon Cognito User Pools. Let's break down your points and provide some clarifications:

  1. You're correct that the JWT authorizer uses the jwks_uri to validate the token's signature. The jwks_uri is indeed typically constructed as issuer + "/.well-known/jwks.json".

  2. Your approach to constructing the issuer URL for a Cognito User Pool is correct. The format you've used (https://cognito-idp.${env.region}.amazonaws.com/${userPoolId}) is the standard format for Cognito User Pool issuers.

  3. You're right about Cognito access tokens not including an 'aud' claim but including a 'client_id' claim. The JWT authorizer in API Gateway will check for 'aud' first, and if not present, it will fall back to checking the 'client_id' claim. So setting jwtAudience to the User Pool Client ID is the correct approach.

  4. Regarding getting the issuer from the CDK UserPool object, unfortunately, there isn't a direct method to retrieve the fully constructed issuer URL. However, you can use the userPoolProviderUrl property of the UserPool object to get close. Here's how you could do it:

    const userPool = new cognito.UserPool(this, 'MyUserPool', {
      // ... other configuration ...
    });
    
    const issuer = userPool.userPoolProviderUrl;

    This issuer will be in the format cognito-idp.${region}.amazonaws.com/${userPoolId}, so you'd just need to prepend 'https://' to use it with the HttpJwtAuthorizer.

In summary, your understanding and implementation are correct. The only potential improvement would be using the userPoolProviderUrl property to get most of the issuer URL, which can help reduce the chance of errors when manually constructing the string.
Sources
aws-cdk-lib.aws_apigatewayv2_authorizers module · AWS CDK
Control access to HTTP APIs with JWT authorizers in API Gateway - Amazon API Gateway
interface JWTConfigurationProperty · AWS CDK

answered 2 years ago

  • There doesn't seem to actually be a property userPoolProviderUrl in a user pool.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.