Authentication Strategies¶
The authentication module provides pluggable authentication strategies for the WebApp construct.
Overview¶
Authentication is implemented allowing you to choose between different authentication methods without changing your application code.
Available Strategies¶
AuthType.NONE- No authentication (public access)AuthType.COGNITO- AWS Cognito authentication with OAuth2 (managed login UI)AuthType.INTERNAL_ACCESS- AWS Cognito authentication with external IdP (e.g., EntraID)
AuthType¶
gds_idea_cdk_constructs.web_app._auth_strategies.AuthType
¶
Bases: StrEnum
Defines the supported authentication types for the WebApp construct.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
IAuthStrategy (Interface)¶
gds_idea_cdk_constructs.web_app._auth_strategies.IAuthStrategy
¶
Bases: ABC
Interface for an authentication strategy.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
create_listener_action
abstractmethod
¶
Return the ALB listener action for this strategy.
create_outputs
abstractmethod
¶
get_minimal_role
abstractmethod
¶
Creates a minimal IAM role configured with permissions required by this strategy.
configure_role_permissions
abstractmethod
¶
Grants an existing role the permissions required by this strategy.
get_environment_variables
abstractmethod
¶
NoAuthStrategy¶
gds_idea_cdk_constructs.web_app._auth_strategies.NoAuthStrategy
¶
Bases: IAuthStrategy
A strategy for apps with no authentication.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
BaseCognitoAuthStrategy¶
gds_idea_cdk_constructs.web_app._auth_strategies.BaseCognitoAuthStrategy
¶
Bases: IAuthStrategy
Base class for Cognito-based authentication strategies.
Provides common setup for User Pool, Domain, and Client creation. Subclasses override _create_user_pool_client() to customize client configuration.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
create_listener_action
¶
Returns the Cognito authentication action for the ALB listener.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
create_outputs
¶
Creates the Cognito Client ID CloudFormation output.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
get_minimal_role
¶
Creates a minimal role with Cognito secret read access.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
configure_role_permissions
¶
get_environment_variables
¶
CognitoManagedLoginAuthStrategy¶
gds_idea_cdk_constructs.web_app._auth_strategies.CognitoManagedLoginAuthStrategy
¶
Bases: BaseCognitoAuthStrategy
A strategy for apps using Cognito authentication with managed login UI.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
CognitoExternalIdpAuthStrategy¶
gds_idea_cdk_constructs.web_app._auth_strategies.CognitoExternalIdpAuthStrategy
¶
Bases: BaseCognitoAuthStrategy
A strategy for apps using Cognito with an external identity provider.
This strategy configures the User Pool Client to use an external IdP (e.g., EntraID, Okta) instead of Cognito's managed login UI.
Source code in src/gds_idea_cdk_constructs/web_app/_auth_strategies.py
Usage Examples¶
No Authentication (Public Access)¶
from gds_idea_cdk_constructs.web_app import WebApp, AuthType
WebApp(
app,
deployment_config=deployment_config,
app_config=app_config,
authentication=AuthType.NONE, # Public access
)
Use cases: - Public dashboards - Open APIs - Status pages - Documentation sites
Behavior: - No authentication required - Direct access to application - Minimal IAM permissions - No environment variables added to container
Cognito Authentication¶
from gds_idea_cdk_constructs.web_app import WebApp, AuthType
WebApp(
app,
deployment_config=deployment_config,
app_config=app_config,
authentication=AuthType.COGNITO, # Requires login
)
Use cases: - Internal tools and dashboards - Applications requiring user identity - Protected data visualization - Admin panels
Behavior: - Users must authenticate via Cognito - ALB performs authentication before forwarding requests - OAuth2 authorization code flow - Session cookies for authenticated users - Automatic redirect to Cognito login page
What gets created: - Cognito User Pool Client (OAuth2 client) - Secrets Manager secret for client credentials - ALB listener rule with authentication action - IAM permissions for secret access
Environment variables added to container:
Authentication Flow (Cognito)¶
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ Browser │ │ ALB │ │ Cognito │ │ ECS │
└────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘
│ │ │ │
│ 1. GET / │ │ │
├──────────────────>│ │ │
│ │ │ │
│ 2. No auth cookie, redirect to Cognito │
│<──────────────────┤ │ │
│ │ │ │
│ 3. Login page │ │ │
├───────────────────────────────────────>│ │
│ │ │ │
│ 4. User logs in │ │ │
├───────────────────────────────────────>│ │
│ │ │ │
│ 5. OAuth callback with code │ │
│<───────────────────────────────────────┤ │
│ │ │ │
│ 6. Exchange code for tokens │ │
├──────────────────>├───────────────────>│ │
│ │ │ │
│ 7. Set auth cookie & forward request │ │
├──────────────────>├───────────────────────────────────────>│
│ │ │ │
│ 8. Response │ │ │
│<──────────────────┴───────────────────────────────────────┤
Accessing User Information (Cognito)¶
Please see our repo https://github.com/co-cddo/gds-idea-app-auth which automatically validates and verifies tokens to provide you with a user object containing user details.
Security Considerations¶
NoAuth¶
- ⚠️ No access control - Anyone can access your application
- ✅ Use for truly public content only
- ✅ Consider WAF rules for rate limiting
- ✅ Ensure application doesn't expose sensitive data
Cognito¶
- ✅ OAuth2 standard - Industry-standard authentication
- ✅ Session management - ALB handles session cookies
- ✅ User pool integration - Leverages existing user directory