intflow-authctl 0.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. intflow_auth/__init__.py +3 -0
  2. intflow_auth/admin_auth.py +512 -0
  3. intflow_auth/api/__init__.py +1 -0
  4. intflow_auth/api/admin_app_roles.py +197 -0
  5. intflow_auth/api/admin_audit.py +77 -0
  6. intflow_auth/api/admin_clients.py +346 -0
  7. intflow_auth/api/admin_operator_tokens.py +357 -0
  8. intflow_auth/api/admin_roles.py +232 -0
  9. intflow_auth/api/admin_users.py +243 -0
  10. intflow_auth/api/cli_login.py +341 -0
  11. intflow_auth/api/dependencies.py +77 -0
  12. intflow_auth/api/health.py +25 -0
  13. intflow_auth/api/login.py +495 -0
  14. intflow_auth/api/oauth.py +595 -0
  15. intflow_auth/api/oidc.py +137 -0
  16. intflow_auth/api/pagination.py +19 -0
  17. intflow_auth/api/rate_limits.py +57 -0
  18. intflow_auth/api/router.py +30 -0
  19. intflow_auth/api/session_management.py +268 -0
  20. intflow_auth/app.py +183 -0
  21. intflow_auth/app_roles.py +531 -0
  22. intflow_auth/audit.py +370 -0
  23. intflow_auth/authctl_cli/__init__.py +1 -0
  24. intflow_auth/authctl_cli/api_client.py +114 -0
  25. intflow_auth/authctl_cli/commands.py +578 -0
  26. intflow_auth/authctl_cli/config.py +151 -0
  27. intflow_auth/authctl_cli/constants.py +7 -0
  28. intflow_auth/authctl_cli/errors.py +5 -0
  29. intflow_auth/authctl_cli/output.py +152 -0
  30. intflow_auth/authctl_cli/parser.py +633 -0
  31. intflow_auth/authctl_cli/validation.py +70 -0
  32. intflow_auth/authctl_credentials.py +57 -0
  33. intflow_auth/authorization_codes.py +531 -0
  34. intflow_auth/bootstrap.py +297 -0
  35. intflow_auth/cli.py +213 -0
  36. intflow_auth/cli_login_service.py +483 -0
  37. intflow_auth/cli_login_transactions.py +621 -0
  38. intflow_auth/config.py +467 -0
  39. intflow_auth/context.py +43 -0
  40. intflow_auth/csrf.py +59 -0
  41. intflow_auth/database.py +101 -0
  42. intflow_auth/errors.py +61 -0
  43. intflow_auth/google_oauth.py +600 -0
  44. intflow_auth/logging.py +246 -0
  45. intflow_auth/login_transactions.py +481 -0
  46. intflow_auth/middleware.py +24 -0
  47. intflow_auth/oauth_clients.py +640 -0
  48. intflow_auth/oauth_service.py +681 -0
  49. intflow_auth/operator_admin_tokens.py +558 -0
  50. intflow_auth/pagination.py +102 -0
  51. intflow_auth/py.typed +1 -0
  52. intflow_auth/rate_limits.py +114 -0
  53. intflow_auth/readiness.py +240 -0
  54. intflow_auth/refresh_tokens.py +559 -0
  55. intflow_auth/roles.py +290 -0
  56. intflow_auth/secret_resolution.py +339 -0
  57. intflow_auth/sessions.py +525 -0
  58. intflow_auth/signing_keys.py +298 -0
  59. intflow_auth/store_resolver.py +286 -0
  60. intflow_auth/tokens.py +78 -0
  61. intflow_auth/usage_metadata.py +15 -0
  62. intflow_auth/users.py +592 -0
  63. intflow_authctl-0.1.8.dist-info/METADATA +56 -0
  64. intflow_authctl-0.1.8.dist-info/RECORD +66 -0
  65. intflow_authctl-0.1.8.dist-info/WHEEL +4 -0
  66. intflow_authctl-0.1.8.dist-info/entry_points.txt +4 -0
@@ -0,0 +1,77 @@
1
+ """Admin audit event search endpoint."""
2
+
3
+ from datetime import datetime
4
+ from typing import Any
5
+
6
+ from fastapi import APIRouter, HTTPException, Request, status
7
+
8
+ from intflow_auth.admin_auth import require_admin_actor
9
+ from intflow_auth.api.dependencies import (
10
+ get_audit_writer,
11
+ get_request_settings,
12
+ )
13
+ from intflow_auth.audit import AuditRecord, AuditSearchFilters
14
+
15
+ MAX_AUDIT_SEARCH_LIMIT = 500
16
+ ADMIN_AUDIT_READ_SCOPE = "admin:audit:read"
17
+
18
+ router = APIRouter(tags=["Admin Audit"])
19
+
20
+
21
+ @router.get("/v1/admin/audit-events")
22
+ async def search_audit_events(
23
+ request: Request,
24
+ actor_user_id: str | None = None,
25
+ target_user_id: str | None = None,
26
+ user_id: str | None = None,
27
+ client_id: str | None = None,
28
+ event_type: str | None = None,
29
+ correlation_id: str | None = None,
30
+ occurred_after: datetime | None = None,
31
+ occurred_before: datetime | None = None,
32
+ limit: int = 100,
33
+ ) -> dict[str, object]:
34
+ """Search sanitized audit events."""
35
+ settings = get_request_settings(request)
36
+ await require_admin_actor(request, required_scopes=(ADMIN_AUDIT_READ_SCOPE,))
37
+ if limit < 1 or limit > MAX_AUDIT_SEARCH_LIMIT:
38
+ raise HTTPException(
39
+ status_code=status.HTTP_400_BAD_REQUEST,
40
+ detail="Audit search limit must be between 1 and 500.",
41
+ )
42
+
43
+ records = await get_audit_writer(request, settings).search(
44
+ AuditSearchFilters(
45
+ actor_user_id=_blank_to_none(actor_user_id),
46
+ target_user_id=_blank_to_none(target_user_id),
47
+ user_id=_blank_to_none(user_id),
48
+ client_id=_blank_to_none(client_id),
49
+ event_type=_blank_to_none(event_type),
50
+ request_id=_blank_to_none(correlation_id),
51
+ occurred_after=occurred_after,
52
+ occurred_before=occurred_before,
53
+ limit=limit,
54
+ )
55
+ )
56
+ return {"events": [_audit_event_response(record) for record in records]}
57
+
58
+
59
+ def _audit_event_response(record: AuditRecord) -> dict[str, Any]:
60
+ return {
61
+ "id": record.id,
62
+ "event_type": record.event_type,
63
+ "result": record.result,
64
+ "actor_user_id": record.actor_user_id,
65
+ "target_user_id": record.target_user_id,
66
+ "client_id": record.client_id,
67
+ "correlation_id": record.request_id,
68
+ "occurred_at": record.occurred_at,
69
+ "payload": record.payload,
70
+ }
71
+
72
+
73
+ def _blank_to_none(value: str | None) -> str | None:
74
+ if value is None:
75
+ return None
76
+ stripped = value.strip()
77
+ return stripped or None
@@ -0,0 +1,346 @@
1
+ """Admin OAuth client management endpoints."""
2
+
3
+ from typing import Any
4
+
5
+ from fastapi import APIRouter, HTTPException, Request
6
+ from fastapi import status as http_status
7
+ from pydantic import BaseModel
8
+
9
+ from intflow_auth.admin_auth import AdminActorContext, require_admin_actor
10
+ from intflow_auth.api.dependencies import (
11
+ client_ip,
12
+ get_audit_writer,
13
+ get_oauth_client_store,
14
+ get_request_settings,
15
+ require_admin_actor_csrf,
16
+ )
17
+ from intflow_auth.api.pagination import admin_page_request
18
+ from intflow_auth.audit import (
19
+ AuditEventInput,
20
+ AuditEventType,
21
+ AuditResult,
22
+ write_audit_event,
23
+ )
24
+ from intflow_auth.config import Settings
25
+ from intflow_auth.oauth_clients import (
26
+ CreatedOAuthClient,
27
+ OAuthClientRecord,
28
+ OAuthClientStore,
29
+ OAuthClientType,
30
+ OAuthClientValidationError,
31
+ create_oauth_client_record,
32
+ disable_oauth_client,
33
+ rotate_oauth_client_secret,
34
+ update_oauth_client_record,
35
+ utc_now,
36
+ validate_oauth_scopes,
37
+ validate_redirect_uris,
38
+ )
39
+ from intflow_auth.pagination import PaginationError
40
+
41
+ AUTHORIZATION_CODE_GRANT = "authorization_code"
42
+ REFRESH_TOKEN_GRANT = "refresh_token"
43
+ OFFLINE_ACCESS_SCOPE = "offline_access"
44
+ ADMIN_CLIENTS_READ_SCOPE = "admin:clients:read"
45
+ ADMIN_CLIENTS_WRITE_SCOPE = "admin:clients:write"
46
+
47
+ router = APIRouter(tags=["Admin Clients"])
48
+
49
+
50
+ class OAuthClientCreateBody(BaseModel):
51
+ """Admin OAuth client creation body."""
52
+
53
+ name: str
54
+ client_type: OAuthClientType
55
+ redirect_uris: list[str]
56
+ allowed_scopes: list[str]
57
+ admin_access_required: bool = False
58
+
59
+
60
+ class OAuthClientUpdateBody(BaseModel):
61
+ """Admin OAuth client update body."""
62
+
63
+ name: str | None = None
64
+ redirect_uris: list[str] | None = None
65
+ allowed_scopes: list[str] | None = None
66
+ admin_access_required: bool | None = None
67
+ enabled: bool | None = None
68
+
69
+
70
+ @router.get("/v1/admin/clients")
71
+ async def list_oauth_clients(
72
+ request: Request,
73
+ name: str | None = None,
74
+ query: str | None = None,
75
+ enabled: bool | None = None,
76
+ limit: int | None = None,
77
+ cursor: str | None = None,
78
+ ) -> dict[str, object]:
79
+ """List OAuth clients."""
80
+ settings = get_request_settings(request)
81
+ await require_admin_actor(request, required_scopes=(ADMIN_CLIENTS_READ_SCOPE,))
82
+ page_request = admin_page_request(limit=limit, cursor=cursor)
83
+ try:
84
+ page = await get_oauth_client_store(request, settings).list_clients_page(
85
+ name=_optional_text(name),
86
+ query=_optional_text(query),
87
+ enabled=enabled,
88
+ page=page_request,
89
+ )
90
+ except PaginationError as exc:
91
+ raise _bad_list_request(str(exc)) from exc
92
+ return {
93
+ "clients": [_client_response(client) for client in page.items],
94
+ "next_cursor": page.next_cursor,
95
+ }
96
+
97
+
98
+ @router.post("/v1/admin/clients", status_code=http_status.HTTP_201_CREATED)
99
+ async def create_oauth_client(
100
+ request: Request,
101
+ body: OAuthClientCreateBody,
102
+ ) -> dict[str, object]:
103
+ """Create an OAuth client and return a secret only for confidential clients."""
104
+ settings = get_request_settings(request)
105
+ context = await require_admin_actor(request, required_scopes=(ADMIN_CLIENTS_WRITE_SCOPE,))
106
+ require_admin_actor_csrf(context, request, settings)
107
+
108
+ name = _required_text(body.name, "name")
109
+ redirect_uris = _validated_redirect_uris(body.redirect_uris)
110
+ allowed_scopes = _validated_oauth_scopes(body.allowed_scopes)
111
+ created = create_oauth_client_record(
112
+ name=name,
113
+ client_type=body.client_type,
114
+ redirect_uris=redirect_uris,
115
+ allowed_scopes=allowed_scopes,
116
+ allowed_grant_types=_allowed_grant_types_for_scopes(allowed_scopes),
117
+ settings=settings,
118
+ admin_access_required=body.admin_access_required,
119
+ )
120
+ await get_oauth_client_store(request, settings).create(created.client)
121
+ await _audit_client_change(
122
+ request,
123
+ settings,
124
+ context=context,
125
+ event_type=AuditEventType.CLIENT_CREATED,
126
+ client=created.client,
127
+ payload_extra={"client_type": created.client.client_type.value},
128
+ )
129
+ return _client_create_response(created)
130
+
131
+
132
+ @router.patch("/v1/admin/clients/{client_id}")
133
+ async def update_oauth_client(
134
+ request: Request,
135
+ client_id: str,
136
+ body: OAuthClientUpdateBody,
137
+ ) -> dict[str, object]:
138
+ """Update OAuth client metadata."""
139
+ settings = get_request_settings(request)
140
+ context = await require_admin_actor(request, required_scopes=(ADMIN_CLIENTS_WRITE_SCOPE,))
141
+ require_admin_actor_csrf(context, request, settings)
142
+ if not body.model_fields_set:
143
+ raise HTTPException(
144
+ status_code=http_status.HTTP_400_BAD_REQUEST,
145
+ detail="No OAuth client update fields were provided.",
146
+ )
147
+
148
+ store = get_oauth_client_store(request, settings)
149
+ client = await _get_client_or_404(store, client_id=client_id)
150
+ allowed_scopes = (
151
+ _validated_oauth_scopes(body.allowed_scopes)
152
+ if body.allowed_scopes is not None
153
+ else None
154
+ )
155
+ updated = update_oauth_client_record(
156
+ client,
157
+ name=(
158
+ _required_text(body.name, "name")
159
+ if body.name is not None
160
+ else None
161
+ ),
162
+ redirect_uris=(
163
+ _validated_redirect_uris(body.redirect_uris)
164
+ if body.redirect_uris is not None
165
+ else None
166
+ ),
167
+ allowed_scopes=allowed_scopes,
168
+ allowed_grant_types=(
169
+ _allowed_grant_types_for_scopes(allowed_scopes)
170
+ if allowed_scopes is not None
171
+ else None
172
+ ),
173
+ admin_access_required=body.admin_access_required,
174
+ enabled=body.enabled,
175
+ )
176
+ await store.save(updated)
177
+ await _audit_client_change(
178
+ request,
179
+ settings,
180
+ context=context,
181
+ event_type=AuditEventType.CLIENT_UPDATED,
182
+ client=updated,
183
+ payload_extra={"enabled": updated.status.value == "active"},
184
+ )
185
+ return _client_response(updated)
186
+
187
+
188
+ @router.post("/v1/admin/clients/{client_id}:rotate-secret")
189
+ async def rotate_client_secret(request: Request, client_id: str) -> dict[str, object]:
190
+ """Rotate a confidential client secret and return the raw value once."""
191
+ settings = get_request_settings(request)
192
+ context = await require_admin_actor(request, required_scopes=(ADMIN_CLIENTS_WRITE_SCOPE,))
193
+ require_admin_actor_csrf(context, request, settings)
194
+
195
+ store = get_oauth_client_store(request, settings)
196
+ client = await _get_client_or_404(store, client_id=client_id)
197
+ try:
198
+ rotated = rotate_oauth_client_secret(client, settings=settings)
199
+ except ValueError as exc:
200
+ raise HTTPException(
201
+ status_code=http_status.HTTP_400_BAD_REQUEST,
202
+ detail="Only confidential clients have client secrets.",
203
+ ) from exc
204
+ await store.save(rotated.client)
205
+ await _audit_client_change(
206
+ request,
207
+ settings,
208
+ context=context,
209
+ event_type=AuditEventType.CLIENT_SECRET_ROTATED,
210
+ client=rotated.client,
211
+ )
212
+ return {
213
+ "client_id": rotated.client.id,
214
+ "client_secret": rotated.client_secret,
215
+ }
216
+
217
+
218
+ @router.post("/v1/admin/clients/{client_id}:disable")
219
+ async def disable_client(request: Request, client_id: str) -> dict[str, object]:
220
+ """Disable an OAuth client."""
221
+ settings = get_request_settings(request)
222
+ context = await require_admin_actor(request, required_scopes=(ADMIN_CLIENTS_WRITE_SCOPE,))
223
+ require_admin_actor_csrf(context, request, settings)
224
+
225
+ store = get_oauth_client_store(request, settings)
226
+ client = await _get_client_or_404(store, client_id=client_id)
227
+ disabled = disable_oauth_client(client, clock=utc_now)
228
+ await store.save(disabled)
229
+ await _audit_client_change(
230
+ request,
231
+ settings,
232
+ context=context,
233
+ event_type=AuditEventType.CLIENT_DISABLED,
234
+ client=disabled,
235
+ )
236
+ return _client_response(disabled)
237
+
238
+
239
+ async def _get_client_or_404(
240
+ store: OAuthClientStore,
241
+ *,
242
+ client_id: str,
243
+ ) -> OAuthClientRecord:
244
+ client = await store.get(client_id)
245
+ if client is None:
246
+ raise HTTPException(
247
+ status_code=http_status.HTTP_404_NOT_FOUND,
248
+ detail="OAuth client was not found.",
249
+ )
250
+ return client
251
+
252
+
253
+ def _client_response(client: OAuthClientRecord) -> dict[str, object]:
254
+ return {
255
+ "id": client.id,
256
+ "name": client.name,
257
+ "client_type": client.client_type.value,
258
+ "redirect_uris": list(client.redirect_uris),
259
+ "allowed_scopes": list(client.allowed_scopes),
260
+ "allowed_grant_types": list(client.allowed_grant_types),
261
+ "admin_access_required": client.admin_access_required,
262
+ "enabled": client.status.value == "active",
263
+ }
264
+
265
+
266
+ def _client_create_response(created: CreatedOAuthClient) -> dict[str, object]:
267
+ response: dict[str, object] = {"client": _client_response(created.client)}
268
+ if created.client_secret is not None:
269
+ response["client_secret"] = created.client_secret
270
+ return response
271
+
272
+
273
+ async def _audit_client_change(
274
+ request: Request,
275
+ settings: Settings,
276
+ *,
277
+ context: AdminActorContext,
278
+ event_type: AuditEventType,
279
+ client: OAuthClientRecord,
280
+ payload_extra: dict[str, Any] | None = None,
281
+ ) -> None:
282
+ payload: dict[str, Any] = {
283
+ "oauth_client_id": client.id,
284
+ "client_status": client.status.value,
285
+ }
286
+ if payload_extra is not None:
287
+ payload.update(payload_extra)
288
+ await write_audit_event(
289
+ get_audit_writer(request, settings),
290
+ AuditEventInput(
291
+ event_type=event_type,
292
+ result=AuditResult.SUCCESS,
293
+ actor_user_id=context.user.id,
294
+ client_id=client.id,
295
+ client_ip=client_ip(request),
296
+ user_agent=request.headers.get("user-agent"),
297
+ payload=payload,
298
+ ),
299
+ )
300
+
301
+
302
+ def _required_text(value: str, field_name: str) -> str:
303
+ cleaned = value.strip()
304
+ if not cleaned:
305
+ raise HTTPException(
306
+ status_code=http_status.HTTP_400_BAD_REQUEST,
307
+ detail=f"{field_name} must not be empty.",
308
+ )
309
+ return cleaned
310
+
311
+
312
+ def _optional_text(value: str | None) -> str | None:
313
+ if value is None:
314
+ return None
315
+ cleaned = value.strip()
316
+ return cleaned or None
317
+
318
+
319
+ def _bad_list_request(detail: str) -> HTTPException:
320
+ return HTTPException(status_code=http_status.HTTP_400_BAD_REQUEST, detail=detail)
321
+
322
+
323
+ def _validated_redirect_uris(values: list[str]) -> tuple[str, ...]:
324
+ try:
325
+ return validate_redirect_uris(values)
326
+ except OAuthClientValidationError as exc:
327
+ raise HTTPException(
328
+ status_code=http_status.HTTP_400_BAD_REQUEST,
329
+ detail=str(exc),
330
+ ) from exc
331
+
332
+
333
+ def _validated_oauth_scopes(values: list[str]) -> tuple[str, ...]:
334
+ try:
335
+ return validate_oauth_scopes(values)
336
+ except OAuthClientValidationError as exc:
337
+ raise HTTPException(
338
+ status_code=http_status.HTTP_400_BAD_REQUEST,
339
+ detail=str(exc),
340
+ ) from exc
341
+
342
+
343
+ def _allowed_grant_types_for_scopes(scopes: tuple[str, ...]) -> tuple[str, ...]:
344
+ if OFFLINE_ACCESS_SCOPE in scopes:
345
+ return (AUTHORIZATION_CODE_GRANT, REFRESH_TOKEN_GRANT)
346
+ return (AUTHORIZATION_CODE_GRANT,)