Skip to content

Best practice for getting okta token for API configured as an origin in CloudFront

0

We currently have a React SPA hosted in CloudFront/S3 that makes a call back to a Private Rest API setup as a second origin. We will be protecting the API using a Lambda Authorizer. This would require the need for the React app to retrieve a token from okta and have that token validated before access to the API is granted.

Based on the limited information I have found it seems that the best and most secure approach is to write a CloudFront function that is triggered on the origin request to handle acquiring the token and inserting that into the header on the call back to the API. This function would need to retrieve client id and secret stored in the secrets manager.

Am I on the right track here or is there a better way?

1 Answer
0

Based on your scenario with a React SPA in CloudFront/S3 and a private REST API as a second origin, there are a few approaches to consider, but your proposed solution has some challenges.

Using a CloudFront function to acquire an Okta token on the origin request isn't the recommended approach for several reasons:

  1. CloudFront functions have limited capabilities and don't support external API calls to Okta or access to AWS Secrets Manager.
  2. Authentication tokens should typically be managed client-side in a SPA architecture.

Instead, consider these better approaches:

  1. Client-side token acquisition: Have your React application handle the Okta authentication flow directly. The SPA would obtain the token from Okta and include it in API requests. The Lambda Authorizer would then validate this token.

  2. API Gateway with Lambda Authorizer: Configure your Lambda Authorizer to validate the Okta token sent by the client. This is a standard pattern for securing APIs with OAuth/OIDC tokens.

  3. Custom header validation: If you want to ensure requests only come through your CloudFront distribution, you can:

  • Configure CloudFront to insert a custom header with a shared secret before forwarding requests to API Gateway
  • Have your Lambda Authorizer validate both this secret header AND the Okta token
  • This provides dual validation that the request came through your authorized CloudFront distribution and from an authenticated user
  1. Origin request policy: When implementing geolocation-based security features, ensure your origin request policies include headers like CloudFront-Viewer-Country and CloudFront-Viewer-Country-Region if needed for additional security context.

The approach combining client-side token acquisition with a custom CloudFront header for origin verification provides a good balance of security while maintaining the proper separation of concerns in your architecture.
Sources
Restrict API Gateway HTTP API access through CloudFront | AWS re:Post
Revise origin request policies - Secure Media Delivery at the Edge on AWS
Step 3. Prepare your CloudFront distributions - Secure Media Delivery at the Edge on AWS

answered a year ago

AWS
SUPPORT ENGINEER

reviewed a year ago

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.