simple-module-auth 0.0.46__tar.gz → 0.0.48__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_auth-0.0.46 → simple_module_auth-0.0.48}/.gitignore +6 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/PKG-INFO +7 -9
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/README.md +3 -5
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/module.py +1 -6
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/directory.py +25 -5
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/tokens.py +2 -3
- simple_module_auth-0.0.48/auth/web/deps.py +24 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/pyproject.toml +4 -9
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/contracts/provider/test_auth_provider_protocol.py +0 -3
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/contracts/resolver/test_auth_resolver_registry.py +0 -3
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/authority/test_auth_oidc_authority.py +4 -1
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/web/deps/test_auth_deps.py +19 -22
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/web/middleware/test_auth_middleware.py +10 -8
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/web/middleware/test_auth_next_target.py +3 -3
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/web/middleware/test_auth_unknown_paths.py +3 -3
- simple_module_auth-0.0.46/auth/web/deps.py +0 -66
- simple_module_auth-0.0.46/package.json +0 -15
- simple_module_auth-0.0.46/tests/web/deps/test_auth_permission_deprecation.py +0 -15
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/LICENSE +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/__init__.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/contracts/__init__.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/contracts/provider.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/contracts/resolver.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/contracts/schemas.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/contracts/tokens.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/core/__init__.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/core/constants.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/core/state.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/locales/de.json +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/locales/en.json +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/locales/es.json +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/models.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/__init__.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/claims.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/jwks.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/persistence.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/renewal.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/renewal_outcomes.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/session.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/tenant.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/py.typed +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/web/__init__.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/web/_route_match.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/auth/web/middleware.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/conftest.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/contracts/schemas/test_auth_user_context.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/contracts/tokens/test_auth_parse_bearer.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/core/audit/test_auth_audit_redaction.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/core/module/test_auth_module.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/jwks/test_auth_oidc_jwks_cache.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/jwks/test_auth_oidc_jwt_hardening.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/renewal/test_auth_oidc_authority_outcomes.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/renewal/test_auth_oidc_concurrent_renewal.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/renewal/test_auth_oidc_outcome_logout.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/renewal/test_auth_oidc_outcome_migration.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/session/test_auth_oidc_shared_helpers.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/session/test_auth_oidc_sign_in.py +0 -0
- {simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/oidc_common/tokens/test_auth_oidc_tokens.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: simple_module_auth
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.48
|
|
4
4
|
Summary: Session-cookie authentication primitives — middleware, login/logout, redirect helpers for simple_module
|
|
5
5
|
Project-URL: Homepage, https://github.com/antosubash/simple_module_python
|
|
6
6
|
Project-URL: Repository, https://github.com/antosubash/simple_module_python
|
|
@@ -25,9 +25,9 @@ Requires-Dist: cryptography>=42
|
|
|
25
25
|
Requires-Dist: httpx<0.29,>=0.27
|
|
26
26
|
Requires-Dist: itsdangerous<3,>=2.2
|
|
27
27
|
Requires-Dist: pyjwt[crypto]<3,>=2.8
|
|
28
|
-
Requires-Dist: simple-module-core==0.0.
|
|
29
|
-
Requires-Dist: simple-module-db==0.0.
|
|
30
|
-
Requires-Dist: simple-module-hosting==0.0.
|
|
28
|
+
Requires-Dist: simple-module-core==0.0.48
|
|
29
|
+
Requires-Dist: simple-module-db==0.0.48
|
|
30
|
+
Requires-Dist: simple-module-hosting==0.0.48
|
|
31
31
|
Description-Content-Type: text/markdown
|
|
32
32
|
|
|
33
33
|
# simple_module_auth
|
|
@@ -81,11 +81,9 @@ from .constants import PERM_PRODUCTS_CREATE
|
|
|
81
81
|
async def create_product(): ...
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
`
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
permissions, declare a dependency for each. Unlike the legacy helper,
|
|
88
|
-
`RequiresPermission` rechecks administrators demoted during the request.
|
|
84
|
+
`RequiresPermission(PERM_VIEW, PERM_EDIT)` admits a user holding **any** supplied
|
|
85
|
+
key. Declare separate dependencies when every permission is required. The gate
|
|
86
|
+
honours direct grants, token scope caps and administrators demoted during a request.
|
|
89
87
|
|
|
90
88
|
## Depends on
|
|
91
89
|
|
|
@@ -49,11 +49,9 @@ from .constants import PERM_PRODUCTS_CREATE
|
|
|
49
49
|
async def create_product(): ...
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
`
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
permissions, declare a dependency for each. Unlike the legacy helper,
|
|
56
|
-
`RequiresPermission` rechecks administrators demoted during the request.
|
|
52
|
+
`RequiresPermission(PERM_VIEW, PERM_EDIT)` admits a user holding **any** supplied
|
|
53
|
+
key. Declare separate dependencies when every permission is required. The gate
|
|
54
|
+
honours direct grants, token scope caps and administrators demoted during a request.
|
|
57
55
|
|
|
58
56
|
## Depends on
|
|
59
57
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Auth module — shared contracts (UserContext, AuthProvider, deps).
|
|
2
2
|
|
|
3
3
|
Intentionally minimal: this module owns the PUBLIC interface (UserContext,
|
|
4
|
-
AuthProvider, PrincipalResolver, get_current_user, CurrentUser
|
|
4
|
+
AuthProvider, PrincipalResolver, get_current_user, CurrentUser)
|
|
5
5
|
that every other module imports. Keeping it stable prevents churn when auth
|
|
6
6
|
internals change.
|
|
7
7
|
|
|
@@ -13,8 +13,6 @@ credential sources (PAT bearer tokens, API keys, etc.).
|
|
|
13
13
|
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
|
-
import importlib.resources
|
|
17
|
-
from pathlib import Path
|
|
18
16
|
from typing import TYPE_CHECKING
|
|
19
17
|
|
|
20
18
|
from simple_module_core.module import ModuleBase, ModuleMeta
|
|
@@ -50,6 +48,3 @@ class AuthModule(ModuleBase):
|
|
|
50
48
|
from auth.web.middleware import AuthMiddleware
|
|
51
49
|
|
|
52
50
|
app.add_middleware(AuthMiddleware)
|
|
53
|
-
|
|
54
|
-
def locale_dirs(self) -> dict[str, Path]:
|
|
55
|
-
return {"auth": Path(str(importlib.resources.files(__package__) / "locales"))}
|
|
@@ -12,7 +12,11 @@ from simple_module_core.user_directory import (
|
|
|
12
12
|
chunked,
|
|
13
13
|
parse_user_ids,
|
|
14
14
|
)
|
|
15
|
-
from simple_module_db.user_directory_sql import
|
|
15
|
+
from simple_module_db.user_directory_sql import (
|
|
16
|
+
name_or_email_contains,
|
|
17
|
+
search_rows,
|
|
18
|
+
to_directory_user,
|
|
19
|
+
)
|
|
16
20
|
from sqlalchemy import String, func, select
|
|
17
21
|
from sqlalchemy.ext.compiler import compiles
|
|
18
22
|
from sqlalchemy.sql.expression import FunctionElement
|
|
@@ -65,13 +69,29 @@ class CacheDirectory:
|
|
|
65
69
|
found[key] = to_directory_user(row, key)
|
|
66
70
|
return found
|
|
67
71
|
|
|
68
|
-
async def search(
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
async def search(
|
|
73
|
+
self,
|
|
74
|
+
db: Any,
|
|
75
|
+
term: str | None,
|
|
76
|
+
*,
|
|
77
|
+
limit: int,
|
|
78
|
+
include_ids: Collection[str] | None = None,
|
|
79
|
+
exclude_ids: Collection[str] = (),
|
|
80
|
+
active_only: bool = False,
|
|
81
|
+
exact_email: bool = False,
|
|
82
|
+
) -> list[DirectoryUser]:
|
|
83
|
+
stmt = select(*self._columns).order_by(self.model.email)
|
|
84
|
+
if exclude_ids:
|
|
85
|
+
stmt = stmt.where(self.model.id.not_in(list(parse_user_ids(exclude_ids))))
|
|
86
|
+
# Cache entries represent signed-in users; this provider has no activity flag.
|
|
87
|
+
if exact_email:
|
|
88
|
+
stmt = stmt.where(func.lower(self.model.email) == (term or "").strip().lower())
|
|
89
|
+
elif term and term.strip():
|
|
71
90
|
stmt = stmt.where(
|
|
72
91
|
name_or_email_contains(self.model.full_name, self.model.email, term.strip())
|
|
73
92
|
)
|
|
74
|
-
|
|
93
|
+
rows = await search_rows(db, stmt, self.model.id, include_ids=include_ids, limit=limit)
|
|
94
|
+
return [to_directory_user(row, str(row.id)) for row in rows]
|
|
75
95
|
|
|
76
96
|
def match_ids(self, term: str) -> Any | None:
|
|
77
97
|
return select(_UuidText(self.model.id)).where(
|
|
@@ -15,8 +15,7 @@ from types import SimpleNamespace
|
|
|
15
15
|
from typing import Any
|
|
16
16
|
|
|
17
17
|
from cryptography.fernet import Fernet, InvalidToken
|
|
18
|
-
from
|
|
19
|
-
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
|
18
|
+
from simple_module_core.crypto import derive_key
|
|
20
19
|
|
|
21
20
|
logger = logging.getLogger(__name__)
|
|
22
21
|
# Browsers drop a cookie over 4096 bytes, and a dropped session cookie means a
|
|
@@ -28,7 +27,7 @@ _COOKIE_OVERHEAD_BYTES = 200
|
|
|
28
27
|
|
|
29
28
|
def _fernet(app: Any, info: bytes) -> Fernet:
|
|
30
29
|
secret = app.state.sm.settings.secret_key
|
|
31
|
-
key =
|
|
30
|
+
key = derive_key(secret, info)
|
|
32
31
|
return Fernet(base64.urlsafe_b64encode(key))
|
|
33
32
|
|
|
34
33
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""FastAPI auth dependencies — get_current_user, CurrentUser."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Annotated
|
|
6
|
+
|
|
7
|
+
from fastapi import Depends, HTTPException, Request
|
|
8
|
+
from simple_module_hosting.i18n_deps import TranslatorDep
|
|
9
|
+
|
|
10
|
+
from auth.contracts.schemas import UserContext
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
async def get_current_user(request: Request, t: TranslatorDep) -> UserContext:
|
|
14
|
+
"""Extract the authenticated user from request state.
|
|
15
|
+
|
|
16
|
+
The auth middleware must set ``request.state.user`` before this runs.
|
|
17
|
+
"""
|
|
18
|
+
user = getattr(request.state, "user", None)
|
|
19
|
+
if user is None:
|
|
20
|
+
raise HTTPException(status_code=401, detail=t.t("auth.errors.not_authenticated"))
|
|
21
|
+
return user
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
CurrentUser = Annotated[UserContext, Depends(get_current_user)]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "simple_module_auth"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.48"
|
|
4
4
|
description = "Session-cookie authentication primitives — middleware, login/logout, redirect helpers for simple_module"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
@@ -25,9 +25,9 @@ dependencies = [
|
|
|
25
25
|
"PyJWT[crypto]>=2.8,<3",
|
|
26
26
|
"cryptography>=42",
|
|
27
27
|
"httpx>=0.27,<0.29",
|
|
28
|
-
"simple_module_core==0.0.
|
|
29
|
-
"simple_module_db==0.0.
|
|
30
|
-
"simple_module_hosting==0.0.
|
|
28
|
+
"simple_module_core==0.0.48",
|
|
29
|
+
"simple_module_db==0.0.48",
|
|
30
|
+
"simple_module_hosting==0.0.48",
|
|
31
31
|
]
|
|
32
32
|
|
|
33
33
|
[project.entry-points.simple_module]
|
|
@@ -46,11 +46,6 @@ build-backend = "hatchling.build"
|
|
|
46
46
|
[tool.hatch.build.targets.wheel]
|
|
47
47
|
packages = ["auth"]
|
|
48
48
|
|
|
49
|
-
# Ship the module-root package.json inside the wheel so the host can
|
|
50
|
-
# discover JS deps via importlib.resources after a pip install.
|
|
51
|
-
[tool.hatch.build.targets.wheel.force-include]
|
|
52
|
-
"package.json" = "auth/package.json"
|
|
53
|
-
|
|
54
49
|
[tool.uv.sources]
|
|
55
50
|
simple_module_core = { workspace = true }
|
|
56
51
|
simple_module_db = { workspace = true }
|
|
@@ -21,9 +21,6 @@ class _FakeProvider:
|
|
|
21
21
|
def get_logout_url(self, request: Request) -> str:
|
|
22
22
|
return "/fake/logout"
|
|
23
23
|
|
|
24
|
-
def get_public_paths(self) -> tuple[tuple[str, ...], tuple[str, ...]]:
|
|
25
|
-
return (("/fake/login",), ())
|
|
26
|
-
|
|
27
24
|
def is_bearer_request(self, request: Request) -> bool:
|
|
28
25
|
return False
|
|
29
26
|
|
|
@@ -105,6 +105,7 @@ async def test_real_users_login_cookie_is_anonymous_after_provider_switch(
|
|
|
105
105
|
from users.accounts.manager import UserManager
|
|
106
106
|
from users.accounts.provider import UsersAuthProvider, clear_session_version_cache
|
|
107
107
|
from users.core.settings import UsersSettings
|
|
108
|
+
from users.core.token_secrets import resolve_token_secrets
|
|
108
109
|
from users.models import OAuthAccount, Role, User
|
|
109
110
|
|
|
110
111
|
role = Role(name="admin")
|
|
@@ -122,7 +123,9 @@ async def test_real_users_login_cookie_is_anonymous_after_provider_switch(
|
|
|
122
123
|
@app.post("/login")
|
|
123
124
|
async def login(request: Request):
|
|
124
125
|
adapter = UserDatabaseWithRoles(db_session, User, OAuthAccount)
|
|
125
|
-
await UserManager(
|
|
126
|
+
await UserManager(
|
|
127
|
+
adapter, SimpleNamespace(), config, resolve_token_secrets("test-secret-key")
|
|
128
|
+
).on_after_login(user, request)
|
|
126
129
|
ctx = await app.state.auth.auth_provider.resolve_user(request)
|
|
127
130
|
return {"roles": ctx.roles}
|
|
128
131
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Tests for auth FastAPI dependencies (get_current_user
|
|
1
|
+
"""Tests for auth FastAPI dependencies (get_current_user and the hosting permission gate)."""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
@@ -9,9 +9,10 @@ from unittest.mock import MagicMock
|
|
|
9
9
|
|
|
10
10
|
import pytest
|
|
11
11
|
from auth.contracts.schemas import UserContext
|
|
12
|
-
from auth.web.deps import get_current_user
|
|
13
|
-
from fastapi import HTTPException
|
|
12
|
+
from auth.web.deps import get_current_user
|
|
13
|
+
from fastapi import Depends, HTTPException
|
|
14
14
|
from simple_module_core.i18n import I18nRegistry, Translator
|
|
15
|
+
from simple_module_hosting.permissions import RequiresPermission
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
def _translator() -> Translator:
|
|
@@ -57,45 +58,41 @@ class TestGetCurrentUser:
|
|
|
57
58
|
|
|
58
59
|
class TestRequirePermission:
|
|
59
60
|
async def test_raises_403_when_missing_permission(self, app):
|
|
60
|
-
"""The
|
|
61
|
-
|
|
62
|
-
check_fn = dep.dependency
|
|
61
|
+
"""The RequiresPermission check raises 403 when user lacks permissions."""
|
|
62
|
+
check_fn = RequiresPermission("products.delete")
|
|
63
63
|
|
|
64
64
|
user = UserContext(id="u1", email="u@test.com", name="User", roles=["viewer"])
|
|
65
65
|
request = _request(app, user)
|
|
66
66
|
|
|
67
67
|
with pytest.raises(HTTPException) as exc_info:
|
|
68
|
-
await check_fn(request
|
|
68
|
+
await check_fn(request)
|
|
69
69
|
assert exc_info.value.status_code == 403
|
|
70
70
|
|
|
71
71
|
async def test_admin_bypasses_permission_check(self, app):
|
|
72
|
-
"""The
|
|
73
|
-
|
|
74
|
-
check_fn = dep.dependency
|
|
72
|
+
"""The RequiresPermission check allows admin users through."""
|
|
73
|
+
check_fn = RequiresPermission("products.delete")
|
|
75
74
|
|
|
76
75
|
admin_user = UserContext(id="a1", email="admin@test.com", name="Admin", roles=["admin"])
|
|
77
76
|
request = _request(app, admin_user)
|
|
78
|
-
await check_fn(request
|
|
77
|
+
await check_fn(request)
|
|
79
78
|
|
|
80
79
|
|
|
81
80
|
class TestRequirePermissionAdvanced:
|
|
82
81
|
async def test_multiple_permissions_any_match(self, app):
|
|
83
82
|
"""User with any of the required permissions should pass."""
|
|
84
|
-
|
|
85
|
-
check_fn = dep.dependency
|
|
83
|
+
check_fn = RequiresPermission("products.view", "products.edit")
|
|
86
84
|
|
|
87
85
|
admin = UserContext(id="a1", email="a@t.com", name="Admin", roles=["admin"])
|
|
88
86
|
request = _request(app, admin)
|
|
89
|
-
await check_fn(request
|
|
87
|
+
await check_fn(request)
|
|
90
88
|
|
|
91
89
|
async def test_non_admin_without_permission_fails(self, app):
|
|
92
|
-
|
|
93
|
-
check_fn = dep.dependency
|
|
90
|
+
check_fn = RequiresPermission("products.delete")
|
|
94
91
|
|
|
95
92
|
user = UserContext(id="u1", email="u@t.com", name="User", roles=["user"])
|
|
96
93
|
request = _request(app, user)
|
|
97
94
|
with pytest.raises(HTTPException) as exc_info:
|
|
98
|
-
await check_fn(request
|
|
95
|
+
await check_fn(request)
|
|
99
96
|
assert exc_info.value.status_code == 403
|
|
100
97
|
assert "products.delete" in str(exc_info.value.detail)
|
|
101
98
|
|
|
@@ -106,19 +103,19 @@ class TestRequirePermissionAdvanced:
|
|
|
106
103
|
so every non-admin role held nothing and this was a 403.
|
|
107
104
|
"""
|
|
108
105
|
app.state.sm.permissions.map_role("clerk", ["products.delete"])
|
|
109
|
-
|
|
106
|
+
check_fn = RequiresPermission("products.delete")
|
|
110
107
|
user = UserContext(id="u1", email="u@t.com", name="User", roles=["clerk"])
|
|
111
|
-
await
|
|
108
|
+
await check_fn(_request(app, user))
|
|
112
109
|
|
|
113
110
|
|
|
114
|
-
def
|
|
115
|
-
"""``token_guard`` admits API tokens on a route gated by ``
|
|
111
|
+
def test_requires_permission_is_a_token_gate():
|
|
112
|
+
"""``token_guard`` admits API tokens on a route gated by ``RequiresPermission``."""
|
|
116
113
|
from fastapi import FastAPI
|
|
117
114
|
from simple_module_hosting.token_guard import route_admits_tokens
|
|
118
115
|
|
|
119
116
|
app = FastAPI()
|
|
120
117
|
|
|
121
|
-
@app.get("/g", dependencies=[
|
|
118
|
+
@app.get("/g", dependencies=[Depends(RequiresPermission("x.view"))])
|
|
122
119
|
async def gated() -> None:
|
|
123
120
|
return None
|
|
124
121
|
|
{simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/web/middleware/test_auth_middleware.py
RENAMED
|
@@ -8,6 +8,7 @@ from auth.contracts.schemas import UserContext
|
|
|
8
8
|
from auth.core.state import AuthState
|
|
9
9
|
from auth.web.middleware import AuthMiddleware
|
|
10
10
|
from fastapi import FastAPI, Request
|
|
11
|
+
from simple_module_core.public_routes import PublicRouteRegistry
|
|
11
12
|
from starlette.middleware.sessions import SessionMiddleware
|
|
12
13
|
from starlette.responses import JSONResponse
|
|
13
14
|
|
|
@@ -36,9 +37,6 @@ class _StubProvider:
|
|
|
36
37
|
def get_logout_url(self, request):
|
|
37
38
|
return "/stub/logout"
|
|
38
39
|
|
|
39
|
-
def get_public_paths(self):
|
|
40
|
-
return (("/stub/login", "/stub/public/"), ())
|
|
41
|
-
|
|
42
40
|
def is_bearer_request(self, request):
|
|
43
41
|
auth = request.headers.get("authorization", "")
|
|
44
42
|
return auth.startswith("Bearer ")
|
|
@@ -50,8 +48,11 @@ def _build_app(provider, *, principal_resolvers=None, public_routes=None):
|
|
|
50
48
|
auth_provider=provider,
|
|
51
49
|
principal_resolvers=list(principal_resolvers or []),
|
|
52
50
|
)
|
|
53
|
-
if public_routes is
|
|
54
|
-
|
|
51
|
+
if public_routes is None:
|
|
52
|
+
public_routes = PublicRouteRegistry()
|
|
53
|
+
public_routes.add_prefix("/stub/login")
|
|
54
|
+
public_routes.add_prefix("/stub/public/")
|
|
55
|
+
app.state.public_routes = public_routes
|
|
55
56
|
|
|
56
57
|
async def _handler(request: Request, path: str = ""):
|
|
57
58
|
user = getattr(request.state, "user", None)
|
|
@@ -163,13 +164,14 @@ async def test_registry_method_scoping_gates_other_verbs():
|
|
|
163
164
|
assert gated.status_code == 401
|
|
164
165
|
|
|
165
166
|
|
|
166
|
-
async def
|
|
167
|
-
"""
|
|
167
|
+
async def test_no_registry_keeps_module_paths_gated(unauthenticated_app):
|
|
168
|
+
"""Missing public-route registration must fail closed."""
|
|
169
|
+
del unauthenticated_app.state.public_routes
|
|
168
170
|
transport = httpx.ASGITransport(app=unauthenticated_app)
|
|
169
171
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as c:
|
|
170
172
|
public = await c.get("/stub/public/data")
|
|
171
173
|
gated = await c.get("/api/protected")
|
|
172
|
-
assert public.status_code ==
|
|
174
|
+
assert public.status_code == 302
|
|
173
175
|
assert gated.status_code == 401
|
|
174
176
|
|
|
175
177
|
|
|
@@ -7,6 +7,7 @@ import pytest
|
|
|
7
7
|
from auth.core.state import AuthState
|
|
8
8
|
from auth.web.middleware import AuthMiddleware
|
|
9
9
|
from fastapi import FastAPI, Request
|
|
10
|
+
from simple_module_core.public_routes import PublicRouteRegistry
|
|
10
11
|
from simple_module_core.redirect_safety import SESSION_NEXT_KEY
|
|
11
12
|
from starlette.middleware.sessions import SessionMiddleware
|
|
12
13
|
from starlette.responses import JSONResponse
|
|
@@ -26,15 +27,14 @@ class _StubProvider:
|
|
|
26
27
|
def get_logout_url(self, request):
|
|
27
28
|
return "/stub/logout"
|
|
28
29
|
|
|
29
|
-
def get_public_paths(self):
|
|
30
|
-
return (("/stub/login", "/stub/public/"), ())
|
|
31
|
-
|
|
32
30
|
def is_bearer_request(self, request):
|
|
33
31
|
return False
|
|
34
32
|
|
|
35
33
|
|
|
36
34
|
def _build_app(provider) -> FastAPI:
|
|
37
35
|
app = FastAPI()
|
|
36
|
+
app.state.public_routes = PublicRouteRegistry()
|
|
37
|
+
app.state.public_routes.add_prefix("/stub/login")
|
|
38
38
|
app.state.auth = AuthState(auth_provider=provider, principal_resolvers=[])
|
|
39
39
|
|
|
40
40
|
# /static/ is framework-public, so this reads the session without auth.
|
|
@@ -13,6 +13,7 @@ from auth.contracts.schemas import UserContext
|
|
|
13
13
|
from auth.core.state import AuthState
|
|
14
14
|
from auth.web.middleware import AuthMiddleware
|
|
15
15
|
from fastapi import APIRouter, FastAPI
|
|
16
|
+
from simple_module_core.public_routes import PublicRouteRegistry
|
|
16
17
|
from simple_module_core.redirect_safety import SESSION_NEXT_KEY
|
|
17
18
|
from starlette.middleware.sessions import SessionMiddleware
|
|
18
19
|
from starlette.requests import Request
|
|
@@ -36,15 +37,14 @@ class _Provider:
|
|
|
36
37
|
def get_logout_url(self, request):
|
|
37
38
|
return "/stub/logout"
|
|
38
39
|
|
|
39
|
-
def get_public_paths(self):
|
|
40
|
-
return (("/stub/login",), ())
|
|
41
|
-
|
|
42
40
|
def is_bearer_request(self, request):
|
|
43
41
|
return request.headers.get("authorization", "").startswith("Bearer ")
|
|
44
42
|
|
|
45
43
|
|
|
46
44
|
def _build(user: UserContext | None = None) -> FastAPI:
|
|
47
45
|
app = FastAPI()
|
|
46
|
+
app.state.public_routes = PublicRouteRegistry()
|
|
47
|
+
app.state.public_routes.add_prefix("/stub/login")
|
|
48
48
|
app.state.auth = AuthState(auth_provider=_Provider(user), principal_resolvers=[])
|
|
49
49
|
|
|
50
50
|
async def ok():
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"""FastAPI auth dependencies — get_current_user, require_permission."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from typing import Annotated
|
|
6
|
-
|
|
7
|
-
from fastapi import Depends, HTTPException, Request
|
|
8
|
-
from simple_module_core.deprecation import deprecated
|
|
9
|
-
from simple_module_core.permissions import ADMIN_ROLE, grants
|
|
10
|
-
from simple_module_hosting.i18n_deps import TranslatorDep
|
|
11
|
-
from simple_module_hosting.permissions import ensure_resolved_permissions
|
|
12
|
-
from simple_module_hosting.token_guard import GATE_PERMISSION, mark_token_gate
|
|
13
|
-
|
|
14
|
-
from auth.contracts.schemas import UserContext
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
async def get_current_user(request: Request, t: TranslatorDep) -> UserContext:
|
|
18
|
-
"""Extract the authenticated user from request state.
|
|
19
|
-
|
|
20
|
-
The auth middleware must set ``request.state.user`` before this runs.
|
|
21
|
-
"""
|
|
22
|
-
user = getattr(request.state, "user", None)
|
|
23
|
-
if user is None:
|
|
24
|
-
raise HTTPException(status_code=401, detail=t.t("auth.errors.not_authenticated"))
|
|
25
|
-
return user
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
CurrentUser = Annotated[UserContext, Depends(get_current_user)]
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
@deprecated(remove_in="0.1.0", use="simple_module_hosting.permissions.RequiresPermission")
|
|
32
|
-
def require_permission(*permissions: str):
|
|
33
|
-
"""Legacy any-of gate, removed in 0.1.0; use ``RequiresPermission`` instead.
|
|
34
|
-
|
|
35
|
-
Retains the role-based admin bypass and cached permission behaviour for
|
|
36
|
-
compatibility. ``RequiresPermission`` rechecks a principal demoted during
|
|
37
|
-
the request; this helper does not.
|
|
38
|
-
"""
|
|
39
|
-
|
|
40
|
-
async def check(
|
|
41
|
-
request: Request,
|
|
42
|
-
t: TranslatorDep,
|
|
43
|
-
user: UserContext = Depends(get_current_user),
|
|
44
|
-
):
|
|
45
|
-
# Admin role bypasses permission checks — but not for a token: an
|
|
46
|
-
# admin's PAT is worth only the scopes it carries, and the resolved
|
|
47
|
-
# set below already applies that cap.
|
|
48
|
-
if ADMIN_ROLE in user.roles and user.scopes is None:
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
# The same resolution RequiresPermission reads: the registry's role map
|
|
52
|
-
# plus module grant sources. This used to call get_permissions_for_roles
|
|
53
|
-
# without a map, which grants every non-admin role nothing (GH #337).
|
|
54
|
-
held = await ensure_resolved_permissions(request)
|
|
55
|
-
if not any(grants(held, p) for p in permissions):
|
|
56
|
-
raise HTTPException(
|
|
57
|
-
status_code=403,
|
|
58
|
-
detail=t.t(
|
|
59
|
-
"auth.errors.missing_permission",
|
|
60
|
-
permissions=", ".join(permissions),
|
|
61
|
-
),
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
# A permission gate: API tokens may reach the route, capped at their
|
|
65
|
-
# scopes (``simple_module_hosting.token_guard``).
|
|
66
|
-
return Depends(mark_token_gate(check, GATE_PERMISSION))
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@simple-module-py/auth",
|
|
3
|
-
"version": "0.0.46",
|
|
4
|
-
"private": true,
|
|
5
|
-
"description": "Frontend assets for the Auth module",
|
|
6
|
-
"peerDependencies": {
|
|
7
|
-
"@inertiajs/react": "^3.11.0",
|
|
8
|
-
"@simple-module-py/ui": "*",
|
|
9
|
-
"react": "^19.0.0",
|
|
10
|
-
"react-dom": "^19.3.1"
|
|
11
|
-
},
|
|
12
|
-
"devDependencies": {
|
|
13
|
-
"@simple-module-py/tsconfig": "*"
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"""The legacy any-of permission helper carries a migration warning (#698)."""
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
from auth.web.deps import require_permission
|
|
5
|
-
from simple_module_core.deprecation import registered_deprecations
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def test_auth_require_permission_warns_and_registers_removal():
|
|
9
|
-
with pytest.warns(DeprecationWarning, match="RequiresPermission"):
|
|
10
|
-
dependency = require_permission("products.view")
|
|
11
|
-
assert callable(dependency.dependency)
|
|
12
|
-
entry = next(
|
|
13
|
-
d for d in registered_deprecations() if d.name == "auth.web.deps.require_permission"
|
|
14
|
-
)
|
|
15
|
-
assert entry.remove_in == "0.1.0"
|
|
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_auth-0.0.46 → simple_module_auth-0.0.48}/auth/oidc_common/renewal_outcomes.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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_module_auth-0.0.46 → simple_module_auth-0.0.48}/tests/core/module/test_auth_module.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
|
|
File without changes
|
|
File without changes
|