simple-module-oidc 0.0.50__tar.gz → 0.0.51__tar.gz
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.
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/PKG-INFO +14 -11
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/README.md +9 -6
- simple_module_oidc-0.0.51/oidc/contracts/__init__.py +5 -0
- simple_module_oidc-0.0.51/oidc/contracts/user_cache.py +101 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/core/settings.py +2 -3
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/pyproject.toml +5 -5
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/conftest.py +1 -0
- simple_module_oidc-0.0.51/tests/contracts/test_ensure_oidc_user.py +222 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/provider/test_oidc_provider.py +14 -0
- simple_module_oidc-0.0.50/oidc/contracts/__init__.py +0 -1
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/.gitignore +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/__init__.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/core/__init__.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/core/constants.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/core/presets.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/core/runtime.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/core/state.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/__init__.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/client.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/directory.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/discovery.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/jwks.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/persistence.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/provider.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/renewal.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/identity/tokens.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/locales/de.json +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/locales/en.json +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/models.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/module.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/pages/LoggedOut.tsx +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/pages/Login.tsx +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/pages/i18n.generated.ts +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/py.typed +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/web/__init__.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/web/endpoints/__init__.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/web/endpoints/api.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/oidc/web/endpoints/views.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/package.json +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/module/test_oidc_boot.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/module/test_oidc_migration.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/module/test_oidc_module.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/runtime/test_oidc_settings_runtime.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/settings/test_oidc_settings_presets.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/client/test_oidc_client.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/directory/test_oidc_directory.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/discovery/test_oidc_discovery.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/jwks/test_oidc_jwks.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/jwks/test_oidc_jwks_resilience.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/persistence/test_oidc_cache_persistence.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/provider/test_oidc_bearer_policy.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/provider/test_oidc_jwt_policy.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/provider/test_oidc_legacy_authority.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/provider/test_oidc_sign_in_from_claims.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/provider/test_oidc_tenant_resolver.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/renewal/test_oidc_refresh.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/renewal/test_oidc_session_transaction.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/oidc_flow_helpers.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_authorization.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_authorization_query.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_endpoint_auth.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_flow.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_keycloak_flow.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_logout.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_logout_event_identity.py +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests-js/oidc-login.test.tsx +0 -0
- {simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests-js/oidc-translations.test.tsx +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: simple_module_oidc
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.51
|
|
4
4
|
Summary: Generic OIDC authentication provider for simple_module — native Microsoft Entra ID, Auth0, Okta, Keycloak, and any OpenID Connect identity provider
|
|
5
5
|
Project-URL: Homepage, https://github.com/antosubash/simple_module_python
|
|
6
6
|
Project-URL: Repository, https://github.com/antosubash/simple_module_python
|
|
@@ -21,10 +21,10 @@ Requires-Python: >=3.12
|
|
|
21
21
|
Requires-Dist: cryptography>=42
|
|
22
22
|
Requires-Dist: httpx<0.29,>=0.27
|
|
23
23
|
Requires-Dist: pyjwt[crypto]<3,>=2.8
|
|
24
|
-
Requires-Dist: simple-module-auth==0.0.
|
|
25
|
-
Requires-Dist: simple-module-core==0.0.
|
|
26
|
-
Requires-Dist: simple-module-db==0.0.
|
|
27
|
-
Requires-Dist: simple-module-hosting==0.0.
|
|
24
|
+
Requires-Dist: simple-module-auth==0.0.51
|
|
25
|
+
Requires-Dist: simple-module-core==0.0.51
|
|
26
|
+
Requires-Dist: simple-module-db==0.0.51
|
|
27
|
+
Requires-Dist: simple-module-hosting==0.0.51
|
|
28
28
|
Description-Content-Type: text/markdown
|
|
29
29
|
|
|
30
30
|
# simple_module_oidc
|
|
@@ -94,9 +94,12 @@ SM_OIDC_CLIENT_SECRET=<client-secret>
|
|
|
94
94
|
SM_OIDC_ROLES_CLAIM_PATH=realm_access.roles
|
|
95
95
|
```
|
|
96
96
|
|
|
97
|
-
`SM_OIDC_ROLES_CLAIM_PATH` seeds the roles path
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
`SM_OIDC_ROLES_CLAIM_PATH` seeds the roles claim path. Because `role_mapping`
|
|
98
|
+
defaults to `{}` so no shared-IdP role grants app privileges until mapped,
|
|
99
|
+
seed `oidc.role_mapping` after `make migrate` and before the first admin signs in
|
|
100
|
+
(with `simple_module_settings` installed:
|
|
101
|
+
`SM_OIDC_ROLE_MAPPING='{"admin":"admin","user":"user"}' uv run smpy settings import-from-env --prefix SM_OIDC_ROLE_MAPPING`).
|
|
102
|
+
A value saved in the Settings UI later wins.
|
|
100
103
|
|
|
101
104
|
Here `<app>` includes the application's scheme and host, and `<server>` is the
|
|
102
105
|
Keycloak server (including any deployment prefix). Discovery is exactly
|
|
@@ -139,7 +142,7 @@ The `entra` preset derives the discovery URL
|
|
|
139
142
|
(`https://login.microsoftonline.com/{tenant_id}/v2.0/.well-known/openid-configuration`),
|
|
140
143
|
validates the **id_token**, keys the user cache on **(issuer, `oid`)**, and
|
|
141
144
|
maps Entra **app roles** (the `roles` claim) to application roles via
|
|
142
|
-
`role_mapping` (default: `
|
|
145
|
+
`role_mapping` (default: `{}` — configure explicit mappings before the first admin signs in). Assign app roles in
|
|
143
146
|
*Entra → App registrations → App roles* and *Enterprise applications → Users and groups*.
|
|
144
147
|
|
|
145
148
|
## Any other OIDC provider
|
|
@@ -163,7 +166,7 @@ SM_OIDC_CLIENT_SECRET=<client-secret>
|
|
|
163
166
|
| `SM_OIDC_TENANT_ID` | — | Used by templated presets (Entra) to derive the discovery URL. |
|
|
164
167
|
| `SM_OIDC_CLIENT_ID` | — | OAuth client id. |
|
|
165
168
|
| `SM_OIDC_CLIENT_SECRET` | — | OAuth client secret. |
|
|
166
|
-
| `SM_OIDC_ROLES_CLAIM_PATH` | preset | Seeds `roles_claim_path
|
|
169
|
+
| `SM_OIDC_ROLES_CLAIM_PATH` | preset | Seeds `roles_claim_path`; combine with seeding `oidc.role_mapping` via `smpy settings import-from-env` before the first admin signs in. |
|
|
167
170
|
|
|
168
171
|
Only the six environment variables above bootstrap process wiring, secrets and the first admin's roles path.
|
|
169
172
|
These variables seed defaults; explicit DB values can override them.
|
|
@@ -182,7 +185,7 @@ and tenant-claim trust read the live settings state.
|
|
|
182
185
|
| `username_claim` / `name_claim` | `preferred_username` / `name` | Display name with fallback. |
|
|
183
186
|
| `email_claim` | `email` | Display email; only generic OIDC literal `email_verified=true` attests ownership. Entra never attests it. |
|
|
184
187
|
| `roles_claim_path` | `roles` for Entra, otherwise empty | Dotted claim path to role list. |
|
|
185
|
-
| `role_mapping` |
|
|
188
|
+
| `role_mapping` | `{}` (empty) | External roles to application roles; no IdP role grants app privileges until mapped, and `tenant:*` roles are filtered on login and restored sessions. |
|
|
186
189
|
| `jwks_cache_ttl_seconds` | 3600 | Last-good signing keys retained on outage; unknown keys share a 30-second fetch cooldown. |
|
|
187
190
|
|
|
188
191
|
## Login, sessions and logout
|
|
@@ -65,9 +65,12 @@ SM_OIDC_CLIENT_SECRET=<client-secret>
|
|
|
65
65
|
SM_OIDC_ROLES_CLAIM_PATH=realm_access.roles
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
`SM_OIDC_ROLES_CLAIM_PATH` seeds the roles path
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
`SM_OIDC_ROLES_CLAIM_PATH` seeds the roles claim path. Because `role_mapping`
|
|
69
|
+
defaults to `{}` so no shared-IdP role grants app privileges until mapped,
|
|
70
|
+
seed `oidc.role_mapping` after `make migrate` and before the first admin signs in
|
|
71
|
+
(with `simple_module_settings` installed:
|
|
72
|
+
`SM_OIDC_ROLE_MAPPING='{"admin":"admin","user":"user"}' uv run smpy settings import-from-env --prefix SM_OIDC_ROLE_MAPPING`).
|
|
73
|
+
A value saved in the Settings UI later wins.
|
|
71
74
|
|
|
72
75
|
Here `<app>` includes the application's scheme and host, and `<server>` is the
|
|
73
76
|
Keycloak server (including any deployment prefix). Discovery is exactly
|
|
@@ -110,7 +113,7 @@ The `entra` preset derives the discovery URL
|
|
|
110
113
|
(`https://login.microsoftonline.com/{tenant_id}/v2.0/.well-known/openid-configuration`),
|
|
111
114
|
validates the **id_token**, keys the user cache on **(issuer, `oid`)**, and
|
|
112
115
|
maps Entra **app roles** (the `roles` claim) to application roles via
|
|
113
|
-
`role_mapping` (default: `
|
|
116
|
+
`role_mapping` (default: `{}` — configure explicit mappings before the first admin signs in). Assign app roles in
|
|
114
117
|
*Entra → App registrations → App roles* and *Enterprise applications → Users and groups*.
|
|
115
118
|
|
|
116
119
|
## Any other OIDC provider
|
|
@@ -134,7 +137,7 @@ SM_OIDC_CLIENT_SECRET=<client-secret>
|
|
|
134
137
|
| `SM_OIDC_TENANT_ID` | — | Used by templated presets (Entra) to derive the discovery URL. |
|
|
135
138
|
| `SM_OIDC_CLIENT_ID` | — | OAuth client id. |
|
|
136
139
|
| `SM_OIDC_CLIENT_SECRET` | — | OAuth client secret. |
|
|
137
|
-
| `SM_OIDC_ROLES_CLAIM_PATH` | preset | Seeds `roles_claim_path
|
|
140
|
+
| `SM_OIDC_ROLES_CLAIM_PATH` | preset | Seeds `roles_claim_path`; combine with seeding `oidc.role_mapping` via `smpy settings import-from-env` before the first admin signs in. |
|
|
138
141
|
|
|
139
142
|
Only the six environment variables above bootstrap process wiring, secrets and the first admin's roles path.
|
|
140
143
|
These variables seed defaults; explicit DB values can override them.
|
|
@@ -153,7 +156,7 @@ and tenant-claim trust read the live settings state.
|
|
|
153
156
|
| `username_claim` / `name_claim` | `preferred_username` / `name` | Display name with fallback. |
|
|
154
157
|
| `email_claim` | `email` | Display email; only generic OIDC literal `email_verified=true` attests ownership. Entra never attests it. |
|
|
155
158
|
| `roles_claim_path` | `roles` for Entra, otherwise empty | Dotted claim path to role list. |
|
|
156
|
-
| `role_mapping` |
|
|
159
|
+
| `role_mapping` | `{}` (empty) | External roles to application roles; no IdP role grants app privileges until mapped, and `tenant:*` roles are filtered on login and restored sessions. |
|
|
157
160
|
| `jwks_cache_ttl_seconds` | 3600 | Last-good signing keys retained on outage; unknown keys share a 30-second fetch cooldown. |
|
|
158
161
|
|
|
159
162
|
## Login, sessions and logout
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Public contract for pre-creating OIDC user cache rows (#1218)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid as uuid_mod
|
|
6
|
+
|
|
7
|
+
from sqlalchemy import or_, select, update
|
|
8
|
+
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
|
9
|
+
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
|
|
10
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
11
|
+
from sqlmodel import col
|
|
12
|
+
|
|
13
|
+
from oidc.models import OidcUserCache
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async def ensure_oidc_user(
|
|
17
|
+
session: AsyncSession,
|
|
18
|
+
issuer: str,
|
|
19
|
+
subject: str,
|
|
20
|
+
*,
|
|
21
|
+
email: str | None = None,
|
|
22
|
+
full_name: str | None = None,
|
|
23
|
+
user_id: uuid_mod.UUID | None = None,
|
|
24
|
+
) -> uuid_mod.UUID:
|
|
25
|
+
"""Idempotently reserve a framework UUID for an ``(issuer, subject)`` pair.
|
|
26
|
+
|
|
27
|
+
``issuer`` must match the token's exact ``iss`` claim and ``subject`` must
|
|
28
|
+
match the configured ``uid_claim`` (``sub`` for ``generic``, ``oid`` for
|
|
29
|
+
``entra``). Callers must commit ``session`` so subsequent OIDC sign-ins
|
|
30
|
+
attach to the reserved UUID.
|
|
31
|
+
|
|
32
|
+
Raises ``ValueError`` if ``issuer`` or ``subject`` is blank, or if a row
|
|
33
|
+
already exists for ``(issuer, subject)`` with a different ``id`` than an
|
|
34
|
+
explicitly requested ``user_id``.
|
|
35
|
+
"""
|
|
36
|
+
if not isinstance(issuer, str) or not issuer.strip():
|
|
37
|
+
raise ValueError("issuer must be a non-empty string")
|
|
38
|
+
if not isinstance(subject, str) or not subject.strip():
|
|
39
|
+
raise ValueError("subject must be a non-empty string")
|
|
40
|
+
|
|
41
|
+
conn = await session.connection()
|
|
42
|
+
insert_fn = pg_insert if conn.dialect.name == "postgresql" else sqlite_insert
|
|
43
|
+
candidate_id = user_id if user_id is not None else uuid_mod.uuid4()
|
|
44
|
+
insert_stmt = (
|
|
45
|
+
insert_fn(OidcUserCache)
|
|
46
|
+
.values(
|
|
47
|
+
id=candidate_id,
|
|
48
|
+
issuer=issuer,
|
|
49
|
+
subject=subject,
|
|
50
|
+
email=email or "",
|
|
51
|
+
full_name=full_name,
|
|
52
|
+
last_login_at=None,
|
|
53
|
+
)
|
|
54
|
+
.on_conflict_do_nothing(index_elements=["issuer", "subject"])
|
|
55
|
+
)
|
|
56
|
+
result = await session.execute(insert_stmt)
|
|
57
|
+
inserted = (result.rowcount or 0) > 0
|
|
58
|
+
|
|
59
|
+
stmt = select(OidcUserCache).where(
|
|
60
|
+
col(OidcUserCache.issuer) == issuer,
|
|
61
|
+
col(OidcUserCache.subject) == subject,
|
|
62
|
+
)
|
|
63
|
+
row = (await session.execute(stmt)).scalar_one()
|
|
64
|
+
row_id = row.id
|
|
65
|
+
|
|
66
|
+
if user_id is not None and row_id != user_id:
|
|
67
|
+
raise ValueError(
|
|
68
|
+
f"OIDC user ({issuer!r}, {subject!r}) already exists with id {row_id}, "
|
|
69
|
+
f"cannot assign {user_id}"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
if not inserted:
|
|
73
|
+
updated = False
|
|
74
|
+
if email:
|
|
75
|
+
res = await session.execute(
|
|
76
|
+
update(OidcUserCache)
|
|
77
|
+
.where(
|
|
78
|
+
col(OidcUserCache.id) == row_id,
|
|
79
|
+
or_(col(OidcUserCache.email).is_(None), col(OidcUserCache.email) == ""),
|
|
80
|
+
)
|
|
81
|
+
.values(email=email)
|
|
82
|
+
.execution_options(synchronize_session=False)
|
|
83
|
+
)
|
|
84
|
+
updated = updated or (res.rowcount or 0) > 0
|
|
85
|
+
if full_name:
|
|
86
|
+
res = await session.execute(
|
|
87
|
+
update(OidcUserCache)
|
|
88
|
+
.where(
|
|
89
|
+
col(OidcUserCache.id) == row_id,
|
|
90
|
+
or_(
|
|
91
|
+
col(OidcUserCache.full_name).is_(None),
|
|
92
|
+
col(OidcUserCache.full_name) == "",
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
.values(full_name=full_name)
|
|
96
|
+
.execution_options(synchronize_session=False)
|
|
97
|
+
)
|
|
98
|
+
updated = updated or (res.rowcount or 0) > 0
|
|
99
|
+
if updated:
|
|
100
|
+
await session.refresh(row)
|
|
101
|
+
return row_id
|
|
@@ -76,9 +76,8 @@ class OidcSettings(DbBackedSettings):
|
|
|
76
76
|
default=3600, ge=0, json_schema_extra={"requires_restart": True}
|
|
77
77
|
)
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
)
|
|
79
|
+
# Secure by default: no IdP role grants app privileges until mapped (#1217).
|
|
80
|
+
role_mapping: dict[str, str] = Field(default_factory=dict)
|
|
82
81
|
|
|
83
82
|
@model_validator(mode="after")
|
|
84
83
|
def _apply_preset(self) -> OidcSettings:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "simple_module_oidc"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.51"
|
|
4
4
|
description = "Generic OIDC authentication provider for simple_module — native Microsoft Entra ID, Auth0, Okta, Keycloak, and any OpenID Connect identity provider"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
@@ -20,10 +20,10 @@ classifiers = [
|
|
|
20
20
|
"Typing :: Typed",
|
|
21
21
|
]
|
|
22
22
|
dependencies = [
|
|
23
|
-
"simple_module_core==0.0.
|
|
24
|
-
"simple_module_db==0.0.
|
|
25
|
-
"simple_module_hosting==0.0.
|
|
26
|
-
"simple_module_auth==0.0.
|
|
23
|
+
"simple_module_core==0.0.51",
|
|
24
|
+
"simple_module_db==0.0.51",
|
|
25
|
+
"simple_module_hosting==0.0.51",
|
|
26
|
+
"simple_module_auth==0.0.51",
|
|
27
27
|
"PyJWT[crypto]>=2.8,<3",
|
|
28
28
|
"cryptography>=42",
|
|
29
29
|
"httpx>=0.27,<0.29",
|
|
@@ -69,6 +69,7 @@ async def oidc_app(httpx_mock, request):
|
|
|
69
69
|
"client_id": _AUDIENCE,
|
|
70
70
|
"client_secret": "secret",
|
|
71
71
|
"api_audience": "api",
|
|
72
|
+
"role_mapping": {"admin": "admin", "user": "user"},
|
|
72
73
|
}
|
|
73
74
|
config.update(getattr(request, "param", {}))
|
|
74
75
|
oidc_settings = OidcSettings(**config)
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""Tests for oidc.contracts.ensure_oidc_user (#1218) and OidcSettings defaults (#1217)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid
|
|
6
|
+
from types import SimpleNamespace
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
from oidc.contracts import ensure_oidc_user
|
|
10
|
+
from oidc.core.settings import OidcSettings
|
|
11
|
+
from oidc.identity.provider import OidcAuthProvider
|
|
12
|
+
from oidc.models import OidcUserCache
|
|
13
|
+
from simple_module_db.engine.session import init_db
|
|
14
|
+
from sqlalchemy import select
|
|
15
|
+
from sqlalchemy.exc import IntegrityError
|
|
16
|
+
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
17
|
+
from starlette.requests import Request
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_role_mapping_defaults_to_empty_dict():
|
|
21
|
+
settings = OidcSettings()
|
|
22
|
+
assert settings.role_mapping == {}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def test_ensure_oidc_user_is_idempotent_and_backfills_missing_fields(
|
|
26
|
+
db_session: AsyncSession,
|
|
27
|
+
):
|
|
28
|
+
first = await ensure_oidc_user(
|
|
29
|
+
db_session,
|
|
30
|
+
"https://idp.example.com",
|
|
31
|
+
"sub-1",
|
|
32
|
+
)
|
|
33
|
+
second = await ensure_oidc_user(
|
|
34
|
+
db_session,
|
|
35
|
+
"https://idp.example.com",
|
|
36
|
+
"sub-1",
|
|
37
|
+
email="alice@example.com",
|
|
38
|
+
full_name="Alice",
|
|
39
|
+
)
|
|
40
|
+
assert first == second
|
|
41
|
+
|
|
42
|
+
row = (
|
|
43
|
+
await db_session.execute(select(OidcUserCache).where(OidcUserCache.id == first))
|
|
44
|
+
).scalar_one()
|
|
45
|
+
assert row.email == "alice@example.com"
|
|
46
|
+
assert row.full_name == "Alice"
|
|
47
|
+
assert row.last_login_at is None
|
|
48
|
+
|
|
49
|
+
# Subsequent call with different metadata does not overwrite existing values.
|
|
50
|
+
third = await ensure_oidc_user(
|
|
51
|
+
db_session,
|
|
52
|
+
"https://idp.example.com",
|
|
53
|
+
"sub-1",
|
|
54
|
+
email="other@example.com",
|
|
55
|
+
full_name="Other",
|
|
56
|
+
)
|
|
57
|
+
assert third == first
|
|
58
|
+
assert row.email == "alice@example.com"
|
|
59
|
+
assert row.full_name == "Alice"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def test_ensure_oidc_user_backfills_empty_string_full_name_from_login(
|
|
63
|
+
db_session: AsyncSession,
|
|
64
|
+
):
|
|
65
|
+
existing = OidcUserCache(
|
|
66
|
+
issuer="https://idp.example.com",
|
|
67
|
+
subject="sub-empty-name",
|
|
68
|
+
email="",
|
|
69
|
+
full_name="",
|
|
70
|
+
)
|
|
71
|
+
db_session.add(existing)
|
|
72
|
+
await db_session.flush()
|
|
73
|
+
|
|
74
|
+
uid = await ensure_oidc_user(
|
|
75
|
+
db_session,
|
|
76
|
+
"https://idp.example.com",
|
|
77
|
+
"sub-empty-name",
|
|
78
|
+
email="bob@example.com",
|
|
79
|
+
full_name="Bob",
|
|
80
|
+
)
|
|
81
|
+
assert uid == existing.id
|
|
82
|
+
await db_session.refresh(existing)
|
|
83
|
+
assert existing.email == "bob@example.com"
|
|
84
|
+
assert existing.full_name == "Bob"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
async def test_ensure_oidc_user_honors_explicit_user_id_and_rejects_mismatch(
|
|
88
|
+
db_session: AsyncSession,
|
|
89
|
+
):
|
|
90
|
+
explicit_id = uuid.uuid4()
|
|
91
|
+
created = await ensure_oidc_user(
|
|
92
|
+
db_session,
|
|
93
|
+
"https://idp.example.com",
|
|
94
|
+
"sub-explicit",
|
|
95
|
+
user_id=explicit_id,
|
|
96
|
+
)
|
|
97
|
+
assert created == explicit_id
|
|
98
|
+
|
|
99
|
+
again = await ensure_oidc_user(
|
|
100
|
+
db_session,
|
|
101
|
+
"https://idp.example.com",
|
|
102
|
+
"sub-explicit",
|
|
103
|
+
user_id=explicit_id,
|
|
104
|
+
)
|
|
105
|
+
assert again == explicit_id
|
|
106
|
+
|
|
107
|
+
with pytest.raises(ValueError, match="already exists"):
|
|
108
|
+
await ensure_oidc_user(
|
|
109
|
+
db_session,
|
|
110
|
+
"https://idp.example.com",
|
|
111
|
+
"sub-explicit",
|
|
112
|
+
user_id=uuid.uuid4(),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
with pytest.raises(IntegrityError):
|
|
116
|
+
await ensure_oidc_user(
|
|
117
|
+
db_session,
|
|
118
|
+
"https://idp.example.com",
|
|
119
|
+
"sub-different",
|
|
120
|
+
user_id=explicit_id,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
async def test_ensure_oidc_user_plain_sqlite_session_rolls_back(tmp_path):
|
|
125
|
+
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'plain.db'}")
|
|
126
|
+
try:
|
|
127
|
+
async with engine.begin() as conn:
|
|
128
|
+
await conn.run_sync(OidcUserCache.__table__.create)
|
|
129
|
+
factory = async_sessionmaker(engine, expire_on_commit=False)
|
|
130
|
+
|
|
131
|
+
async with factory() as session:
|
|
132
|
+
await ensure_oidc_user(session, "https://idp.example.com", "sub-rollback")
|
|
133
|
+
await session.rollback()
|
|
134
|
+
|
|
135
|
+
async with factory() as session:
|
|
136
|
+
rows = (await session.execute(select(OidcUserCache))).scalars().all()
|
|
137
|
+
assert rows == []
|
|
138
|
+
finally:
|
|
139
|
+
await engine.dispose()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
async def test_ensure_oidc_user_concurrent_backfill_preserves_committed_profile(tmp_path):
|
|
143
|
+
state = init_db(f"sqlite+aiosqlite:///{tmp_path / 'race.db'}")
|
|
144
|
+
try:
|
|
145
|
+
async with state.engine.begin() as conn:
|
|
146
|
+
await conn.run_sync(OidcUserCache.__table__.create)
|
|
147
|
+
|
|
148
|
+
async with state.session_factory() as s0:
|
|
149
|
+
uid = await ensure_oidc_user(s0, "https://idp.example.com", "sub-race")
|
|
150
|
+
await s0.commit()
|
|
151
|
+
|
|
152
|
+
async with state.session_factory() as s1, state.session_factory() as s2:
|
|
153
|
+
# Load the empty row into s2's identity map BEFORE s1 commits its backfill.
|
|
154
|
+
stale = (
|
|
155
|
+
await s2.execute(select(OidcUserCache).where(OidcUserCache.id == uid))
|
|
156
|
+
).scalar_one()
|
|
157
|
+
assert stale.email == "" and stale.full_name is None
|
|
158
|
+
|
|
159
|
+
await ensure_oidc_user(
|
|
160
|
+
s1,
|
|
161
|
+
"https://idp.example.com",
|
|
162
|
+
"sub-race",
|
|
163
|
+
email="first@example.com",
|
|
164
|
+
full_name="First",
|
|
165
|
+
)
|
|
166
|
+
await s1.commit()
|
|
167
|
+
|
|
168
|
+
await ensure_oidc_user(
|
|
169
|
+
s2,
|
|
170
|
+
"https://idp.example.com",
|
|
171
|
+
"sub-race",
|
|
172
|
+
email="second@example.com",
|
|
173
|
+
full_name="Second",
|
|
174
|
+
)
|
|
175
|
+
await s2.commit()
|
|
176
|
+
|
|
177
|
+
async with state.session_factory() as check:
|
|
178
|
+
row = (
|
|
179
|
+
await check.execute(select(OidcUserCache).where(OidcUserCache.id == uid))
|
|
180
|
+
).scalar_one()
|
|
181
|
+
assert row.email == "first@example.com"
|
|
182
|
+
assert row.full_name == "First"
|
|
183
|
+
finally:
|
|
184
|
+
await state.engine.dispose()
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
async def test_ensure_oidc_user_entra_attaches_on_oid_not_sub(tmp_path):
|
|
188
|
+
state = init_db(f"sqlite+aiosqlite:///{tmp_path / 'entra.db'}")
|
|
189
|
+
try:
|
|
190
|
+
async with state.engine.begin() as conn:
|
|
191
|
+
await conn.run_sync(OidcUserCache.__table__.create)
|
|
192
|
+
issuer = "https://login.microsoftonline.com/test-tenant/v2.0"
|
|
193
|
+
claims = {"iss": issuer, "sub": "pairwise-sub", "oid": "entra-oid-42"}
|
|
194
|
+
async with state.session_factory() as db:
|
|
195
|
+
reserved_oid = await ensure_oidc_user(db, issuer, claims["oid"])
|
|
196
|
+
await db.commit()
|
|
197
|
+
|
|
198
|
+
app = SimpleNamespace(state=SimpleNamespace(sm=SimpleNamespace(db=state)))
|
|
199
|
+
request = Request({"type": "http", "app": app, "headers": []})
|
|
200
|
+
provider = OidcAuthProvider(
|
|
201
|
+
OidcSettings(provider="entra", tenant_id="test-tenant", client_id="app")
|
|
202
|
+
)
|
|
203
|
+
ctx, _ = await provider.sign_in_from_claims(request, claims)
|
|
204
|
+
assert ctx.id == str(reserved_oid)
|
|
205
|
+
finally:
|
|
206
|
+
await state.engine.dispose()
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
@pytest.mark.parametrize(
|
|
210
|
+
("issuer", "subject"),
|
|
211
|
+
[
|
|
212
|
+
("", "sub"),
|
|
213
|
+
(" ", "sub"),
|
|
214
|
+
("https://idp.example.com", ""),
|
|
215
|
+
("https://idp.example.com", " "),
|
|
216
|
+
],
|
|
217
|
+
)
|
|
218
|
+
async def test_ensure_oidc_user_rejects_blank_identity(
|
|
219
|
+
db_session: AsyncSession, issuer: str, subject: str
|
|
220
|
+
):
|
|
221
|
+
with pytest.raises(ValueError):
|
|
222
|
+
await ensure_oidc_user(db_session, issuer, subject)
|
|
@@ -124,8 +124,22 @@ def test_roles_claim_path_exact_key_then_dotted_path(path, claims):
|
|
|
124
124
|
client_id="my-app",
|
|
125
125
|
client_secret="secret",
|
|
126
126
|
roles_claim_path=path,
|
|
127
|
+
role_mapping={"admin": "admin"},
|
|
127
128
|
)
|
|
128
129
|
ctx = OidcAuthProvider(settings)._claims_to_user_context(
|
|
129
130
|
{"sub": "s-1", **claims}, cache_id="cache"
|
|
130
131
|
)
|
|
131
132
|
assert ctx.roles == ["admin"]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_default_role_mapping_grants_no_privileges():
|
|
136
|
+
settings = OidcSettings(
|
|
137
|
+
provider="entra",
|
|
138
|
+
tenant_id="test-tenant",
|
|
139
|
+
client_id="my-app",
|
|
140
|
+
client_secret="secret",
|
|
141
|
+
)
|
|
142
|
+
ctx = OidcAuthProvider(settings)._claims_to_user_context(
|
|
143
|
+
{"oid": "u-1", "roles": ["admin", "user"]}, cache_id="cache"
|
|
144
|
+
)
|
|
145
|
+
assert ctx.roles == []
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"""OIDC module contracts."""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/module/test_oidc_migration.py
RENAMED
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/core/module/test_oidc_module.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/client/test_oidc_client.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/jwks/test_oidc_jwks.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/identity/renewal/test_oidc_refresh.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_flow.py
RENAMED
|
File without changes
|
|
File without changes
|
{simple_module_oidc-0.0.50 → simple_module_oidc-0.0.51}/tests/web/endpoints/test_oidc_logout.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|