- Newest
- Most votes
- Most comments
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/
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:
-
You're correct that the JWT authorizer uses the
jwks_urito validate the token's signature. Thejwks_uriis indeed typically constructed asissuer + "/.well-known/jwks.json". -
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. -
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
jwtAudienceto the User Pool Client ID is the correct approach. -
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
userPoolProviderUrlproperty 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
issuerwill be in the formatcognito-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
Relevant content
asked 4 years ago
asked 4 years ago
- AWS OFFICIALUpdated 8 months ago

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