Authoriser
The Authoriser class provides flexible authorisation rules for controlling access to your application.
Authoriser
Handles authorisation logic using composable rules
Source code in src/cognito_auth/authoriser.py
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
Functions
__init__(rules, require_all=False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
list[AuthorisationRule]
|
List of authorisation rules |
required |
require_all
|
bool
|
If True, ALL rules must pass. If False, ANY rule can pass. |
False
|
Source code in src/cognito_auth/authoriser.py
from_lists(allowed_groups=None, allowed_users=None, require_all=False)
classmethod
Create an Authoriser from simple lists of allowed values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allowed_groups
|
list[str] | None
|
List of allowed Cognito groups |
None
|
allowed_users
|
list[str] | None
|
List of allowed email addresses |
None
|
require_all
|
bool
|
If True, ALL rules must pass. If False, ANY rule passes. |
False
|
Returns:
| Type | Description |
|---|---|
Authoriser
|
Authoriser instance with the specified rules |
Source code in src/cognito_auth/authoriser.py
from_config()
classmethod
Create an Authoriser from configuration with automatic TTL caching.
Config is cached for 5 minutes to allow adding new users without restarting. Call clear_config_cache() to force immediate reload.
Requires one of these environment variables: - COGNITO_AUTH_CONFIG_PATH: Path to local JSON file (development) - COGNITO_AUTH_SECRET_NAME: AWS Secrets Manager secret name (production)
Config format (JSON): { "allowed_groups": ["developers", "admins"], "allowed_users": ["user@example.com"], "require_all": false }
Returns:
| Type | Description |
|---|---|
Authoriser
|
Authoriser instance configured from the loaded settings |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither environment variable is set or config is invalid |
Example
Development
export COGNITO_AUTH_CONFIG_PATH=./auth-config.json authoriser = Authoriser.from_config()
Production
export COGNITO_AUTH_SECRET_NAME=my-app/auth-config authoriser = Authoriser.from_config()
Source code in src/cognito_auth/authoriser.py
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 | |
clear_config_cache()
classmethod
Manually clear the config cache to force immediate reload.
Useful when you need to apply config changes immediately without waiting for the 5-minute TTL to expire.
Example
from cognito_auth import Authoriser
After updating secret in AWS
Authoriser.clear_config_cache()
Next call will fetch fresh config
guard = AuthGuard.from_config()
Source code in src/cognito_auth/authoriser.py
is_authorised(user)
Check if user is authorised
Source code in src/cognito_auth/authoriser.py
Authorisation Rules
GroupRule
GroupRule
Allow users in specific Cognito groups
Source code in src/cognito_auth/authoriser.py
Functions
__init__(allowed_groups)
is_allowed(user)
Source code in src/cognito_auth/authoriser.py
EmailRule
EmailRule
Allow specific users by email address
Source code in src/cognito_auth/authoriser.py
Functions
__init__(allowed_emails)
Examples
Basic Usage
from cognito_auth import Authoriser, User
# Allow specific groups
authoriser = Authoriser.from_lists(
allowed_groups=["developers", "admins"]
)
user = User.create_mock(groups=["developers"])
assert authoriser.is_authorised(user) is True
OR Logic (Default)
By default, user must match ANY rule:
authoriser = Authoriser.from_lists(
allowed_groups=["developers"],
allowed_users=["special@example.com"],
require_all=False # Default
)
# User passes if they're in "developers" OR have email "special@example.com"
AND Logic
Require user to match ALL rules:
authoriser = Authoriser.from_lists(
allowed_groups=["developers"],
allowed_users=["admin@example.com"],
require_all=True
)
# User must be in "developers" AND have email "admin@example.com"
Loading from Configuration
# From local file (development)
import os
os.environ["COGNITO_AUTH_CONFIG_PATH"] = "./auth-config.json"
authoriser = Authoriser.from_config()
# From AWS Secrets Manager (production)
os.environ["COGNITO_AUTH_SECRET_NAME"] = "my-app/auth-config"
authoriser = Authoriser.from_config()
Configuration File Format
Create auth-config.json:
{
"allowed_groups": ["developers", "admins", "users"],
"allowed_users": ["special-user@example.com"],
"require_all": false
}
See auth-config.example.json for a complete template.
Caching
Authorisation config loaded via from_config() is cached for 5 minutes (300 seconds). To force a reload: