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,265 @@
|
|
|
1
|
+
"""Executable security-BCP posture helpers for RFC 9700-aligned deployments.
|
|
2
|
+
|
|
3
|
+
This module converts hardening, FAPI 2.0 security, and peer profiles from declarative target sets
|
|
4
|
+
into runtime-enforceable policy. The authoritative release path uses these
|
|
5
|
+
helpers from the authorization endpoint, token endpoint, contract generation,
|
|
6
|
+
and discovery metadata generation so the published posture matches runtime
|
|
7
|
+
behavior.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import Any, Final, Mapping, Protocol, Sequence
|
|
13
|
+
|
|
14
|
+
from tba_contracts_identity.oauth import (
|
|
15
|
+
RuntimeSecurityProfile,
|
|
16
|
+
SenderConstraintResult as SenderConstraintResult,
|
|
17
|
+
)
|
|
18
|
+
from tba_protocol_oauth.standards.assertion_framework import (
|
|
19
|
+
JWT_BEARER_GRANT_TYPE,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
RFC9700_SPEC_URL: Final[str] = "https://www.rfc-editor.org/rfc/rfc9700"
|
|
23
|
+
DEVICE_CODE_GRANT_TYPE: Final[str] = "urn:ietf:params:oauth:grant-type:device_code"
|
|
24
|
+
TOKEN_EXCHANGE_GRANT_TYPE: Final[str] = (
|
|
25
|
+
"urn:ietf:params:oauth:grant-type:token-exchange"
|
|
26
|
+
)
|
|
27
|
+
_DPOP_HEADER_NAMES: Final[tuple[str, ...]] = ("DPoP", "dpop")
|
|
28
|
+
_CLIENT_CERT_HEADER_NAMES: Final[tuple[str, ...]] = (
|
|
29
|
+
"X-Client-Cert-SHA256",
|
|
30
|
+
"X-SSL-Client-SHA256",
|
|
31
|
+
"X_SSL_CLIENT_CERT_SHA256",
|
|
32
|
+
"MTLS-Client-Cert-SHA256",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class OAuthDeploymentProfile(Protocol):
|
|
37
|
+
profile: str
|
|
38
|
+
flags: Mapping[str, bool | str]
|
|
39
|
+
|
|
40
|
+
def flag_enabled(self, name: str) -> bool: ...
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class OAuthPolicyViolation(ValueError):
|
|
44
|
+
"""A fail-closed runtime policy violation."""
|
|
45
|
+
|
|
46
|
+
def __init__(self, error: str, description: str, status_code: int = 400) -> None:
|
|
47
|
+
super().__init__(description)
|
|
48
|
+
self.error = error
|
|
49
|
+
self.description = description
|
|
50
|
+
self.status_code = status_code
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _is_hardened_profile(deployment: OAuthDeploymentProfile) -> bool:
|
|
54
|
+
return deployment.profile in {"hardening", "fapi2-security", "peer-claim"}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _is_fapi_profile(deployment: OAuthDeploymentProfile) -> bool:
|
|
58
|
+
return deployment.profile == "fapi2-security"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _base_response_types(deployment: OAuthDeploymentProfile) -> tuple[str, ...]:
|
|
62
|
+
if not deployment.flag_enabled("enable_oidc_core"):
|
|
63
|
+
return ("code",)
|
|
64
|
+
if _is_hardened_profile(deployment):
|
|
65
|
+
return ("code",)
|
|
66
|
+
return (
|
|
67
|
+
"code",
|
|
68
|
+
"token",
|
|
69
|
+
"id_token",
|
|
70
|
+
"code token",
|
|
71
|
+
"code id_token",
|
|
72
|
+
"token id_token",
|
|
73
|
+
"code token id_token",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _base_response_modes(deployment: OAuthDeploymentProfile) -> tuple[str, ...]:
|
|
78
|
+
if _is_hardened_profile(deployment):
|
|
79
|
+
return ("query", "form_post")
|
|
80
|
+
return ("query", "fragment", "form_post")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _base_grant_types(deployment: OAuthDeploymentProfile) -> tuple[str, ...]:
|
|
84
|
+
grants: list[str] = ["authorization_code", "client_credentials", "refresh_token"]
|
|
85
|
+
if not _is_hardened_profile(deployment):
|
|
86
|
+
grants.append("password")
|
|
87
|
+
if deployment.flag_enabled("enable_rfc7521") or deployment.flag_enabled(
|
|
88
|
+
"enable_rfc7523"
|
|
89
|
+
):
|
|
90
|
+
grants.append(JWT_BEARER_GRANT_TYPE)
|
|
91
|
+
if deployment.flag_enabled("enable_rfc8628"):
|
|
92
|
+
grants.append(DEVICE_CODE_GRANT_TYPE)
|
|
93
|
+
if deployment.flag_enabled("enable_rfc8693"):
|
|
94
|
+
grants.append(TOKEN_EXCHANGE_GRANT_TYPE)
|
|
95
|
+
return tuple(dict.fromkeys(grants))
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _allowed_client_auth_methods(
|
|
99
|
+
deployment: OAuthDeploymentProfile,
|
|
100
|
+
) -> tuple[str, ...]:
|
|
101
|
+
if _is_fapi_profile(deployment):
|
|
102
|
+
return ("private_key_jwt", "tls_client_auth", "self_signed_tls_client_auth")
|
|
103
|
+
methods: list[str] = [
|
|
104
|
+
"client_secret_basic",
|
|
105
|
+
"client_secret_post",
|
|
106
|
+
"private_key_jwt",
|
|
107
|
+
]
|
|
108
|
+
if deployment.flag_enabled("enable_rfc8705") or _is_hardened_profile(deployment):
|
|
109
|
+
methods.extend(["tls_client_auth", "self_signed_tls_client_auth"])
|
|
110
|
+
return tuple(dict.fromkeys(methods))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def runtime_security_profile(
|
|
114
|
+
deployment: OAuthDeploymentProfile,
|
|
115
|
+
) -> RuntimeSecurityProfile:
|
|
116
|
+
hardened = _is_hardened_profile(deployment)
|
|
117
|
+
fapi_mode = _is_fapi_profile(deployment)
|
|
118
|
+
enabled = deployment.flag_enabled("enable_rfc9700") if hardened else False
|
|
119
|
+
dpop_supported = deployment.flag_enabled("enable_rfc9449")
|
|
120
|
+
mtls_supported = deployment.flag_enabled("enable_rfc8705") or hardened
|
|
121
|
+
return RuntimeSecurityProfile(
|
|
122
|
+
profile=deployment.profile,
|
|
123
|
+
enabled=enabled,
|
|
124
|
+
fapi_mode=fapi_mode,
|
|
125
|
+
oauth21_alignment_mode=str(
|
|
126
|
+
deployment.flags.get("oauth21_alignment_mode", "tracked")
|
|
127
|
+
),
|
|
128
|
+
require_tls=bool(deployment.flags.get("require_tls", True)),
|
|
129
|
+
pkce_required=deployment.flag_enabled("enable_rfc7636"),
|
|
130
|
+
pkce_required_for_all_clients=deployment.flag_enabled("enable_rfc7636")
|
|
131
|
+
and hardened,
|
|
132
|
+
pkce_s256_required=hardened,
|
|
133
|
+
implicit_hybrid_allowed=not hardened,
|
|
134
|
+
password_grant_allowed=not hardened,
|
|
135
|
+
par_required=enabled and deployment.flag_enabled("enable_rfc9126"),
|
|
136
|
+
par_client_auth_required=fapi_mode,
|
|
137
|
+
par_redirect_uri_required=fapi_mode,
|
|
138
|
+
request_uri_max_lifetime_seconds=599 if fapi_mode else 600,
|
|
139
|
+
minimal_frontchannel_authorization=fapi_mode,
|
|
140
|
+
sender_constraint_required=enabled and (dpop_supported or mtls_supported),
|
|
141
|
+
dpop_supported=dpop_supported,
|
|
142
|
+
mtls_supported=mtls_supported,
|
|
143
|
+
allowed_client_auth_methods=_allowed_client_auth_methods(deployment),
|
|
144
|
+
resource_indicators_supported=deployment.flag_enabled("enable_rfc8707"),
|
|
145
|
+
rich_authorization_requests_supported=deployment.flag_enabled("enable_rfc9396"),
|
|
146
|
+
request_objects_supported=deployment.flag_enabled("enable_rfc9101"),
|
|
147
|
+
issuer_identification_supported=deployment.flag_enabled("enable_rfc9207"),
|
|
148
|
+
authorization_response_iss_required=fapi_mode
|
|
149
|
+
or deployment.flag_enabled("enable_rfc9207"),
|
|
150
|
+
query_bearer_disabled=not bool(
|
|
151
|
+
deployment.flags.get("enable_rfc6750_query", False)
|
|
152
|
+
),
|
|
153
|
+
form_bearer_disabled=not bool(
|
|
154
|
+
deployment.flags.get("enable_rfc6750_form", False)
|
|
155
|
+
),
|
|
156
|
+
allowed_response_types=_base_response_types(deployment),
|
|
157
|
+
allowed_response_modes=_base_response_modes(deployment),
|
|
158
|
+
allowed_grant_types=_base_grant_types(deployment),
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def security_bcp_profile(
|
|
163
|
+
deployment: OAuthDeploymentProfile,
|
|
164
|
+
) -> dict[str, object]:
|
|
165
|
+
return runtime_security_profile(deployment).as_dict()
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def authorization_enforcement_matrix(
|
|
169
|
+
deployments: Sequence[OAuthDeploymentProfile],
|
|
170
|
+
) -> list[dict[str, object]]:
|
|
171
|
+
return [
|
|
172
|
+
runtime_security_profile(deployment).as_dict() for deployment in deployments
|
|
173
|
+
]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def assert_authorization_request_allowed(
|
|
177
|
+
params: dict[str, Any],
|
|
178
|
+
deployment: OAuthDeploymentProfile,
|
|
179
|
+
) -> None:
|
|
180
|
+
policy = runtime_security_profile(deployment)
|
|
181
|
+
if policy.minimal_frontchannel_authorization and params.get(
|
|
182
|
+
"_frontchannel_request"
|
|
183
|
+
):
|
|
184
|
+
extra_keys = {
|
|
185
|
+
key
|
|
186
|
+
for key, value in params.items()
|
|
187
|
+
if value not in (None, "", [], (), {})
|
|
188
|
+
and key not in {"client_id", "request_uri", "_frontchannel_request"}
|
|
189
|
+
and not str(key).startswith("_")
|
|
190
|
+
}
|
|
191
|
+
if extra_keys:
|
|
192
|
+
raise OAuthPolicyViolation(
|
|
193
|
+
"invalid_request",
|
|
194
|
+
"FAPI front-channel authorization requests must be limited to client_id and request_uri",
|
|
195
|
+
)
|
|
196
|
+
response_type = str(params.get("response_type") or "").strip()
|
|
197
|
+
if response_type not in set(policy.allowed_response_types):
|
|
198
|
+
raise OAuthPolicyViolation(
|
|
199
|
+
"unsupported_response_type",
|
|
200
|
+
f"response_type {response_type!r} is not permitted by the active RFC 9700 profile",
|
|
201
|
+
)
|
|
202
|
+
requested_mode = str(params.get("response_mode") or "").strip()
|
|
203
|
+
if requested_mode and requested_mode not in set(policy.allowed_response_modes):
|
|
204
|
+
raise OAuthPolicyViolation(
|
|
205
|
+
"unsupported_response_mode",
|
|
206
|
+
f"response_mode {requested_mode!r} is not permitted by the active RFC 9700 profile",
|
|
207
|
+
)
|
|
208
|
+
if (
|
|
209
|
+
policy.pkce_required_for_all_clients
|
|
210
|
+
and response_type == "code"
|
|
211
|
+
and not params.get("code_challenge")
|
|
212
|
+
):
|
|
213
|
+
raise OAuthPolicyViolation(
|
|
214
|
+
"invalid_request",
|
|
215
|
+
"PKCE is mandatory for authorization_code flows in hardening, FAPI, and peer profiles",
|
|
216
|
+
)
|
|
217
|
+
if policy.pkce_s256_required and params.get("code_challenge_method") not in {
|
|
218
|
+
None,
|
|
219
|
+
"",
|
|
220
|
+
"S256",
|
|
221
|
+
}:
|
|
222
|
+
raise OAuthPolicyViolation(
|
|
223
|
+
"invalid_request",
|
|
224
|
+
"PKCE code_challenge_method must be S256 in hardening, FAPI, and peer profiles",
|
|
225
|
+
)
|
|
226
|
+
if policy.par_required and not params.get("request_uri"):
|
|
227
|
+
raise OAuthPolicyViolation(
|
|
228
|
+
"invalid_request",
|
|
229
|
+
"Pushed authorization requests are mandatory in hardening, FAPI, and peer profiles when RFC 9126 is enabled",
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def assert_token_request_allowed(
|
|
234
|
+
data: dict[str, str],
|
|
235
|
+
deployment: OAuthDeploymentProfile,
|
|
236
|
+
) -> None:
|
|
237
|
+
policy = runtime_security_profile(deployment)
|
|
238
|
+
grant_type = str(data.get("grant_type") or "").strip()
|
|
239
|
+
if grant_type not in set(policy.allowed_grant_types):
|
|
240
|
+
raise OAuthPolicyViolation(
|
|
241
|
+
"unsupported_grant_type",
|
|
242
|
+
f"grant_type {grant_type!r} is not permitted by the active RFC 9700 profile",
|
|
243
|
+
)
|
|
244
|
+
if not policy.password_grant_allowed and grant_type == "password":
|
|
245
|
+
raise OAuthPolicyViolation(
|
|
246
|
+
"unsupported_grant_type",
|
|
247
|
+
"resource owner password credentials grant is disabled in hardening, FAPI, and peer profiles",
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def dpop_proof_from_request(request: Any) -> str | None:
|
|
252
|
+
headers = getattr(request, "headers", {}) or {}
|
|
253
|
+
for name in _DPOP_HEADER_NAMES:
|
|
254
|
+
value = headers.get(name) if hasattr(headers, "get") else None
|
|
255
|
+
if value:
|
|
256
|
+
return str(value)
|
|
257
|
+
return None
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def client_certificate_thumbprint_from_request(request: Any) -> str | None:
|
|
261
|
+
from tba_protocol_oauth.standards.mutual_tls_client_authentication import (
|
|
262
|
+
presented_trusted_certificate_thumbprint,
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
return presented_trusted_certificate_thumbprint(request)
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Mapping
|
|
4
|
+
|
|
5
|
+
from tba_contracts_identity.protocol_configuration import (
|
|
6
|
+
protocol_settings as settings,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
from .security_profile import (
|
|
10
|
+
DEVICE_CODE_GRANT_TYPE,
|
|
11
|
+
RFC9700_SPEC_URL,
|
|
12
|
+
TOKEN_EXCHANGE_GRANT_TYPE,
|
|
13
|
+
OAuthDeploymentProfile,
|
|
14
|
+
OAuthPolicyViolation,
|
|
15
|
+
RuntimeSecurityProfile,
|
|
16
|
+
SenderConstraintResult,
|
|
17
|
+
assert_authorization_request_allowed,
|
|
18
|
+
assert_token_request_allowed,
|
|
19
|
+
authorization_enforcement_matrix,
|
|
20
|
+
client_certificate_thumbprint_from_request,
|
|
21
|
+
dpop_proof_from_request,
|
|
22
|
+
runtime_security_profile,
|
|
23
|
+
security_bcp_profile,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
ML_DSA_65_ALG = "ML-DSA-65"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _pqc_jose_enabled() -> bool:
|
|
30
|
+
configured = (
|
|
31
|
+
str(getattr(settings, "jwt_signing_alg", "") or "").replace("_", "-").upper()
|
|
32
|
+
)
|
|
33
|
+
return bool(getattr(settings, "enable_pqc_jose", False)) or configured in {
|
|
34
|
+
"ML-DSA-65",
|
|
35
|
+
"MLDSA65",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _access_token_signing_algs() -> list[str]:
|
|
40
|
+
algs = ["EdDSA"]
|
|
41
|
+
if _pqc_jose_enabled():
|
|
42
|
+
algs.append(ML_DSA_65_ALG)
|
|
43
|
+
return algs
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def validate_sender_constraint(
|
|
47
|
+
request: Any,
|
|
48
|
+
deployment: OAuthDeploymentProfile,
|
|
49
|
+
*,
|
|
50
|
+
dpop_proof: str | None = None,
|
|
51
|
+
replay_checker: Any | None = None,
|
|
52
|
+
nonce_consumer: Any | None = None,
|
|
53
|
+
) -> SenderConstraintResult:
|
|
54
|
+
policy = runtime_security_profile(deployment)
|
|
55
|
+
cert_thumbprint = client_certificate_thumbprint_from_request(request)
|
|
56
|
+
if dpop_proof and policy.dpop_supported:
|
|
57
|
+
from tba_protocol_oauth.standards.dpop import verify_proof
|
|
58
|
+
|
|
59
|
+
jkt = verify_proof(
|
|
60
|
+
dpop_proof,
|
|
61
|
+
getattr(request, "method", "POST"),
|
|
62
|
+
str(getattr(request, "url", "")),
|
|
63
|
+
replay_checker=replay_checker,
|
|
64
|
+
nonce_consumer=nonce_consumer,
|
|
65
|
+
)
|
|
66
|
+
return SenderConstraintResult(
|
|
67
|
+
mechanism="dpop",
|
|
68
|
+
token_type="DPoP",
|
|
69
|
+
confirmation_claim={"jkt": jkt},
|
|
70
|
+
jkt=jkt,
|
|
71
|
+
)
|
|
72
|
+
if cert_thumbprint and policy.mtls_supported:
|
|
73
|
+
return SenderConstraintResult(
|
|
74
|
+
mechanism="mtls",
|
|
75
|
+
token_type="bearer",
|
|
76
|
+
confirmation_claim={"x5t#S256": cert_thumbprint},
|
|
77
|
+
cert_thumbprint=cert_thumbprint,
|
|
78
|
+
)
|
|
79
|
+
if policy.sender_constraint_required:
|
|
80
|
+
allowed: list[str] = []
|
|
81
|
+
if policy.dpop_supported:
|
|
82
|
+
allowed.append("DPoP proof")
|
|
83
|
+
if policy.mtls_supported:
|
|
84
|
+
allowed.append("mutual-TLS certificate thumbprint")
|
|
85
|
+
detail = " or ".join(allowed) if allowed else "sender-constrained token proof"
|
|
86
|
+
raise OAuthPolicyViolation(
|
|
87
|
+
"invalid_request",
|
|
88
|
+
f"hardening, FAPI, and peer profiles require {detail} at the token issuance boundary",
|
|
89
|
+
)
|
|
90
|
+
return SenderConstraintResult()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
async def validate_sender_constraint_async(
|
|
94
|
+
request: Any,
|
|
95
|
+
deployment: OAuthDeploymentProfile,
|
|
96
|
+
*,
|
|
97
|
+
dpop_proof: str | None = None,
|
|
98
|
+
replay_checker: Any | None = None,
|
|
99
|
+
nonce_consumer: Any | None = None,
|
|
100
|
+
) -> SenderConstraintResult:
|
|
101
|
+
policy = runtime_security_profile(deployment)
|
|
102
|
+
cert_thumbprint = client_certificate_thumbprint_from_request(request)
|
|
103
|
+
if dpop_proof and policy.dpop_supported:
|
|
104
|
+
from tba_protocol_oauth.standards.dpop import verify_proof_async
|
|
105
|
+
|
|
106
|
+
jkt = await verify_proof_async(
|
|
107
|
+
dpop_proof,
|
|
108
|
+
getattr(request, "method", "POST"),
|
|
109
|
+
str(getattr(request, "url", "")),
|
|
110
|
+
replay_checker=replay_checker,
|
|
111
|
+
nonce_consumer=nonce_consumer,
|
|
112
|
+
)
|
|
113
|
+
return SenderConstraintResult(
|
|
114
|
+
mechanism="dpop",
|
|
115
|
+
token_type="DPoP",
|
|
116
|
+
confirmation_claim={"jkt": jkt},
|
|
117
|
+
jkt=jkt,
|
|
118
|
+
)
|
|
119
|
+
if cert_thumbprint and policy.mtls_supported:
|
|
120
|
+
return SenderConstraintResult(
|
|
121
|
+
mechanism="mtls",
|
|
122
|
+
token_type="bearer",
|
|
123
|
+
confirmation_claim={"x5t#S256": cert_thumbprint},
|
|
124
|
+
cert_thumbprint=cert_thumbprint,
|
|
125
|
+
)
|
|
126
|
+
if policy.sender_constraint_required:
|
|
127
|
+
allowed: list[str] = []
|
|
128
|
+
if policy.dpop_supported:
|
|
129
|
+
allowed.append("DPoP proof")
|
|
130
|
+
if policy.mtls_supported:
|
|
131
|
+
allowed.append("mutual-TLS certificate thumbprint")
|
|
132
|
+
detail = " or ".join(allowed) if allowed else "sender-constrained token proof"
|
|
133
|
+
raise OAuthPolicyViolation(
|
|
134
|
+
"invalid_request",
|
|
135
|
+
f"hardening, FAPI, and peer profiles require {detail} at the token issuance boundary",
|
|
136
|
+
)
|
|
137
|
+
return SenderConstraintResult()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def verify_access_token_sender_constraint(
|
|
141
|
+
request: Any,
|
|
142
|
+
token_payload: Mapping[str, Any],
|
|
143
|
+
deployment: OAuthDeploymentProfile,
|
|
144
|
+
*,
|
|
145
|
+
access_token: str | None = None,
|
|
146
|
+
dpop_proof: str | None = None,
|
|
147
|
+
replay_checker: Any | None = None,
|
|
148
|
+
nonce_consumer: Any | None = None,
|
|
149
|
+
) -> SenderConstraintResult:
|
|
150
|
+
"""Validate sender-constrained access-token presentation on resource paths.
|
|
151
|
+
|
|
152
|
+
The helper is intentionally fail-closed for bound tokens but does not reject
|
|
153
|
+
ordinary bearer tokens simply because hardening mode is enabled; that
|
|
154
|
+
issuance-side requirement is enforced at the token boundary.
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
policy = runtime_security_profile(deployment)
|
|
158
|
+
cnf = (
|
|
159
|
+
token_payload.get("cnf")
|
|
160
|
+
if isinstance(token_payload.get("cnf"), Mapping)
|
|
161
|
+
else {}
|
|
162
|
+
)
|
|
163
|
+
jkt = cnf.get("jkt")
|
|
164
|
+
x5t = cnf.get("x5t#S256")
|
|
165
|
+
|
|
166
|
+
if jkt:
|
|
167
|
+
if not policy.dpop_supported:
|
|
168
|
+
raise OAuthPolicyViolation(
|
|
169
|
+
"invalid_token",
|
|
170
|
+
"DPoP-bound token presented while DPoP support is disabled",
|
|
171
|
+
status_code=401,
|
|
172
|
+
)
|
|
173
|
+
if not dpop_proof:
|
|
174
|
+
raise OAuthPolicyViolation(
|
|
175
|
+
"invalid_token",
|
|
176
|
+
"missing DPoP proof for DPoP-bound access token",
|
|
177
|
+
status_code=401,
|
|
178
|
+
)
|
|
179
|
+
from tba_protocol_oauth.standards.dpop import verify_proof
|
|
180
|
+
|
|
181
|
+
try:
|
|
182
|
+
verified_jkt = verify_proof(
|
|
183
|
+
dpop_proof,
|
|
184
|
+
getattr(request, "method", "GET"),
|
|
185
|
+
str(getattr(request, "url", "")),
|
|
186
|
+
jkt=str(jkt),
|
|
187
|
+
access_token=access_token,
|
|
188
|
+
replay_checker=replay_checker,
|
|
189
|
+
nonce_consumer=nonce_consumer,
|
|
190
|
+
)
|
|
191
|
+
except ValueError as exc:
|
|
192
|
+
raise OAuthPolicyViolation(
|
|
193
|
+
"invalid_token", str(exc), status_code=401
|
|
194
|
+
) from exc
|
|
195
|
+
return SenderConstraintResult(
|
|
196
|
+
mechanism="dpop",
|
|
197
|
+
token_type="DPoP",
|
|
198
|
+
confirmation_claim={"jkt": verified_jkt},
|
|
199
|
+
jkt=verified_jkt,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
if dpop_proof:
|
|
203
|
+
raise OAuthPolicyViolation(
|
|
204
|
+
"invalid_token",
|
|
205
|
+
"unexpected DPoP proof for non-DPoP access token",
|
|
206
|
+
status_code=401,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
if x5t:
|
|
210
|
+
if not policy.mtls_supported:
|
|
211
|
+
raise OAuthPolicyViolation(
|
|
212
|
+
"invalid_token",
|
|
213
|
+
"certificate-bound token presented while mTLS support is disabled",
|
|
214
|
+
status_code=401,
|
|
215
|
+
)
|
|
216
|
+
from tba_protocol_oauth.standards.mutual_tls_client_authentication import (
|
|
217
|
+
validate_request_certificate_binding,
|
|
218
|
+
)
|
|
219
|
+
from tba_primitives_identity.errors import InvalidTokenError
|
|
220
|
+
|
|
221
|
+
try:
|
|
222
|
+
cert_thumbprint = validate_request_certificate_binding(
|
|
223
|
+
token_payload, request
|
|
224
|
+
)
|
|
225
|
+
except InvalidTokenError as exc:
|
|
226
|
+
raise OAuthPolicyViolation(
|
|
227
|
+
"invalid_token", str(exc), status_code=401
|
|
228
|
+
) from exc
|
|
229
|
+
return SenderConstraintResult(
|
|
230
|
+
mechanism="mtls",
|
|
231
|
+
token_type="bearer",
|
|
232
|
+
confirmation_claim={"x5t#S256": str(x5t)},
|
|
233
|
+
cert_thumbprint=cert_thumbprint,
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
return SenderConstraintResult()
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
async def verify_access_token_sender_constraint_async(
|
|
240
|
+
request: Any,
|
|
241
|
+
token_payload: Mapping[str, Any],
|
|
242
|
+
deployment: OAuthDeploymentProfile,
|
|
243
|
+
*,
|
|
244
|
+
access_token: str | None = None,
|
|
245
|
+
dpop_proof: str | None = None,
|
|
246
|
+
replay_checker: Any | None = None,
|
|
247
|
+
nonce_consumer: Any | None = None,
|
|
248
|
+
) -> SenderConstraintResult:
|
|
249
|
+
policy = runtime_security_profile(deployment)
|
|
250
|
+
cnf = (
|
|
251
|
+
token_payload.get("cnf")
|
|
252
|
+
if isinstance(token_payload.get("cnf"), Mapping)
|
|
253
|
+
else {}
|
|
254
|
+
)
|
|
255
|
+
jkt = cnf.get("jkt")
|
|
256
|
+
x5t = cnf.get("x5t#S256")
|
|
257
|
+
|
|
258
|
+
if jkt:
|
|
259
|
+
if not policy.dpop_supported:
|
|
260
|
+
raise OAuthPolicyViolation(
|
|
261
|
+
"invalid_token",
|
|
262
|
+
"DPoP-bound token presented while DPoP support is disabled",
|
|
263
|
+
status_code=401,
|
|
264
|
+
)
|
|
265
|
+
if not dpop_proof:
|
|
266
|
+
raise OAuthPolicyViolation(
|
|
267
|
+
"invalid_token",
|
|
268
|
+
"missing DPoP proof for DPoP-bound access token",
|
|
269
|
+
status_code=401,
|
|
270
|
+
)
|
|
271
|
+
from tba_protocol_oauth.standards.dpop import verify_proof_async
|
|
272
|
+
|
|
273
|
+
try:
|
|
274
|
+
verified_jkt = await verify_proof_async(
|
|
275
|
+
dpop_proof,
|
|
276
|
+
getattr(request, "method", "GET"),
|
|
277
|
+
str(getattr(request, "url", "")),
|
|
278
|
+
jkt=str(jkt),
|
|
279
|
+
access_token=access_token,
|
|
280
|
+
replay_checker=replay_checker,
|
|
281
|
+
nonce_consumer=nonce_consumer,
|
|
282
|
+
)
|
|
283
|
+
except ValueError as exc:
|
|
284
|
+
raise OAuthPolicyViolation(
|
|
285
|
+
"invalid_token", str(exc), status_code=401
|
|
286
|
+
) from exc
|
|
287
|
+
return SenderConstraintResult(
|
|
288
|
+
mechanism="dpop",
|
|
289
|
+
token_type="DPoP",
|
|
290
|
+
confirmation_claim={"jkt": verified_jkt},
|
|
291
|
+
jkt=verified_jkt,
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
if dpop_proof:
|
|
295
|
+
raise OAuthPolicyViolation(
|
|
296
|
+
"invalid_token",
|
|
297
|
+
"unexpected DPoP proof for non-DPoP access token",
|
|
298
|
+
status_code=401,
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
if x5t:
|
|
302
|
+
if not policy.mtls_supported:
|
|
303
|
+
raise OAuthPolicyViolation(
|
|
304
|
+
"invalid_token",
|
|
305
|
+
"certificate-bound token presented while mTLS support is disabled",
|
|
306
|
+
status_code=401,
|
|
307
|
+
)
|
|
308
|
+
from tba_protocol_oauth.standards.mutual_tls_client_authentication import (
|
|
309
|
+
validate_request_certificate_binding,
|
|
310
|
+
)
|
|
311
|
+
from tba_primitives_identity.errors import InvalidTokenError
|
|
312
|
+
|
|
313
|
+
try:
|
|
314
|
+
cert_thumbprint = validate_request_certificate_binding(
|
|
315
|
+
token_payload, request
|
|
316
|
+
)
|
|
317
|
+
except InvalidTokenError as exc:
|
|
318
|
+
raise OAuthPolicyViolation(
|
|
319
|
+
"invalid_token", str(exc), status_code=401
|
|
320
|
+
) from exc
|
|
321
|
+
return SenderConstraintResult(
|
|
322
|
+
mechanism="mtls",
|
|
323
|
+
token_type="bearer",
|
|
324
|
+
confirmation_claim={"x5t#S256": str(x5t)},
|
|
325
|
+
cert_thumbprint=cert_thumbprint,
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
return SenderConstraintResult()
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def discovery_policy_metadata(
|
|
332
|
+
deployment: OAuthDeploymentProfile,
|
|
333
|
+
) -> dict[str, object]:
|
|
334
|
+
policy = runtime_security_profile(deployment)
|
|
335
|
+
payload: dict[str, object] = {
|
|
336
|
+
"response_types_supported": list(policy.allowed_response_types),
|
|
337
|
+
"grant_types_supported": list(policy.allowed_grant_types),
|
|
338
|
+
"response_modes_supported": list(policy.allowed_response_modes),
|
|
339
|
+
"code_challenge_methods_supported": ["S256"] if policy.pkce_required else [],
|
|
340
|
+
"token_endpoint_auth_methods_supported": list(
|
|
341
|
+
policy.allowed_client_auth_methods
|
|
342
|
+
),
|
|
343
|
+
}
|
|
344
|
+
if policy.authorization_response_iss_required:
|
|
345
|
+
payload["authorization_response_iss_parameter_supported"] = True
|
|
346
|
+
if policy.dpop_supported:
|
|
347
|
+
payload["dpop_signing_alg_values_supported"] = ["EdDSA"]
|
|
348
|
+
if policy.mtls_supported or policy.sender_constraint_required:
|
|
349
|
+
payload["tls_client_certificate_bound_access_tokens"] = True
|
|
350
|
+
if policy.par_required:
|
|
351
|
+
payload["require_pushed_authorization_requests"] = True
|
|
352
|
+
if policy.request_objects_supported:
|
|
353
|
+
payload["request_parameter_supported"] = True
|
|
354
|
+
payload["request_object_signing_alg_values_supported"] = [
|
|
355
|
+
"HS256",
|
|
356
|
+
"HS384",
|
|
357
|
+
"HS512",
|
|
358
|
+
]
|
|
359
|
+
if policy.resource_indicators_supported:
|
|
360
|
+
payload["resource_parameter_supported"] = True
|
|
361
|
+
if policy.rich_authorization_requests_supported:
|
|
362
|
+
payload["authorization_details_types_supported"] = ["*"]
|
|
363
|
+
if deployment.flag_enabled("enable_rfc9068") or deployment.profile in {
|
|
364
|
+
"production",
|
|
365
|
+
"hardening",
|
|
366
|
+
"fapi2-security",
|
|
367
|
+
"peer-claim",
|
|
368
|
+
}:
|
|
369
|
+
payload["access_token_signing_alg_values_supported"] = (
|
|
370
|
+
_access_token_signing_algs()
|
|
371
|
+
)
|
|
372
|
+
if policy.fapi_mode:
|
|
373
|
+
payload["fapi_profiles_supported"] = ["fapi2-security"]
|
|
374
|
+
return payload
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
__all__ = [
|
|
378
|
+
"RFC9700_SPEC_URL",
|
|
379
|
+
"DEVICE_CODE_GRANT_TYPE",
|
|
380
|
+
"TOKEN_EXCHANGE_GRANT_TYPE",
|
|
381
|
+
"OAuthPolicyViolation",
|
|
382
|
+
"OAuthDeploymentProfile",
|
|
383
|
+
"SenderConstraintResult",
|
|
384
|
+
"RuntimeSecurityProfile",
|
|
385
|
+
"security_bcp_profile",
|
|
386
|
+
"runtime_security_profile",
|
|
387
|
+
"authorization_enforcement_matrix",
|
|
388
|
+
"assert_authorization_request_allowed",
|
|
389
|
+
"assert_token_request_allowed",
|
|
390
|
+
"dpop_proof_from_request",
|
|
391
|
+
"client_certificate_thumbprint_from_request",
|
|
392
|
+
"validate_sender_constraint",
|
|
393
|
+
"validate_sender_constraint_async",
|
|
394
|
+
"verify_access_token_sender_constraint",
|
|
395
|
+
"verify_access_token_sender_constraint_async",
|
|
396
|
+
"discovery_policy_metadata",
|
|
397
|
+
]
|