tba-protocol-oauth 0.4.0.dev2__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.
- tba_protocol_oauth/__init__.py +122 -0
- tba_protocol_oauth/bindings.py +116 -0
- tba_protocol_oauth/capability_requirements.py +5 -0
- tba_protocol_oauth/claim_sets.py +19 -0
- tba_protocol_oauth/claims.py +79 -0
- tba_protocol_oauth/compatibility.py +39 -0
- tba_protocol_oauth/errors.py +25 -0
- tba_protocol_oauth/features.py +36 -0
- tba_protocol_oauth/jwtoken.py +16 -0
- tba_protocol_oauth/migrations.py +37 -0
- tba_protocol_oauth/protocol.py +282 -0
- tba_protocol_oauth/py.typed +0 -0
- tba_protocol_oauth/reusable_components.py +5 -0
- tba_protocol_oauth/schemas.py +198 -0
- tba_protocol_oauth/standards/__init__.py +9 -0
- tba_protocol_oauth/standards/_dpop/__init__.py +57 -0
- tba_protocol_oauth/standards/_dpop/metadata.py +18 -0
- tba_protocol_oauth/standards/_dpop/primitives.py +194 -0
- tba_protocol_oauth/standards/_dpop/proofs.py +393 -0
- tba_protocol_oauth/standards/_dpop/state.py +74 -0
- tba_protocol_oauth/standards/_introspection_activity.py +32 -0
- tba_protocol_oauth/standards/_mtls_identity.py +98 -0
- tba_protocol_oauth/standards/_rfc8693/__init__.py +26 -0
- tba_protocol_oauth/standards/_rfc8693/endpoint.py +56 -0
- tba_protocol_oauth/standards/_rfc8693/runtime.py +289 -0
- tba_protocol_oauth/standards/_rfc9700/__init__.py +45 -0
- tba_protocol_oauth/standards/_rfc9700/security_profile.py +265 -0
- tba_protocol_oauth/standards/_rfc9700/sender_constraints.py +397 -0
- tba_protocol_oauth/standards/assertion_framework.py +173 -0
- tba_protocol_oauth/standards/authorization_framework.py +102 -0
- tba_protocol_oauth/standards/authorization_server_metadata.py +43 -0
- tba_protocol_oauth/standards/authorization_server_metadata_endpoint.py +21 -0
- tba_protocol_oauth/standards/bearer_token_usage.py +86 -0
- tba_protocol_oauth/standards/client_registration_management.py +89 -0
- tba_protocol_oauth/standards/device_authorization.py +140 -0
- tba_protocol_oauth/standards/dpop.py +59 -0
- tba_protocol_oauth/standards/dynamic_client_registration.py +68 -0
- tba_protocol_oauth/standards/gnap.py +6 -0
- tba_protocol_oauth/standards/introspection.py +56 -0
- tba_protocol_oauth/standards/issuer_identification.py +130 -0
- tba_protocol_oauth/standards/json_web_token.py +159 -0
- tba_protocol_oauth/standards/jwt_access_tokens.py +44 -0
- tba_protocol_oauth/standards/jwt_best_practices.py +44 -0
- tba_protocol_oauth/standards/jwt_client_auth.py +208 -0
- tba_protocol_oauth/standards/jwt_secured_authorization_requests.py +421 -0
- tba_protocol_oauth/standards/legacy_jwt_client_assertions.py +113 -0
- tba_protocol_oauth/standards/mutual_tls_client_authentication.py +394 -0
- tba_protocol_oauth/standards/native_apps.py +132 -0
- tba_protocol_oauth/standards/oauth_security_bcp.py +47 -0
- tba_protocol_oauth/standards/proof_key_for_code_exchange.py +82 -0
- tba_protocol_oauth/standards/protected_resource_metadata.py +103 -0
- tba_protocol_oauth/standards/pushed_authorization_requests.py +198 -0
- tba_protocol_oauth/standards/resource_indicators.py +127 -0
- tba_protocol_oauth/standards/resource_verifier_contract.py +91 -0
- tba_protocol_oauth/standards/revocation.py +59 -0
- tba_protocol_oauth/standards/rich_authorization_requests.py +221 -0
- tba_protocol_oauth/standards/token_endpoint.py +76 -0
- tba_protocol_oauth/standards/token_exchange.py +60 -0
- tba_protocol_oauth/versions.py +110 -0
- tba_protocol_oauth-0.4.0.dev2.dist-info/METADATA +114 -0
- tba_protocol_oauth-0.4.0.dev2.dist-info/RECORD +62 -0
- tba_protocol_oauth-0.4.0.dev2.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Proof Key for Code Exchange utilities.
|
|
2
|
+
|
|
3
|
+
This module keeps the OAuth protocol public API while delegating reusable
|
|
4
|
+
RFC 7636 proof-key primitives to ``tba-provider-security-proof-pkce``.
|
|
5
|
+
|
|
6
|
+
See RFC 7636: https://www.rfc-editor.org/rfc/rfc7636
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import warnings
|
|
12
|
+
from typing import Final
|
|
13
|
+
|
|
14
|
+
from tba_provider_security_proof_pkce import (
|
|
15
|
+
PKCE_SPEC_URL,
|
|
16
|
+
make_pkce_verifier,
|
|
17
|
+
pkce_s256_challenge,
|
|
18
|
+
validate_pkce_verifier,
|
|
19
|
+
verify_pkce_s256_challenge,
|
|
20
|
+
)
|
|
21
|
+
from tba_contracts_identity.protocol_configuration import (
|
|
22
|
+
protocol_settings as settings,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
RFC7636_SPEC_URL: Final = PKCE_SPEC_URL
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def makeCodeVerifier(length: int = 43) -> str:
|
|
29
|
+
"""Return a high-entropy RFC 7636 ``code_verifier`` string."""
|
|
30
|
+
|
|
31
|
+
try:
|
|
32
|
+
return make_pkce_verifier(length)
|
|
33
|
+
except ValueError as exc:
|
|
34
|
+
raise ValueError("length must be between 43 and 128 characters") from exc
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def makeCodeChallenge(verifier: str) -> str:
|
|
38
|
+
"""Derive an ``S256`` ``code_challenge`` from *verifier*."""
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
value = validate_pkce_verifier(verifier)
|
|
42
|
+
except ValueError as exc:
|
|
43
|
+
raise ValueError("invalid code_verifier") from exc
|
|
44
|
+
return pkce_s256_challenge(value)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def verify_code_challenge(
|
|
48
|
+
verifier: str, challenge: str, *, enabled: bool | None = None
|
|
49
|
+
) -> bool:
|
|
50
|
+
"""Return ``True`` if *challenge* matches *verifier* using ``S256``."""
|
|
51
|
+
|
|
52
|
+
if enabled is None:
|
|
53
|
+
enabled = settings.enable_rfc7636
|
|
54
|
+
return verify_pkce_s256_challenge(verifier, challenge, enabled=enabled)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def create_code_verifier(length: int = 43) -> str:
|
|
58
|
+
warnings.warn(
|
|
59
|
+
"create_code_verifier is deprecated, use makeCodeVerifier",
|
|
60
|
+
DeprecationWarning,
|
|
61
|
+
stacklevel=2,
|
|
62
|
+
)
|
|
63
|
+
return makeCodeVerifier(length)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def create_code_challenge(verifier: str) -> str:
|
|
67
|
+
warnings.warn(
|
|
68
|
+
"create_code_challenge is deprecated, use makeCodeChallenge",
|
|
69
|
+
DeprecationWarning,
|
|
70
|
+
stacklevel=2,
|
|
71
|
+
)
|
|
72
|
+
return makeCodeChallenge(verifier)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
__all__ = [
|
|
76
|
+
"makeCodeVerifier",
|
|
77
|
+
"makeCodeChallenge",
|
|
78
|
+
"verify_code_challenge",
|
|
79
|
+
"RFC7636_SPEC_URL",
|
|
80
|
+
"create_code_verifier",
|
|
81
|
+
"create_code_challenge",
|
|
82
|
+
]
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""OAuth 2.0 Protected Resource Metadata semantics (RFC 9728)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Final, Protocol
|
|
6
|
+
|
|
7
|
+
from tba_primitives_identity.standards import StandardOwner, describe_owner
|
|
8
|
+
|
|
9
|
+
from .authorization_server_metadata import ISSUER, JWKS_PATH
|
|
10
|
+
from .oauth_security_bcp import runtime_security_profile
|
|
11
|
+
from .resource_verifier_contract import build_protected_resource_verifier_contract
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
RFC9728_SPEC_URL: Final[str] = "https://www.rfc-editor.org/rfc/rfc9728"
|
|
15
|
+
PROTECTED_RESOURCE_METADATA_PATH: Final[str] = "/.well-known/oauth-protected-resource"
|
|
16
|
+
ML_DSA_65_ALG: Final[str] = "ML-DSA-65"
|
|
17
|
+
OWNER = StandardOwner(
|
|
18
|
+
label="RFC 9728",
|
|
19
|
+
title="OAuth 2.0 Protected Resource Metadata",
|
|
20
|
+
runtime_status="profile-aware-protected-resource-metadata",
|
|
21
|
+
public_surface=(PROTECTED_RESOURCE_METADATA_PATH,),
|
|
22
|
+
notes=("HTTP publication is composed above the protocol layer.",),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ProtectedResourceDeployment(Protocol):
|
|
27
|
+
profile: str
|
|
28
|
+
issuer: str
|
|
29
|
+
flags: dict[str, bool | str]
|
|
30
|
+
active_targets: tuple[str, ...]
|
|
31
|
+
|
|
32
|
+
def flag_enabled(self, name: str) -> bool: ...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _pqc_jose_enabled(deployment: ProtectedResourceDeployment) -> bool:
|
|
36
|
+
configured = (
|
|
37
|
+
str(deployment.flags.get("jwt_signing_alg", "") or "").replace("_", "-").upper()
|
|
38
|
+
)
|
|
39
|
+
return deployment.flag_enabled("enable_pqc_jose") or configured in {
|
|
40
|
+
"ML-DSA-65",
|
|
41
|
+
"MLDSA65",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _access_token_signing_algs(
|
|
46
|
+
deployment: ProtectedResourceDeployment,
|
|
47
|
+
) -> list[str]:
|
|
48
|
+
algs = ["EdDSA"]
|
|
49
|
+
if _pqc_jose_enabled(deployment):
|
|
50
|
+
algs.append(ML_DSA_65_ALG)
|
|
51
|
+
return algs
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def build_protected_resource_metadata(
|
|
55
|
+
deployment: ProtectedResourceDeployment,
|
|
56
|
+
) -> dict[str, object]:
|
|
57
|
+
policy = runtime_security_profile(deployment)
|
|
58
|
+
verifier_contract = build_protected_resource_verifier_contract(deployment)
|
|
59
|
+
methods = ["header"]
|
|
60
|
+
if bool(deployment.flags.get("enable_rfc6750_form", False)):
|
|
61
|
+
methods.append("body")
|
|
62
|
+
if bool(deployment.flags.get("enable_rfc6750_query", False)):
|
|
63
|
+
methods.append("query")
|
|
64
|
+
|
|
65
|
+
issuer = verifier_contract.issuer or ISSUER
|
|
66
|
+
return {
|
|
67
|
+
"resource": verifier_contract.resource,
|
|
68
|
+
"authorization_servers": list(verifier_contract.accepted_issuers),
|
|
69
|
+
"jwks_uri": f"{issuer}{JWKS_PATH}",
|
|
70
|
+
"bearer_methods_supported": methods,
|
|
71
|
+
"dpop_signing_alg_values_supported": ["EdDSA"] if policy.dpop_supported else [],
|
|
72
|
+
"tls_client_certificate_bound_access_tokens": bool(
|
|
73
|
+
policy.mtls_supported or policy.sender_constraint_required
|
|
74
|
+
),
|
|
75
|
+
"fapi_profiles_supported": ["fapi2-security"] if policy.fapi_mode else [],
|
|
76
|
+
"resource_documentation": f"{issuer}/docs/resource-metadata",
|
|
77
|
+
"scopes_supported": ["openid", "profile", "email"],
|
|
78
|
+
"access_token_signing_alg_values_supported": _access_token_signing_algs(
|
|
79
|
+
deployment
|
|
80
|
+
),
|
|
81
|
+
"active_targets": list(deployment.active_targets),
|
|
82
|
+
**verifier_contract.as_metadata_projection(),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def describe() -> dict[str, object]:
|
|
87
|
+
return describe_owner(
|
|
88
|
+
OWNER,
|
|
89
|
+
specification_version="RFC 9728",
|
|
90
|
+
metadata_path=PROTECTED_RESOURCE_METADATA_PATH,
|
|
91
|
+
spec_url=RFC9728_SPEC_URL,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
__all__ = [
|
|
96
|
+
"ML_DSA_65_ALG",
|
|
97
|
+
"OWNER",
|
|
98
|
+
"PROTECTED_RESOURCE_METADATA_PATH",
|
|
99
|
+
"RFC9728_SPEC_URL",
|
|
100
|
+
"ProtectedResourceDeployment",
|
|
101
|
+
"build_protected_resource_metadata",
|
|
102
|
+
"describe",
|
|
103
|
+
]
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"""OAuth 2.0 Pushed Authorization Requests owner and helper module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from typing import Any, Final, Mapping
|
|
8
|
+
|
|
9
|
+
from tba_contracts_identity.oauth import (
|
|
10
|
+
PARValidationResult,
|
|
11
|
+
PushedAuthorizationPersistenceRequest,
|
|
12
|
+
PushedAuthorizationResult,
|
|
13
|
+
)
|
|
14
|
+
from tba_primitives_identity.standards import StandardOwner, describe_owner
|
|
15
|
+
from tba_capability_authorization_request_staging import PushedAuthorizationCapability
|
|
16
|
+
|
|
17
|
+
STATUS: Final[str] = "durable-request-uri-runtime"
|
|
18
|
+
RFC9126_SPEC_URL: Final[str] = "https://www.rfc-editor.org/rfc/rfc9126"
|
|
19
|
+
REQUEST_URI_PREFIX: Final[str] = "urn:ietf:params:oauth:request_uri:"
|
|
20
|
+
PAR_ONE_TIME_USE: Final[bool] = True
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
OWNER = StandardOwner(
|
|
24
|
+
label="RFC 9126",
|
|
25
|
+
title="OAuth 2.0 Pushed Authorization Requests",
|
|
26
|
+
runtime_status=STATUS,
|
|
27
|
+
public_surface=("/par", "/authorize"),
|
|
28
|
+
notes=(
|
|
29
|
+
"Authoritative standards-tree owner module for durable PAR persistence, request_uri "
|
|
30
|
+
"validation, expiration checks, client binding, and one-time consumption semantics."
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class PushedAuthorizationRequestError(ValueError):
|
|
36
|
+
"""Raised when a pushed authorization request violates the active PAR policy."""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class PushedAuthorizationDisabledError(RuntimeError):
|
|
40
|
+
"""Raised when composition disables the RFC 9126 protocol feature."""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True, slots=True)
|
|
44
|
+
class RFC9126PushedAuthorizationService:
|
|
45
|
+
"""Map normalized RFC 9126 input to durable pushed authorization."""
|
|
46
|
+
|
|
47
|
+
capability: PushedAuthorizationCapability
|
|
48
|
+
enabled: bool = True
|
|
49
|
+
|
|
50
|
+
async def push(
|
|
51
|
+
self,
|
|
52
|
+
*,
|
|
53
|
+
client_id: str,
|
|
54
|
+
tenant_id: str | None,
|
|
55
|
+
params: Mapping[str, object],
|
|
56
|
+
) -> PushedAuthorizationResult:
|
|
57
|
+
if not self.enabled:
|
|
58
|
+
raise PushedAuthorizationDisabledError(
|
|
59
|
+
f"RFC 9126 support is disabled: {RFC9126_SPEC_URL}"
|
|
60
|
+
)
|
|
61
|
+
call = await self.capability.call(
|
|
62
|
+
"push_authorization_request",
|
|
63
|
+
PushedAuthorizationPersistenceRequest(
|
|
64
|
+
client_id=client_id,
|
|
65
|
+
tenant_id=tenant_id,
|
|
66
|
+
params=dict(params),
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
result = call.value
|
|
70
|
+
if not isinstance(result, PushedAuthorizationResult):
|
|
71
|
+
raise TypeError(
|
|
72
|
+
"oauth.pushed-authorization must return PushedAuthorizationResult"
|
|
73
|
+
)
|
|
74
|
+
validate_request_uri(result.request_uri)
|
|
75
|
+
return result
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _utc(value: Any) -> datetime:
|
|
79
|
+
if isinstance(value, datetime):
|
|
80
|
+
return value if value.tzinfo is not None else value.replace(tzinfo=timezone.utc)
|
|
81
|
+
raise PushedAuthorizationRequestError(
|
|
82
|
+
f"invalid PAR timestamp value: {RFC9126_SPEC_URL}"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def is_request_uri(value: str | None) -> bool:
|
|
87
|
+
return bool(value) and str(value).startswith(REQUEST_URI_PREFIX)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def validate_request_uri(value: str | None) -> str:
|
|
91
|
+
request_uri = str(value or "").strip()
|
|
92
|
+
if not is_request_uri(request_uri):
|
|
93
|
+
raise PushedAuthorizationRequestError(
|
|
94
|
+
f"invalid request_uri value: {RFC9126_SPEC_URL}"
|
|
95
|
+
)
|
|
96
|
+
return request_uri
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def validate_pushed_authorization_request_row(
|
|
100
|
+
row: Any,
|
|
101
|
+
*,
|
|
102
|
+
client_id: str | None = None,
|
|
103
|
+
request_uri: str | None = None,
|
|
104
|
+
now: datetime | None = None,
|
|
105
|
+
) -> PARValidationResult:
|
|
106
|
+
effective_request_uri = validate_request_uri(
|
|
107
|
+
request_uri or getattr(row, "request_uri", None)
|
|
108
|
+
)
|
|
109
|
+
row_request_uri = validate_request_uri(getattr(row, "request_uri", None))
|
|
110
|
+
if row_request_uri != effective_request_uri:
|
|
111
|
+
raise PushedAuthorizationRequestError(
|
|
112
|
+
f"request_uri mismatch: {RFC9126_SPEC_URL}"
|
|
113
|
+
)
|
|
114
|
+
expires_at = _utc(getattr(row, "expires_at", None))
|
|
115
|
+
current = now if now is not None else datetime.now(timezone.utc)
|
|
116
|
+
current = (
|
|
117
|
+
current if current.tzinfo is not None else current.replace(tzinfo=timezone.utc)
|
|
118
|
+
)
|
|
119
|
+
consumed_at = getattr(row, "consumed_at", None)
|
|
120
|
+
if consumed_at is not None:
|
|
121
|
+
consumed_at = _utc(consumed_at)
|
|
122
|
+
if expires_at <= current:
|
|
123
|
+
raise PushedAuthorizationRequestError(
|
|
124
|
+
f"request_uri has expired: {RFC9126_SPEC_URL}"
|
|
125
|
+
)
|
|
126
|
+
if PAR_ONE_TIME_USE and consumed_at is not None:
|
|
127
|
+
raise PushedAuthorizationRequestError(
|
|
128
|
+
f"request_uri has already been consumed: {RFC9126_SPEC_URL}"
|
|
129
|
+
)
|
|
130
|
+
bound_client = getattr(row, "client_id", None)
|
|
131
|
+
if (
|
|
132
|
+
client_id not in {None, ""}
|
|
133
|
+
and bound_client not in {None, "", client_id}
|
|
134
|
+
and str(bound_client) != str(client_id)
|
|
135
|
+
):
|
|
136
|
+
raise PushedAuthorizationRequestError(
|
|
137
|
+
f"request_uri is not bound to the presented client_id: {RFC9126_SPEC_URL}"
|
|
138
|
+
)
|
|
139
|
+
params = dict(getattr(row, "params", None) or {})
|
|
140
|
+
return PARValidationResult(
|
|
141
|
+
request_uri=row_request_uri,
|
|
142
|
+
client_id=str(bound_client) if bound_client not in {None, ""} else None,
|
|
143
|
+
expires_at=expires_at,
|
|
144
|
+
consumed=consumed_at is not None,
|
|
145
|
+
params=params,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def consume_pushed_authorization_request(
|
|
150
|
+
row: Any, *, now: datetime | None = None
|
|
151
|
+
) -> datetime:
|
|
152
|
+
consumed_at = now if now is not None else datetime.now(timezone.utc)
|
|
153
|
+
consumed_at = (
|
|
154
|
+
consumed_at
|
|
155
|
+
if consumed_at.tzinfo is not None
|
|
156
|
+
else consumed_at.replace(tzinfo=timezone.utc)
|
|
157
|
+
)
|
|
158
|
+
setattr(row, "consumed_at", consumed_at)
|
|
159
|
+
return consumed_at
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def par_result_payload(result: PARValidationResult) -> dict[str, Any]:
|
|
163
|
+
return {
|
|
164
|
+
"request_uri": result.request_uri,
|
|
165
|
+
"client_id": result.client_id,
|
|
166
|
+
"expires_at": result.expires_at.isoformat(),
|
|
167
|
+
"consumed": result.consumed,
|
|
168
|
+
"param_keys": sorted(result.params.keys()),
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def describe() -> dict[str, object]:
|
|
173
|
+
return describe_owner(
|
|
174
|
+
OWNER,
|
|
175
|
+
request_uri_prefix=REQUEST_URI_PREFIX,
|
|
176
|
+
one_time_use=PAR_ONE_TIME_USE,
|
|
177
|
+
spec_url=RFC9126_SPEC_URL,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
__all__ = [
|
|
182
|
+
"STATUS",
|
|
183
|
+
"RFC9126_SPEC_URL",
|
|
184
|
+
"REQUEST_URI_PREFIX",
|
|
185
|
+
"PAR_ONE_TIME_USE",
|
|
186
|
+
"StandardOwner",
|
|
187
|
+
"PARValidationResult",
|
|
188
|
+
"PushedAuthorizationRequestError",
|
|
189
|
+
"PushedAuthorizationDisabledError",
|
|
190
|
+
"RFC9126PushedAuthorizationService",
|
|
191
|
+
"OWNER",
|
|
192
|
+
"is_request_uri",
|
|
193
|
+
"validate_request_uri",
|
|
194
|
+
"validate_pushed_authorization_request_row",
|
|
195
|
+
"consume_pushed_authorization_request",
|
|
196
|
+
"par_result_payload",
|
|
197
|
+
"describe",
|
|
198
|
+
]
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""Resource Indicators for OAuth 2.0 owner and helper module.
|
|
2
|
+
|
|
3
|
+
The repository retains a bounded but certification-grade resource-indicator
|
|
4
|
+
profile:
|
|
5
|
+
|
|
6
|
+
- every supplied ``resource`` value must be a valid absolute URI
|
|
7
|
+
- multiple identical values are allowed
|
|
8
|
+
- multiple *distinct* values fail closed with ``invalid_target`` in the active
|
|
9
|
+
release path because this package binds one effective resource audience per
|
|
10
|
+
request/issued token
|
|
11
|
+
- when both ``audience`` and ``resource`` are supplied they must resolve to the
|
|
12
|
+
same effective target or the request fails closed with ``invalid_target``
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Final, Sequence
|
|
18
|
+
from tba_contracts_identity.oauth import ResourceSelection
|
|
19
|
+
from tba_primitives_identity.standards import StandardOwner, describe_owner
|
|
20
|
+
from urllib.parse import urlparse
|
|
21
|
+
|
|
22
|
+
STATUS: Final[str] = "resource-bound-runtime"
|
|
23
|
+
RFC8707_SPEC_URL: Final[str] = "https://www.rfc-editor.org/rfc/rfc8707"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
OWNER = StandardOwner(
|
|
27
|
+
label="RFC 8707",
|
|
28
|
+
title="Resource Indicators for OAuth 2.0",
|
|
29
|
+
runtime_status=STATUS,
|
|
30
|
+
public_surface=(
|
|
31
|
+
"/authorize",
|
|
32
|
+
"/token",
|
|
33
|
+
"/device_authorization",
|
|
34
|
+
"/token/exchange",
|
|
35
|
+
"/par",
|
|
36
|
+
),
|
|
37
|
+
notes=(
|
|
38
|
+
"Canonical standards-tree owner module for resource indicator validation across "
|
|
39
|
+
"authorization, token, PAR, device, and token-exchange surfaces. The active "
|
|
40
|
+
"release path binds one effective resource audience per request and fails "
|
|
41
|
+
"closed on conflicting or ambiguous resource/audience inputs."
|
|
42
|
+
),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _coerce_resources(resources: Sequence[str] | str | None) -> list[str]:
|
|
47
|
+
if resources is None:
|
|
48
|
+
return []
|
|
49
|
+
if isinstance(resources, str):
|
|
50
|
+
return [resources] if resources else []
|
|
51
|
+
if isinstance(resources, Sequence) and len(resources) == 0:
|
|
52
|
+
return []
|
|
53
|
+
return [str(item) for item in resources if str(item)]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _validate_resource_uri(value: str) -> str:
|
|
57
|
+
parsed = urlparse(value)
|
|
58
|
+
if not parsed.scheme or not parsed.netloc or parsed.fragment:
|
|
59
|
+
raise ValueError(f"invalid resource indicator: {RFC8707_SPEC_URL}")
|
|
60
|
+
return value
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def select_resource_indicator(
|
|
64
|
+
resources: Sequence[str] | str | None,
|
|
65
|
+
*,
|
|
66
|
+
audience: str | None = None,
|
|
67
|
+
allow_multiple_distinct: bool = False,
|
|
68
|
+
) -> ResourceSelection:
|
|
69
|
+
normalized = [_validate_resource_uri(item) for item in _coerce_resources(resources)]
|
|
70
|
+
distinct = tuple(dict.fromkeys(normalized))
|
|
71
|
+
if not distinct:
|
|
72
|
+
return ResourceSelection(resources=(), resource=None, audience=audience)
|
|
73
|
+
if len(distinct) > 1 and not allow_multiple_distinct:
|
|
74
|
+
raise ValueError(
|
|
75
|
+
f"ambiguous resource indicators are not supported: {RFC8707_SPEC_URL}"
|
|
76
|
+
)
|
|
77
|
+
selected = distinct[0]
|
|
78
|
+
if audience not in {None, "", selected}:
|
|
79
|
+
raise ValueError(
|
|
80
|
+
f"conflicting audience/resource values are not supported: {RFC8707_SPEC_URL}"
|
|
81
|
+
)
|
|
82
|
+
return ResourceSelection(
|
|
83
|
+
resources=distinct,
|
|
84
|
+
resource=selected,
|
|
85
|
+
audience=selected if selected is not None else audience,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def extract_resource(
|
|
90
|
+
resources: Sequence[str] | str | None, *, audience: str | None = None
|
|
91
|
+
) -> str | None:
|
|
92
|
+
return select_resource_indicator(resources, audience=audience).resource
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def resource_binding_summary(
|
|
96
|
+
resources: Sequence[str] | str | None, *, audience: str | None = None
|
|
97
|
+
) -> dict[str, object]:
|
|
98
|
+
selection = select_resource_indicator(resources, audience=audience)
|
|
99
|
+
return {
|
|
100
|
+
"resource_count": len(selection.resources),
|
|
101
|
+
"resources": list(selection.resources),
|
|
102
|
+
"resource": selection.resource,
|
|
103
|
+
"audience": selection.audience,
|
|
104
|
+
"single_effective_target": True,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def describe() -> dict[str, object]:
|
|
109
|
+
return describe_owner(
|
|
110
|
+
OWNER,
|
|
111
|
+
single_effective_target=True,
|
|
112
|
+
conflicting_inputs_fail_closed=True,
|
|
113
|
+
spec_url=RFC8707_SPEC_URL,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
__all__ = [
|
|
118
|
+
"STATUS",
|
|
119
|
+
"RFC8707_SPEC_URL",
|
|
120
|
+
"StandardOwner",
|
|
121
|
+
"OWNER",
|
|
122
|
+
"ResourceSelection",
|
|
123
|
+
"extract_resource",
|
|
124
|
+
"resource_binding_summary",
|
|
125
|
+
"select_resource_indicator",
|
|
126
|
+
"describe",
|
|
127
|
+
]
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Executable verifier-grade protected-resource contracts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from tba_contracts_identity.oauth import ProtectedResourceVerifierContract
|
|
6
|
+
from tba_protocol_oauth.standards.oauth_security_bcp import (
|
|
7
|
+
OAuthDeploymentProfile,
|
|
8
|
+
runtime_security_profile,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
ML_DSA_65_ALG = "ML-DSA-65"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _pqc_jose_enabled(deployment: OAuthDeploymentProfile) -> bool:
|
|
15
|
+
configured = (
|
|
16
|
+
str(
|
|
17
|
+
getattr(deployment, "jwt_signing_alg", "")
|
|
18
|
+
or deployment.flags.get("jwt_signing_alg", "")
|
|
19
|
+
)
|
|
20
|
+
.replace("_", "-")
|
|
21
|
+
.upper()
|
|
22
|
+
)
|
|
23
|
+
return deployment.flag_enabled("enable_pqc_jose") or configured in {
|
|
24
|
+
"ML-DSA-65",
|
|
25
|
+
"MLDSA65",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _allowed_token_algs(
|
|
30
|
+
deployment: OAuthDeploymentProfile,
|
|
31
|
+
) -> tuple[str, ...]:
|
|
32
|
+
algs = ["RS256", "ES256", "EdDSA"]
|
|
33
|
+
if _pqc_jose_enabled(deployment):
|
|
34
|
+
algs.append(ML_DSA_65_ALG)
|
|
35
|
+
return tuple(algs)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def build_protected_resource_verifier_contract(
|
|
39
|
+
deployment: OAuthDeploymentProfile,
|
|
40
|
+
) -> ProtectedResourceVerifierContract:
|
|
41
|
+
policy = runtime_security_profile(deployment)
|
|
42
|
+
issuer = str(getattr(deployment, "issuer", "")).rstrip("/")
|
|
43
|
+
resource = str(getattr(deployment, "protected_resource_identifier", ""))
|
|
44
|
+
if not issuer or not resource:
|
|
45
|
+
raise ValueError("protected-resource deployment requires issuer and resource")
|
|
46
|
+
|
|
47
|
+
modes: list[str] = ["bearer"]
|
|
48
|
+
if policy.dpop_supported:
|
|
49
|
+
modes.append("dpop")
|
|
50
|
+
if policy.mtls_supported:
|
|
51
|
+
modes.append("mtls")
|
|
52
|
+
|
|
53
|
+
required_claims = ["iss", "sub", "aud", "exp", "iat"]
|
|
54
|
+
if policy.sender_constraint_required:
|
|
55
|
+
required_claims.append("cnf")
|
|
56
|
+
|
|
57
|
+
replay_expectation = (
|
|
58
|
+
"proof-bound replay resistant"
|
|
59
|
+
if policy.sender_constraint_required
|
|
60
|
+
else "bearer replay constrained by token lifetime"
|
|
61
|
+
)
|
|
62
|
+
freshness_expectation = "introspection or local validation required at request time"
|
|
63
|
+
return ProtectedResourceVerifierContract(
|
|
64
|
+
verifier_logic_id=f"resource-verifier:{deployment.profile}",
|
|
65
|
+
issuer=issuer,
|
|
66
|
+
accepted_issuers=(issuer,),
|
|
67
|
+
resource=resource,
|
|
68
|
+
accepted_audiences=(resource,),
|
|
69
|
+
accepted_token_classes=("access_token",),
|
|
70
|
+
allowed_algs=_allowed_token_algs(deployment),
|
|
71
|
+
jwks_uri=f"{issuer}/.well-known/jwks.json",
|
|
72
|
+
introspection_endpoint=f"{issuer}/introspect",
|
|
73
|
+
sender_constraint_modes=tuple(dict.fromkeys(modes)),
|
|
74
|
+
sender_constraint_required=bool(policy.sender_constraint_required),
|
|
75
|
+
required_claims=tuple(required_claims),
|
|
76
|
+
required_scopes=("openid",),
|
|
77
|
+
max_authz_staleness_seconds=300,
|
|
78
|
+
cache_policy="cache metadata and JWKS only within configured freshness windows; fail closed on stale authorization state",
|
|
79
|
+
clock_skew_seconds=60,
|
|
80
|
+
fail_closed=True,
|
|
81
|
+
revocation_check="introspection required for opaque tokens and for JWTs when local freshness cannot be proven",
|
|
82
|
+
replay_expectation=replay_expectation,
|
|
83
|
+
freshness_expectation=freshness_expectation,
|
|
84
|
+
introspection_auth_methods=tuple(policy.allowed_client_auth_methods),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
__all__ = [
|
|
89
|
+
"ProtectedResourceVerifierContract",
|
|
90
|
+
"build_protected_resource_verifier_contract",
|
|
91
|
+
]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""RFC 7009 capability mapping without HTTP or persistence ownership."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Final
|
|
7
|
+
|
|
8
|
+
from tba_contracts_identity.tokens import (
|
|
9
|
+
TokenRevocationRequest,
|
|
10
|
+
TokenRevocationResult,
|
|
11
|
+
)
|
|
12
|
+
from tba_capability_token_revocation import TokenRevocationCapability
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
RFC7009_SPEC_URL: Final[str] = "https://www.rfc-editor.org/rfc/rfc7009"
|
|
16
|
+
CANONICAL_REVOCATION_PATH: Final[str] = "/revoke"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RevocationDisabledError(RuntimeError):
|
|
20
|
+
"""Raised when composition disables the RFC 7009 protocol feature."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class RFC7009RevocationService:
|
|
25
|
+
"""Map RFC 7009 semantics to protocol-neutral token revocation."""
|
|
26
|
+
|
|
27
|
+
capability: TokenRevocationCapability
|
|
28
|
+
enabled: bool = True
|
|
29
|
+
|
|
30
|
+
async def revoke(
|
|
31
|
+
self,
|
|
32
|
+
token: str,
|
|
33
|
+
*,
|
|
34
|
+
token_type_hint: str | None = None,
|
|
35
|
+
) -> TokenRevocationResult:
|
|
36
|
+
if not self.enabled:
|
|
37
|
+
raise RevocationDisabledError(
|
|
38
|
+
f"RFC 7009 support is disabled: {RFC7009_SPEC_URL}"
|
|
39
|
+
)
|
|
40
|
+
call = await self.capability.call(
|
|
41
|
+
"revoke_token",
|
|
42
|
+
TokenRevocationRequest(
|
|
43
|
+
token,
|
|
44
|
+
token_type_hint=token_type_hint,
|
|
45
|
+
reason="revoked_via_endpoint",
|
|
46
|
+
),
|
|
47
|
+
)
|
|
48
|
+
result = call.value
|
|
49
|
+
if not isinstance(result, TokenRevocationResult):
|
|
50
|
+
raise TypeError("token.revocation must return TokenRevocationResult")
|
|
51
|
+
return result
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
__all__ = [
|
|
55
|
+
"CANONICAL_REVOCATION_PATH",
|
|
56
|
+
"RFC7009RevocationService",
|
|
57
|
+
"RFC7009_SPEC_URL",
|
|
58
|
+
"RevocationDisabledError",
|
|
59
|
+
]
|