- Newest
- Most votes
- Most comments
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:
- CloudFront functions have limited capabilities and don't support external API calls to Okta or access to AWS Secrets Manager.
- Authentication tokens should typically be managed client-side in a SPA architecture.
Instead, consider these better approaches:
-
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.
-
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.
-
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
- Origin request policy: When implementing geolocation-based security features, ensure your origin request policies include headers like
CloudFront-Viewer-CountryandCloudFront-Viewer-Country-Regionif 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
Relevant content
asked a year ago
asked 2 years ago
asked 2 years ago
