Skip to content

feat(auth): Implement secure httpOnly cookie authentication for Okta#1920

Open
arjunp99 wants to merge 3 commits intodata-dot-all:mainfrom
arjunp99:feature/secure-httponly-cookie-auth
Open

feat(auth): Implement secure httpOnly cookie authentication for Okta#1920
arjunp99 wants to merge 3 commits intodata-dot-all:mainfrom
arjunp99:feature/secure-httponly-cookie-auth

Conversation

@arjunp99
Copy link
Contributor

@arjunp99 arjunp99 commented Mar 3, 2026

Summary

Replace localStorage token storage with httpOnly cookies to prevent XSS attacks for Okta authentication. This implements a custom PKCE flow while maintaining existing Cognito/Amplify behavior unchanged.

Security Improvements

  • Tokens stored in httpOnly cookies (not accessible via JavaScript - prevents XSS token theft)
  • SameSite=Lax prevents CSRF while allowing OAuth redirects from Okta
  • Secure flag ensures HTTPS-only transmission

Changes

Frontend

  • frontend/src/utils/pkce.js - PKCE utility for secure OAuth code exchange
  • frontend/src/authentication/views/Callback.js - OAuth callback handler
  • frontend/src/authentication/contexts/GenericAuthContext.js - Cookie-based auth for Okta
  • frontend/src/services/hooks/useClient.js - Relative URLs + credentials for cookies
  • frontend/src/routes.js - Added /callback route

Backend

  • backend/auth_handler.py - Token exchange, userinfo, logout endpoints
  • deploy/stacks/lambda_api.py - Auth handler Lambda + API routes
  • deploy/stacks/cloudfront.py - Proxy /auth/, /graphql/, /search/* to API Gateway
  • deploy/custom_resources/custom_authorizer/custom_authorizer_lambda.py - Read tokens from Cookie header

How It Works

  1. User clicks login → redirected to Okta with PKCE challenge
  2. Okta redirects back to /callback with authorization code
  3. Frontend calls /auth/token-exchange with code + PKCE verifier
  4. Backend exchanges code for tokens, sets httpOnly cookies
  5. All subsequent API calls include cookies automatically (same-origin via CloudFront proxy)
  6. Custom authorizer reads access_token from Cookie header

Backward Compatibility

  • Cognito users: No changes - continues using Amplify with Authorization header

arjunp99 added 3 commits March 3, 2026 11:00
Replace localStorage token storage with httpOnly cookies to prevent XSS
attacks. Implements custom PKCE flow for Okta authentication while
maintaining existing Cognito/Amplify behavior unchanged.

Changes:
- Add PKCE utility for secure OAuth code exchange
- Add Callback view for handling OAuth redirects
- Add backend auth_handler for token exchange endpoints
- Update GenericAuthContext with cookie-based auth for Okta
- Update useClient to work without Authorization header for Okta
- Configure CloudFront to proxy /auth/*, /graphql/*, /search/* paths
- Update Lambda API with auth endpoints and CORS for cookies
- Update custom authorizer to read tokens from Cookie header

Security improvements:
- Tokens stored in httpOnly cookies (not accessible via JavaScript)
- SameSite=Lax prevents CSRF while allowing OAuth redirects
- Secure flag ensures HTTPS-only transmission
Security improvements:
- Add structured logging with sanitized error messages
- Remove hardcoded CloudFront URL fallback (requires proper config)
- Move SimpleCookie import to module level for better performance

Frontend enhancements:
- Add 30-second timeout to token exchange requests
- Fix useEffect dependency array in useClient hook
- Implement OAuth callback handler with PKCE validation

Infrastructure updates:
- Configure auth handler Lambda for cookie-based authentication
- Add API Gateway routes for token exchange, logout, and userinfo
- Improve CloudFront URL parsing documentation

All changes pass Ruff linting and formatting checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant