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,98 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Mapping
|
|
4
|
+
from urllib.parse import unquote
|
|
5
|
+
|
|
6
|
+
from cryptography import x509
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def normalize_presented_pem(value: str | bytes | None) -> bytes | None:
|
|
10
|
+
if value in {None, b"", ""}:
|
|
11
|
+
return None
|
|
12
|
+
text = value.decode("utf-8") if isinstance(value, bytes) else str(value)
|
|
13
|
+
text = unquote(text).replace("\\n", "\n").strip()
|
|
14
|
+
if not text.startswith("-----BEGIN CERTIFICATE-----"):
|
|
15
|
+
return None
|
|
16
|
+
return text.encode("utf-8")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _cert_from_pem(cert_pem: str | bytes) -> x509.Certificate:
|
|
20
|
+
pem = normalize_presented_pem(cert_pem) or (
|
|
21
|
+
cert_pem if isinstance(cert_pem, bytes) else str(cert_pem).encode("utf-8")
|
|
22
|
+
)
|
|
23
|
+
return x509.load_pem_x509_certificate(pem)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def certificate_subject_dn(cert_pem: str | bytes) -> str:
|
|
27
|
+
return _cert_from_pem(cert_pem).subject.rfc4514_string()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def certificate_san_values(cert_pem: str | bytes) -> dict[str, tuple[str, ...]]:
|
|
31
|
+
cert = _cert_from_pem(cert_pem)
|
|
32
|
+
try:
|
|
33
|
+
sans = cert.extensions.get_extension_for_class(
|
|
34
|
+
x509.SubjectAlternativeName
|
|
35
|
+
).value
|
|
36
|
+
except x509.ExtensionNotFound:
|
|
37
|
+
return {"dns": (), "uri": (), "ip": (), "email": ()}
|
|
38
|
+
return {
|
|
39
|
+
"dns": tuple(sans.get_values_for_type(x509.DNSName)),
|
|
40
|
+
"uri": tuple(sans.get_values_for_type(x509.UniformResourceIdentifier)),
|
|
41
|
+
"ip": tuple(str(value) for value in sans.get_values_for_type(x509.IPAddress)),
|
|
42
|
+
"email": tuple(sans.get_values_for_type(x509.RFC822Name)),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _metadata_values(metadata: Mapping[str, Any], key: str) -> tuple[str, ...]:
|
|
47
|
+
value = metadata.get(key)
|
|
48
|
+
if value is None:
|
|
49
|
+
return ()
|
|
50
|
+
if isinstance(value, (list, tuple, set)):
|
|
51
|
+
return tuple(str(item).strip() for item in value if str(item).strip())
|
|
52
|
+
text = str(value).strip()
|
|
53
|
+
return (text,) if text else ()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def registered_certificate_identity_metadata(
|
|
57
|
+
registration_metadata: Mapping[str, Any] | None,
|
|
58
|
+
) -> dict[str, tuple[str, ...]]:
|
|
59
|
+
metadata = dict(registration_metadata or {})
|
|
60
|
+
return {
|
|
61
|
+
"subject_dn": _metadata_values(metadata, "tls_client_auth_subject_dn"),
|
|
62
|
+
"san_dns": _metadata_values(metadata, "tls_client_auth_san_dns"),
|
|
63
|
+
"san_uri": _metadata_values(metadata, "tls_client_auth_san_uri"),
|
|
64
|
+
"san_ip": _metadata_values(metadata, "tls_client_auth_san_ip"),
|
|
65
|
+
"san_email": _metadata_values(metadata, "tls_client_auth_san_email"),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def certificate_matches_registered_identity(
|
|
70
|
+
cert_pem: str | bytes,
|
|
71
|
+
registration_metadata: Mapping[str, Any] | None,
|
|
72
|
+
) -> bool:
|
|
73
|
+
expected = registered_certificate_identity_metadata(registration_metadata)
|
|
74
|
+
if not any(expected.values()):
|
|
75
|
+
return False
|
|
76
|
+
if expected["subject_dn"] and certificate_subject_dn(cert_pem) in set(
|
|
77
|
+
expected["subject_dn"]
|
|
78
|
+
):
|
|
79
|
+
return True
|
|
80
|
+
sans = certificate_san_values(cert_pem)
|
|
81
|
+
return any(
|
|
82
|
+
set(expected[key]).intersection(sans[san_key])
|
|
83
|
+
for key, san_key in (
|
|
84
|
+
("san_dns", "dns"),
|
|
85
|
+
("san_uri", "uri"),
|
|
86
|
+
("san_ip", "ip"),
|
|
87
|
+
("san_email", "email"),
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
__all__ = [
|
|
93
|
+
"certificate_matches_registered_identity",
|
|
94
|
+
"certificate_san_values",
|
|
95
|
+
"certificate_subject_dn",
|
|
96
|
+
"normalize_presented_pem",
|
|
97
|
+
"registered_certificate_identity_metadata",
|
|
98
|
+
]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .endpoint import makeDelegationToken, makeImpersonationToken
|
|
4
|
+
from .runtime import (
|
|
5
|
+
RFC8693_SPEC_URL,
|
|
6
|
+
TOKEN_EXCHANGE_GRANT_TYPE,
|
|
7
|
+
TokenExchangeRequest,
|
|
8
|
+
TokenExchangeResponse,
|
|
9
|
+
TokenType,
|
|
10
|
+
exchange_token,
|
|
11
|
+
validate_subject_token,
|
|
12
|
+
validate_token_exchange_request,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"RFC8693_SPEC_URL",
|
|
17
|
+
"TOKEN_EXCHANGE_GRANT_TYPE",
|
|
18
|
+
"TokenExchangeRequest",
|
|
19
|
+
"TokenExchangeResponse",
|
|
20
|
+
"TokenType",
|
|
21
|
+
"exchange_token",
|
|
22
|
+
"makeDelegationToken",
|
|
23
|
+
"makeImpersonationToken",
|
|
24
|
+
"validate_subject_token",
|
|
25
|
+
"validate_token_exchange_request",
|
|
26
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Deterministic RFC 8693 compatibility helpers; no HTTP carrier ownership."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from tba_protocol_oauth.standards._rfc8693.runtime import (
|
|
6
|
+
TOKEN_EXCHANGE_GRANT_TYPE,
|
|
7
|
+
TokenExchangeRequest,
|
|
8
|
+
TokenExchangeResponse,
|
|
9
|
+
TokenType,
|
|
10
|
+
exchange_token,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def makeImpersonationToken(
|
|
15
|
+
*,
|
|
16
|
+
subject_token: str,
|
|
17
|
+
actor_token: str,
|
|
18
|
+
audience: str | None = None,
|
|
19
|
+
scope: str | None = None,
|
|
20
|
+
issuer: str = "https://issuer.example",
|
|
21
|
+
client_id: str | None = None,
|
|
22
|
+
) -> TokenExchangeResponse:
|
|
23
|
+
request = TokenExchangeRequest(
|
|
24
|
+
grant_type=TOKEN_EXCHANGE_GRANT_TYPE,
|
|
25
|
+
subject_token=subject_token,
|
|
26
|
+
subject_token_type=TokenType.ACCESS_TOKEN.value,
|
|
27
|
+
actor_token=actor_token,
|
|
28
|
+
actor_token_type=TokenType.ACCESS_TOKEN.value,
|
|
29
|
+
audience=audience,
|
|
30
|
+
scope=scope,
|
|
31
|
+
)
|
|
32
|
+
return exchange_token(request, issuer=issuer, client_id=client_id)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def makeDelegationToken(
|
|
36
|
+
*,
|
|
37
|
+
subject_token: str,
|
|
38
|
+
audience: str | None = None,
|
|
39
|
+
scope: str | None = None,
|
|
40
|
+
issuer: str = "https://issuer.example",
|
|
41
|
+
client_id: str | None = None,
|
|
42
|
+
) -> TokenExchangeResponse:
|
|
43
|
+
request = TokenExchangeRequest(
|
|
44
|
+
grant_type=TOKEN_EXCHANGE_GRANT_TYPE,
|
|
45
|
+
subject_token=subject_token,
|
|
46
|
+
subject_token_type=TokenType.ACCESS_TOKEN.value,
|
|
47
|
+
audience=audience,
|
|
48
|
+
scope=scope,
|
|
49
|
+
)
|
|
50
|
+
return exchange_token(request, issuer=issuer, client_id=client_id)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
__all__ = [
|
|
54
|
+
"makeDelegationToken",
|
|
55
|
+
"makeImpersonationToken",
|
|
56
|
+
]
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"""RFC 8693 - OAuth 2.0 Token Exchange.
|
|
2
|
+
|
|
3
|
+
This module provides functionality for OAuth 2.0 Token Exchange as defined
|
|
4
|
+
in RFC 8693. Token exchange allows clients to exchange one type of token
|
|
5
|
+
for another, enabling scenarios like impersonation, delegation, and
|
|
6
|
+
token format conversion.
|
|
7
|
+
See RFC 8693: https://www.rfc-editor.org/rfc/rfc8693
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
from typing import Any, Dict, List, Optional, Union
|
|
14
|
+
from tba_contracts_identity.protocol_configuration import (
|
|
15
|
+
protocol_settings as settings,
|
|
16
|
+
)
|
|
17
|
+
from tba_protocol_oauth.jwtoken import JWTCoder
|
|
18
|
+
from tba_protocol_oauth.standards.json_web_token import decode_jwt, encode_jwt
|
|
19
|
+
from tba_contracts_identity.protocols import TokenType
|
|
20
|
+
|
|
21
|
+
RFC8693_SPEC_URL = "https://www.rfc-editor.org/rfc/rfc8693"
|
|
22
|
+
|
|
23
|
+
# Token Exchange Grant Type
|
|
24
|
+
TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange"
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"RFC8693_SPEC_URL",
|
|
28
|
+
"TOKEN_EXCHANGE_GRANT_TYPE",
|
|
29
|
+
"TokenExchangeRequest",
|
|
30
|
+
"TokenExchangeResponse",
|
|
31
|
+
"TokenType",
|
|
32
|
+
"exchange_token",
|
|
33
|
+
"validate_subject_token",
|
|
34
|
+
"validate_token_exchange_request",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _header_value(value: object) -> str | None:
|
|
39
|
+
if value is None:
|
|
40
|
+
return None
|
|
41
|
+
if isinstance(value, str):
|
|
42
|
+
stripped = value.strip()
|
|
43
|
+
return stripped or None
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TokenExchangeRequest:
|
|
48
|
+
"""Represents a token exchange request per RFC 8693."""
|
|
49
|
+
|
|
50
|
+
def __init__(
|
|
51
|
+
self,
|
|
52
|
+
grant_type: str,
|
|
53
|
+
subject_token: str,
|
|
54
|
+
subject_token_type: str,
|
|
55
|
+
*,
|
|
56
|
+
actor_token: Optional[str] = None,
|
|
57
|
+
actor_token_type: Optional[str] = None,
|
|
58
|
+
resource: Optional[Union[str, List[str]]] = None,
|
|
59
|
+
audience: Optional[Union[str, List[str]]] = None,
|
|
60
|
+
scope: Optional[str] = None,
|
|
61
|
+
requested_token_type: Optional[str] = None,
|
|
62
|
+
):
|
|
63
|
+
"""Initialize a token exchange request.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
grant_type: Must be TOKEN_EXCHANGE_GRANT_TYPE
|
|
67
|
+
subject_token: The token representing the subject
|
|
68
|
+
subject_token_type: Type URI of the subject token
|
|
69
|
+
actor_token: Optional token representing the actor
|
|
70
|
+
actor_token_type: Type URI of the actor token (required if actor_token provided)
|
|
71
|
+
resource: Optional target resource(s)
|
|
72
|
+
audience: Optional target audience(s)
|
|
73
|
+
scope: Optional requested scope
|
|
74
|
+
requested_token_type: Optional requested token type
|
|
75
|
+
"""
|
|
76
|
+
self.grant_type = grant_type
|
|
77
|
+
self.subject_token = subject_token
|
|
78
|
+
self.subject_token_type = subject_token_type
|
|
79
|
+
self.actor_token = actor_token
|
|
80
|
+
self.actor_token_type = actor_token_type
|
|
81
|
+
self.resource = resource
|
|
82
|
+
self.audience = audience
|
|
83
|
+
self.scope = scope
|
|
84
|
+
self.requested_token_type = requested_token_type
|
|
85
|
+
|
|
86
|
+
def validate(self) -> None:
|
|
87
|
+
"""Validate the token exchange request per RFC 8693."""
|
|
88
|
+
if self.grant_type != TOKEN_EXCHANGE_GRANT_TYPE:
|
|
89
|
+
raise ValueError(f"Invalid grant_type: {self.grant_type}")
|
|
90
|
+
|
|
91
|
+
if not self.subject_token:
|
|
92
|
+
raise ValueError("subject_token is required")
|
|
93
|
+
|
|
94
|
+
if not self.subject_token_type:
|
|
95
|
+
raise ValueError("subject_token_type is required")
|
|
96
|
+
|
|
97
|
+
if self.actor_token and not self.actor_token_type:
|
|
98
|
+
raise ValueError(
|
|
99
|
+
"actor_token_type is required when actor_token is provided"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if self.actor_token_type and not self.actor_token:
|
|
103
|
+
raise ValueError(
|
|
104
|
+
"actor_token is required when actor_token_type is provided"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class TokenExchangeResponse:
|
|
109
|
+
"""Represents a token exchange response per RFC 8693."""
|
|
110
|
+
|
|
111
|
+
def __init__(
|
|
112
|
+
self,
|
|
113
|
+
access_token: str,
|
|
114
|
+
token_type: str = "Bearer",
|
|
115
|
+
*,
|
|
116
|
+
expires_in: Optional[int] = None,
|
|
117
|
+
refresh_token: Optional[str] = None,
|
|
118
|
+
scope: Optional[str] = None,
|
|
119
|
+
issued_token_type: Optional[str] = None,
|
|
120
|
+
):
|
|
121
|
+
"""Initialize a token exchange response.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
access_token: The issued access token
|
|
125
|
+
token_type: Token type (typically "Bearer")
|
|
126
|
+
expires_in: Token lifetime in seconds
|
|
127
|
+
refresh_token: Optional refresh token
|
|
128
|
+
scope: Granted scope
|
|
129
|
+
issued_token_type: Type of the issued token
|
|
130
|
+
"""
|
|
131
|
+
self.access_token = access_token
|
|
132
|
+
self.token_type = token_type
|
|
133
|
+
self.expires_in = expires_in
|
|
134
|
+
self.refresh_token = refresh_token
|
|
135
|
+
self.scope = scope
|
|
136
|
+
self.issued_token_type = issued_token_type
|
|
137
|
+
|
|
138
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
139
|
+
"""Convert response to dictionary for JSON serialization."""
|
|
140
|
+
result = {
|
|
141
|
+
"access_token": self.access_token,
|
|
142
|
+
"token_type": self.token_type,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if self.expires_in is not None:
|
|
146
|
+
result["expires_in"] = self.expires_in
|
|
147
|
+
if self.refresh_token:
|
|
148
|
+
result["refresh_token"] = self.refresh_token
|
|
149
|
+
if self.scope:
|
|
150
|
+
result["scope"] = self.scope
|
|
151
|
+
if self.issued_token_type:
|
|
152
|
+
result["issued_token_type"] = self.issued_token_type
|
|
153
|
+
|
|
154
|
+
return result
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def validate_token_exchange_request(
|
|
158
|
+
grant_type: str, subject_token: str, subject_token_type: str, **kwargs
|
|
159
|
+
) -> TokenExchangeRequest:
|
|
160
|
+
"""Validate and create a TokenExchangeRequest.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
grant_type: The grant type
|
|
164
|
+
subject_token: The subject token
|
|
165
|
+
subject_token_type: The subject token type
|
|
166
|
+
**kwargs: Additional optional parameters
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
Validated TokenExchangeRequest instance
|
|
170
|
+
|
|
171
|
+
Raises:
|
|
172
|
+
RuntimeError: If RFC 8693 support is disabled
|
|
173
|
+
ValueError: If validation fails
|
|
174
|
+
"""
|
|
175
|
+
if not settings.enable_rfc8693:
|
|
176
|
+
raise RuntimeError("RFC 8693 support disabled")
|
|
177
|
+
|
|
178
|
+
request = TokenExchangeRequest(
|
|
179
|
+
grant_type=grant_type,
|
|
180
|
+
subject_token=subject_token,
|
|
181
|
+
subject_token_type=subject_token_type,
|
|
182
|
+
**kwargs,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
request.validate()
|
|
186
|
+
return request
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def validate_subject_token(token: str, token_type: str) -> Dict[str, Any]:
|
|
190
|
+
"""Validate a subject token based on its type.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
token: The token to validate
|
|
194
|
+
token_type: The token type URI
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
Token claims/metadata
|
|
198
|
+
|
|
199
|
+
Raises:
|
|
200
|
+
ValueError: If token validation fails
|
|
201
|
+
"""
|
|
202
|
+
if token_type in (TokenType.ACCESS_TOKEN.value, TokenType.JWT.value):
|
|
203
|
+
try:
|
|
204
|
+
return decode_jwt(token)
|
|
205
|
+
except Exception as e:
|
|
206
|
+
raise ValueError(f"Invalid JWT token: {e}")
|
|
207
|
+
elif token_type == TokenType.REFRESH_TOKEN.value:
|
|
208
|
+
# For refresh tokens, we might need different validation
|
|
209
|
+
# This is a placeholder - implement based on your token format
|
|
210
|
+
return {"token_type": "refresh_token", "value": token}
|
|
211
|
+
else:
|
|
212
|
+
# For other token types, basic validation
|
|
213
|
+
if not token.strip():
|
|
214
|
+
raise ValueError("Empty token")
|
|
215
|
+
return {"token_type": token_type, "value": token}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _sign_exchange_access_token(**claims: Any) -> str:
|
|
219
|
+
try:
|
|
220
|
+
return JWTCoder.default().sign(**claims)
|
|
221
|
+
except RuntimeError as exc:
|
|
222
|
+
if "active event loop" not in str(exc):
|
|
223
|
+
raise
|
|
224
|
+
return encode_jwt(**claims)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def exchange_token(
|
|
228
|
+
request: TokenExchangeRequest,
|
|
229
|
+
*,
|
|
230
|
+
issuer: str,
|
|
231
|
+
client_id: Optional[str] = None,
|
|
232
|
+
jkt: str | None = None,
|
|
233
|
+
) -> TokenExchangeResponse:
|
|
234
|
+
"""Perform token exchange per RFC 8693.
|
|
235
|
+
|
|
236
|
+
Args:
|
|
237
|
+
request: Validated token exchange request
|
|
238
|
+
issuer: Token issuer
|
|
239
|
+
client_id: Optional client identifier
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
TokenExchangeResponse with the new token
|
|
243
|
+
|
|
244
|
+
Raises:
|
|
245
|
+
RuntimeError: If RFC 8693 support is disabled
|
|
246
|
+
ValueError: If token exchange fails
|
|
247
|
+
"""
|
|
248
|
+
if not settings.enable_rfc8693:
|
|
249
|
+
raise RuntimeError("RFC 8693 support disabled")
|
|
250
|
+
|
|
251
|
+
# Validate subject token
|
|
252
|
+
subject_claims = validate_subject_token(
|
|
253
|
+
request.subject_token, request.subject_token_type
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
# Validate actor token if provided
|
|
257
|
+
if request.actor_token and request.actor_token_type:
|
|
258
|
+
validate_subject_token(request.actor_token, request.actor_token_type)
|
|
259
|
+
|
|
260
|
+
# Create new token based on request
|
|
261
|
+
# Extract subject from original token
|
|
262
|
+
subject_id = subject_claims.get("sub", "unknown")
|
|
263
|
+
tenant_id = subject_claims.get("tid", "default")
|
|
264
|
+
|
|
265
|
+
# Determine scope - use requested scope or inherit from subject token
|
|
266
|
+
scope = request.scope or subject_claims.get("scope", "")
|
|
267
|
+
|
|
268
|
+
# Create new access token
|
|
269
|
+
extra_claims: Dict[str, Any] = {}
|
|
270
|
+
if jkt:
|
|
271
|
+
extra_claims["cnf"] = {"jkt": jkt}
|
|
272
|
+
access_token = _sign_exchange_access_token(
|
|
273
|
+
sub=subject_id,
|
|
274
|
+
tid=tenant_id,
|
|
275
|
+
scopes=scope.split() if scope else [],
|
|
276
|
+
aud=request.audience,
|
|
277
|
+
**extra_claims,
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
# Determine issued token type
|
|
281
|
+
issued_token_type = request.requested_token_type or TokenType.ACCESS_TOKEN.value
|
|
282
|
+
|
|
283
|
+
return TokenExchangeResponse(
|
|
284
|
+
access_token=access_token,
|
|
285
|
+
token_type="Bearer",
|
|
286
|
+
expires_in=3600, # 1 hour default
|
|
287
|
+
scope=scope,
|
|
288
|
+
issued_token_type=issued_token_type,
|
|
289
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .sender_constraints import (
|
|
4
|
+
DEVICE_CODE_GRANT_TYPE,
|
|
5
|
+
RFC9700_SPEC_URL,
|
|
6
|
+
TOKEN_EXCHANGE_GRANT_TYPE,
|
|
7
|
+
OAuthDeploymentProfile,
|
|
8
|
+
OAuthPolicyViolation,
|
|
9
|
+
RuntimeSecurityProfile,
|
|
10
|
+
SenderConstraintResult,
|
|
11
|
+
assert_authorization_request_allowed,
|
|
12
|
+
assert_token_request_allowed,
|
|
13
|
+
authorization_enforcement_matrix,
|
|
14
|
+
client_certificate_thumbprint_from_request,
|
|
15
|
+
discovery_policy_metadata,
|
|
16
|
+
dpop_proof_from_request,
|
|
17
|
+
runtime_security_profile,
|
|
18
|
+
security_bcp_profile,
|
|
19
|
+
validate_sender_constraint,
|
|
20
|
+
validate_sender_constraint_async,
|
|
21
|
+
verify_access_token_sender_constraint,
|
|
22
|
+
verify_access_token_sender_constraint_async,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"DEVICE_CODE_GRANT_TYPE",
|
|
27
|
+
"RFC9700_SPEC_URL",
|
|
28
|
+
"TOKEN_EXCHANGE_GRANT_TYPE",
|
|
29
|
+
"OAuthDeploymentProfile",
|
|
30
|
+
"OAuthPolicyViolation",
|
|
31
|
+
"RuntimeSecurityProfile",
|
|
32
|
+
"SenderConstraintResult",
|
|
33
|
+
"assert_authorization_request_allowed",
|
|
34
|
+
"assert_token_request_allowed",
|
|
35
|
+
"authorization_enforcement_matrix",
|
|
36
|
+
"client_certificate_thumbprint_from_request",
|
|
37
|
+
"discovery_policy_metadata",
|
|
38
|
+
"dpop_proof_from_request",
|
|
39
|
+
"runtime_security_profile",
|
|
40
|
+
"security_bcp_profile",
|
|
41
|
+
"validate_sender_constraint",
|
|
42
|
+
"validate_sender_constraint_async",
|
|
43
|
+
"verify_access_token_sender_constraint",
|
|
44
|
+
"verify_access_token_sender_constraint_async",
|
|
45
|
+
]
|