User
The User class represents an authenticated user from AWS Cognito via ALB OIDC headers.
User
Represents an authenticated user from AWS ALB + Cognito.
Source code in src/cognito_auth/user.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
Attributes
sub
property
User's subject identifier (unique user ID)
username
property
User's username
email
property
User's email address
email_domain
property
groups
property
User's Cognito groups
is_authenticated
property
Whether the user is authenticated
is_admin
property
Whether the user is an admin (member of gds-idea group)
email_verified
property
Whether the user's email has been verified
exp
property
Token expiration time
issuer
property
Token issuer (Cognito User Pool)
oidc_claims
property
All claims from x-amzn-oidc-data token
access_claims
property
All claims from x-amzn-oidc-accesstoken token
Functions
__init__(oidc_data_header, access_token_header, region, verify_tokens=True)
Initialize User from ALB headers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oidc_data_header
|
str | None
|
Value of x-amzn-oidc-data header |
required |
access_token_header
|
str | None
|
Value of x-amzn-oidc-accesstoken header |
required |
region
|
str
|
AWS region (e.g., 'eu-west-2') |
required |
verify_tokens
|
bool
|
Whether to verify token signatures (default: True) |
True
|
Raises:
| Type | Description |
|---|---|
MissingTokenError
|
If required headers are missing |
InvalidTokenError
|
If tokens are invalid |
ExpiredTokenError
|
If tokens have expired |
Source code in src/cognito_auth/user.py
create_mock(email=None, username=None, sub=None, groups=None, email_verified=True, region='eu-west-2', **extra_claims)
classmethod
Create a mock user for development and testing.
This method creates a User instance without requiring valid JWT tokens. It loads defaults from dev-mock-user.json if present, and falls back to sensible defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
email
|
str | None
|
User's email address |
None
|
username
|
str | None
|
User's username |
None
|
sub
|
str | None
|
User's subject identifier (unique ID) |
None
|
groups
|
list[str] | None
|
List of Cognito groups |
None
|
email_verified
|
bool
|
Whether email is verified |
True
|
region
|
str
|
AWS region |
'eu-west-2'
|
**extra_claims
|
Any
|
Additional claims to include in tokens |
{}
|
Returns:
| Type | Description |
|---|---|
User
|
User instance with mock data |
Example
user = User.create_mock(email="dev@company.com", groups=["admin"]) user = User.create_mock() # Uses defaults from JSON or hardcoded
Source code in src/cognito_auth/user.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
Examples
Creating a User from Headers
In production, the User is automatically created by framework auth classes:
from cognito_auth.streamlit import StreamlitAuth
auth = StreamlitAuth()
user = auth.get_auth_user() # User created from request headers
print(f"Email: {user.email}")
print(f"Groups: {user.groups}")
print(f"Is Admin: {user.is_admin}")
Creating a Mock User for Testing
For local development and testing:
from cognito_auth import User
# With defaults
user = User.create_mock()
# With custom values
user = User.create_mock(
email="developer@example.com",
groups=["developers", "admin"]
)
assert user.is_authenticated is True
assert "developers" in user.groups
Properties
All user properties are read-only and extracted from the JWT tokens.
Authentication Properties
is_authenticated: Whether the user is authenticated (tokens are valid)email_verified: Whether the user's email is verified in Cognito
Identity Properties
sub: User's unique subject identifier (UUID)username: User's username (typically same assub)email: User's email addressemail_domain: Domain portion of email (e.g., "example.com")
Authorisation Properties
groups: List of Cognito groups the user belongs tois_admin: Whether user belongs to "gds-idea" admin group
Token Properties
exp: Token expiration timestampissuer: Token issuer URLoidc_claims: All claims from ALB OIDC tokenaccess_claims: All claims from Cognito access token