Skip to content

Commit 5b76044

Browse files
committed
Add validator for NodeCountry element
- Adds requirement iso3166 to lookup if a country code is valid (country exists and code is in ISO 3166-1 alpha-2 format) - Adds validator method in eIDASConfig class (since both IdP and SP MUST have the NodeCountry element) - Adds tests related to NodeCountry validation
1 parent da7a62e commit 5b76044

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ install_requires =
5151
pytz
5252
requests >= 1.0.0
5353
six
54+
iso3166
5455

5556

5657
[options.packages.find]

src/saml2/config.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import sys
1010
from functools import partial
11+
from iso3166 import countries
1112

1213
import six
1314

@@ -587,30 +588,47 @@ def assert_declared(cls, error_signal):
587588
return (lambda x: x is not None,
588589
partial(error_signal, message="be declared"))
589590

591+
@staticmethod
592+
def validate_node_country_format(node_country):
593+
try:
594+
return countries.get(node_country).alpha2 == node_country
595+
except KeyError:
596+
return False
597+
590598

591599
class eIDASSPConfig(SPConfig, eIDASConfig):
600+
def get_endpoint_element(self, element):
601+
return getattr(self, "_sp_endpoints", {}).get(element, None)
602+
592603
def validate(self):
593604
validators = [
594605
RuleValidator(
595606
"single_logout_service",
596-
self._sp_endpoints.get("single_logout_service"),
607+
self.get_endpoint_element("single_logout_service"),
597608
*self.assert_not_declared(should_warning)
598609
),
599610
RuleValidator(
600611
"artifact_resolution_service",
601-
self._sp_endpoints.get("artifact_resolution_service"),
612+
self.get_endpoint_element("artifact_resolution_service"),
602613
*self.assert_not_declared(should_warning)
603614
),
604615
RuleValidator(
605616
"manage_name_id_service",
606-
self._sp_endpoints.get("manage_name_id_service"),
617+
self.get_endpoint_element("manage_name_id_service"),
607618
*self.assert_not_declared(should_warning)
608619
),
609620
RuleValidator(
610621
"KeyDescriptor",
611622
self.cert_file or self.encryption_keypairs,
612623
*self.assert_declared(must_error)
613-
)
624+
),
625+
RuleValidator(
626+
"node_country",
627+
getattr(self, "_sp_node_country", None),
628+
self.validate_node_country_format,
629+
partial(must_error,
630+
message="be declared in ISO 3166-1 alpha-2 format")
631+
),
614632
]
615633

616634
for validator in validators:

tests/eidas/test_sp.py

+16
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,19 @@ def test_no_keydescriptor(self, config):
112112

113113
with pytest.raises(ConfigValidationError):
114114
conf.validate()
115+
116+
def test_no_nodecountry(self, config):
117+
del config["service"]["sp"]["node_country"]
118+
conf = eIDASSPConfig()
119+
conf.load(config)
120+
121+
with pytest.raises(ConfigValidationError):
122+
conf.validate()
123+
124+
def test_nodecountry_wrong_format(self, config):
125+
config["service"]["sp"]["node_country"] = "gr"
126+
conf = eIDASSPConfig()
127+
conf.load(config)
128+
129+
with pytest.raises(ConfigValidationError):
130+
conf.validate()

0 commit comments

Comments
 (0)