Skip to content

Commit 7fff2d2

Browse files
committed
fix: take trust_anchors from parameters
1 parent 5236302 commit 7fff2d2

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

pyeudiw/tests/satosa/test_backend.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import uuid
3+
import copy
34
import base64
45
import datetime
56
import json
@@ -108,12 +109,15 @@ def issue_sd_jwt(specification: dict, settings: dict, issuer_key: JWK, holder_ke
108109
def _mock_auth_callback_function(context: Context, internal_data: InternalData):
109110
return JsonResponse({"response": "Authentication successful"}, status="200")
110111

112+
def base64url_to_int(val):
113+
import base64
114+
import binascii
115+
return int.from_bytes(base64.urlsafe_b64decode(val + '=='), 'big')
111116
class TestOpenID4VPBackend:
112117
@pytest.fixture(autouse=True)
113118
def create_backend(self):
114-
115119
db_engine_inst = DBEngine(CONFIG["storage"])
116-
120+
117121
jwk = {
118122
"kty": "RSA",
119123
"use": "sig",
@@ -129,42 +133,31 @@ def create_backend(self):
129133
"qi": "kn9Etj4a2erCUmoZUQalPjHxCRYm5Q3wAkFIRGSQADA51mkwQHyTYqXbHcmXn2ZgXBVI6XDWJB51Me-NCPfITTlusqxvATF7Q-QJtdK_FbgNtcVRNc1FMq_M7VBHA1i9wJR7T4t57aywfXPmlsA5TToTDRe-ybdw0C3ys4KQATs"
130134
}
131135

132-
def base64url_to_int(val):
133-
import base64
134-
import binascii
135-
return int.from_bytes(base64.urlsafe_b64decode(val + '=='), 'big')
136-
137136
# Extract components from JWK
138-
n = base64url_to_int(jwk['n'])
139-
e = base64url_to_int(jwk['e'])
140-
d = base64url_to_int(jwk['d'])
141-
p = base64url_to_int(jwk['p'])
142-
q = base64url_to_int(jwk['q'])
143-
dp = base64url_to_int(jwk['dp'])
144-
dq = base64url_to_int(jwk['dq'])
145-
qi = base64url_to_int(jwk['qi'])
137+
_n = base64url_to_int(jwk['n'])
138+
_e = base64url_to_int(jwk['e'])
139+
_d = base64url_to_int(jwk['d'])
140+
_p = base64url_to_int(jwk['p'])
141+
_q = base64url_to_int(jwk['q'])
142+
_dp = base64url_to_int(jwk['dp'])
143+
_dq = base64url_to_int(jwk['dq'])
144+
_qi = base64url_to_int(jwk['qi'])
146145

147146
# Create RSA private key
148147
private_key = rsa.RSAPrivateNumbers(
149-
p=p,
150-
q=q,
151-
d=d,
152-
dmp1=dp,
153-
dmq1=dq,
154-
iqmp=qi,
155-
public_numbers=rsa.RSAPublicNumbers(e=e, n=n)
148+
p=_p,
149+
q=_q,
150+
d=_d,
151+
dmp1=_dp,
152+
dmq1=_dq,
153+
iqmp=_qi,
154+
public_numbers=rsa.RSAPublicNumbers(e=_e, n=_n)
156155
).private_key()
157156

158157
self.chain = der_list_to_pem_list(gen_chain(leaf_private_key=private_key))
159158
issuer_pem = self.chain[-1]
160159
self.x509_leaf_private_key = jwk
161160

162-
db_engine_inst.add_trust_anchor(
163-
entity_id=ta_ec["iss"],
164-
entity_configuration=ta_ec_signed,
165-
exp=EXP,
166-
)
167-
168161
db_engine_inst.add_trust_anchor(
169162
entity_id="ca.example.com",
170163
entity_configuration=issuer_pem,

pyeudiw/tests/settings.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from pyeudiw.tools.utils import exp_from_now, iat_now
77
from pyeudiw.tests.x509.test_x509 import gen_chain
88

9+
from pyeudiw.tests.federation.base import ta_jwk
10+
911
BASE_URL = "https://example.com"
1012
AUTHZ_PAGE = "example.com"
1113
AUTH_ENDPOINT = "https://example.com/auth"
@@ -216,7 +218,11 @@
216218
"metadata": _METADATA,
217219
"metadata_type": "openid_credential_verifier",
218220
"authority_hints": ["https://trust-anchor.example.org"],
219-
"trust_anchors": ["https://trust-anchor.example.org"],
221+
"trust_anchors": {
222+
"https://trust-anchor.example.org": [
223+
ta_jwk.serialize(private=False),
224+
]
225+
},
220226
"default_sig_alg": "RS256",
221227
"federation_jwks": [
222228
{

pyeudiw/trust/handler/federation.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
from pyeudiw.trust.exceptions import MissingProtocolSpecificJwks, UnknownTrustAnchor
1919
from pyeudiw.trust.handler.interface import TrustHandlerInterface
2020
from pyeudiw.trust.model.trust_source import TrustSourceData, TrustEvaluationType
21+
from pyeudiw.federation.statements import (
22+
get_entity_configurations,
23+
get_entity_statements,
24+
)
2125

2226
from .commons import DEFAULT_HTTPC_PARAMS
2327

@@ -31,7 +35,7 @@ def __init__(
3135
self,
3236
metadata: List[dict],
3337
authority_hints: List[str],
34-
trust_anchors: List[str],
38+
trust_anchors: dict[str, dict[str, str]],
3539
default_sig_alg: str,
3640
federation_jwks: List[dict[str, Union[str, List[str]]]],
3741
trust_marks: List[dict],
@@ -53,7 +57,7 @@ def __init__(
5357
self.metadata_type = metadata_type
5458
self.metadata: dict = metadata
5559
self.authority_hints: List[str] = authority_hints
56-
self.trust_anchors: List[str] = trust_anchors
60+
self.trust_anchors: dict[str, dict[str, str]] = trust_anchors
5761
self.default_sig_alg: str = default_sig_alg
5862
self.federation_jwks: List[dict[str, Union[str, List[str]]]] = federation_jwks
5963
self.trust_marks: List[dict] = trust_marks
@@ -189,23 +193,30 @@ def validate_trust_material(
189193
"Unknown Trust Anchor: can't find 'iss' in the "
190194
f"first entity statement: {_first_statement} "
191195
)
192-
193-
try:
194-
trust_anchor = db_engine.get_trust_anchor(trust_anchor_eid)
195-
except EntryNotFound:
196+
197+
if not trust_anchor_eid in self.trust_anchors:
196198
raise UnknownTrustAnchor(
197199
f"Unknown Trust Anchor: '{trust_anchor_eid}' is not "
198200
"a recognizable Trust Anchor."
199201
)
200-
201-
decoded_ec = decode_jwt_payload(
202-
trust_anchor['federation']['entity_configuration']
203-
)
204-
jwks = decoded_ec.get('jwks', {}).get('keys', [])
202+
203+
if len(self.trust_anchors[trust_anchor_eid]) != 0:
204+
jwks = self.trust_anchors[trust_anchor_eid]
205+
else:
206+
try:
207+
trust_anchor = get_entity_configurations(trust_anchor_eid, self.httpc_params, False)
208+
decoded_ec = decode_jwt_payload(
209+
trust_anchor['federation']['entity_configuration']
210+
)
211+
jwks = decoded_ec.get('jwks', {}).get('keys', [])
212+
except Exception as e:
213+
raise UnknownTrustAnchor(
214+
f"Cannot fetch Trust Anchor '{trust_anchor_eid}' entity configuration: {e}"
215+
) from e
205216

206217
if not jwks:
207218
raise MissingProtocolSpecificJwks(
208-
f"Cannot find any jwks in {decoded_ec}"
219+
f"Cannot find any jwks in for the Trust Anchor '{trust_anchor_eid}'"
209220
)
210221

211222
tc = StaticTrustChainValidator(

0 commit comments

Comments
 (0)