1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
from contextlib import closing
import pytest
from pathutils import full_path
from saml2 import config
from saml2 import sigver
from saml2.assertion import Policy
from saml2.attribute_converter import ac_factory
from saml2.extension import mdattr
from saml2.md import RequestedAttribute
from saml2.mdie import to_dict
from saml2.mdstore import MetadataStore
from saml2.saml import NAME_FORMAT_URI
from saml2.server import Server
ATTRCONV = ac_factory(full_path("attributemaps"))
sec_config = config.Config()
sec_config.xmlsec_binary = sigver.get_xmlsec_binary(["/opt/local/bin"])
__author__ = "rolandh"
MDS = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
MDS.imp([{"class": "saml2.mdstore.MetaDataMD", "metadata": [(full_path("swamid.md"),)]}])
def _eq(l1, l2):
return set(l1) == set(l2)
def test_filter_ava():
policy_conf = {
"default": {
"lifetime": {"minutes": 15},
# "attribute_restrictions": None # means all I have
"entity_categories": ["swamid"],
}
}
policy = Policy(policy_conf, MDS)
ava = {"givenName": ["Derek"], "sn": ["Jeter"], "mail": ["derek@nyy.mlb.com", "dj@example.com"], "c": ["USA"]}
ava = policy.filter(ava, "https://connect.sunet.se/shibboleth")
assert _eq(list(ava.keys()), ["mail", "givenName", "sn", "c"])
assert _eq(ava["mail"], ["derek@nyy.mlb.com", "dj@example.com"])
def test_filter_ava2():
policy_conf = {
"default": {
"lifetime": {"minutes": 15},
# "attribute_restrictions": None # means all I have
"entity_categories": ["refeds", "edugain"],
}
}
policy = Policy(policy_conf, MDS)
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
}
ava = policy.filter(ava, "https://connect.sunet.se/shibboleth")
# Mismatch, policy deals with eduGAIN, metadata says SWAMID
# So only minimum should come out
assert _eq(list(ava.keys()), ["eduPersonTargetedID"])
def test_filter_ava3():
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_cat_sfs_hei.xml"),)]}])
policy_conf = {
"default": {
"lifetime": {"minutes": 15},
# "attribute_restrictions": None # means all I have
"entity_categories": ["swamid"],
}
}
policy = Policy(policy_conf, mds)
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
"norEduPersonNIN": "19800101134",
}
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp")
assert _eq(list(ava.keys()), ["norEduPersonNIN"])
def test_filter_ava4():
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_cat_re_nren.xml"),)]}])
policy_conf = {
"default": {
"lifetime": {"minutes": 15},
# "attribute_restrictions": None # means all I have
"entity_categories": ["swamid"],
}
}
policy = Policy(policy_conf, mds)
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
"norEduPersonNIN": "19800101134",
}
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp")
assert _eq(list(ava.keys()), ["givenName", "c", "mail", "sn"])
def test_filter_ava5():
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_cat_re.xml"),)]}])
policy = Policy(
{
"default": {
"lifetime": {"minutes": 15},
# "attribute_restrictions": None # means all I have
"entity_categories": ["swamid"],
}
},
mds,
)
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
"norEduPersonNIN": "19800101134",
}
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp")
assert _eq(list(ava.keys()), [])
def test_idp_policy_filter():
with closing(Server("idp_conf_ec")) as idp:
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
"norEduPersonNIN": "19800101134",
}
policy = idp.config.getattr("policy", "idp")
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp")
# because no entity category
assert list(ava.keys()) == ["eduPersonTargetedID"]
def test_entity_category_import_from_path():
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
# The file entity_cat_rs.xml contains the SAML metadata for an SP
# tagged with the REFEDs R&S entity category.
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_cat_rs.xml"),)]}])
# The entity category module myentitycategory.py is in the tests
# directory which is on the standard module search path.
# The module uses a custom interpretation of the REFEDs R&S entity category
# by adding eduPersonUniqueId.
policy = Policy({"default": {"lifetime": {"minutes": 15}, "entity_categories": ["myentitycategory"]}}, mds)
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"displayName": "Derek Jeter",
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
"eduPersonUniqueId": "R13ET7UD68K0HGR153KE@my.org",
"eduPersonScopedAffiliation": "member@my.org",
"eduPersonPrincipalName": "user01@my.org",
"norEduPersonNIN": "19800101134",
}
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp")
# We expect c and norEduPersonNIN to be filtered out since they are not
# part of the custom entity category.
assert _eq(
list(ava.keys()),
[
"eduPersonTargetedID",
"eduPersonPrincipalName",
"eduPersonUniqueId",
"displayName",
"givenName",
"eduPersonScopedAffiliation",
"mail",
"sn",
],
)
def test_filter_ava_required_attributes_with_no_friendly_name():
entity_id = "https://no-friendly-name.example.edu/saml2/metadata/"
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_no_friendly_name_sp.xml"),)]}])
policy_conf = {"default": {"lifetime": {"minutes": 15}, "entity_categories": ["swamid"]}}
policy = Policy(policy_conf, mds)
ava = {
"givenName": ["Derek"],
"sn": ["Jeter"],
"mail": ["derek@nyy.mlb.com"],
"c": ["USA"],
"eduPersonTargetedID": "foo!bar!xyz",
"norEduPersonNIN": "19800101134",
}
attribute_requirements = mds.attribute_requirement(entity_id)
required = attribute_requirements.get("required", [])
optional = attribute_requirements.get("optional", [])
# ensure the requirements define the eduPersonTargetedID
# without the friendlyName attribute
oid_eptid = "urn:oid:1.3.6.1.4.1.5923.1.1.1.10"
requested_attribute_eptid = RequestedAttribute(name=oid_eptid, name_format=NAME_FORMAT_URI, is_required="true")
assert required == [to_dict(requested_attribute_eptid, onts=[mdattr])]
ava = policy.filter(ava, entity_id, required=required, optional=optional)
assert _eq(list(ava.keys()), ["eduPersonTargetedID"])
def test_filter_ava_esi_coco():
entity_id = "https://esi-coco.example.edu/saml2/metadata/"
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_esi_and_coco_sp.xml"),)]}])
policy_conf = {"default": {"lifetime": {"minutes": 15}, "entity_categories": ["swamid"]}}
policy = Policy(policy_conf, mds)
ava = {
"givenName": ["Test"],
"sn": ["Testsson"],
"mail": ["test@example.com"],
"c": ["SE"],
"schacHomeOrganization": ["example.com"],
"eduPersonScopedAffiliation": ["student@example.com"],
"schacPersonalUniqueCode": [
"urn:schac:personalUniqueCode:int:esi:ladok.se:externtstudentuid-00000000-1111-2222-3333-444444444444"
],
}
requested_attributes = [
{
"friendly_name": "eduPersonScopedAffiliation",
"name": "1.3.6.1.4.1.5923.1.1.1.9",
"name_format": NAME_FORMAT_URI,
"is_required": "true",
},
{
"friendly_name": "schacHomeOrganization",
"name": "1.3.6.1.4.1.25178.1.2.9",
"name_format": NAME_FORMAT_URI,
"is_required": "true",
},
]
ava = policy.filter(ava, entity_id, required=requested_attributes)
assert _eq(list(ava.keys()), ["eduPersonScopedAffiliation", "schacHomeOrganization", "schacPersonalUniqueCode"])
assert _eq(ava["eduPersonScopedAffiliation"], ["student@example.com"])
assert _eq(ava["schacHomeOrganization"], ["example.com"])
assert _eq(
ava["schacPersonalUniqueCode"],
["urn:schac:personalUniqueCode:int:esi:ladok.se:externtstudentuid-00000000-1111-2222-3333-444444444444"],
)
@pytest.mark.skip("Temporarily disabled")
def test_filter_ava_refeds_anonymous_access():
entity_id = "https://anonymous.example.edu/saml2/metadata/"
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_anonymous_sp.xml"),)]}])
policy_conf = {"default": {"lifetime": {"minutes": 15}, "entity_categories": ["swamid"]}}
policy = Policy(policy_conf, mds)
ava = {
"displayName": ["Test Testsson"],
"eduPersonAssurance": ["http://www.swamid.se/policy/assurance/al1"],
"eduPersonScopedAffiliation": ["student@example.com"],
"eduPersonTargetedID": "foo!bar!xyz",
"givenName": ["Test"],
"mail": ["test@example.com"],
"pairwise-id": ["pairwise-id@example.com"],
"schacHomeOrganization": ["example.com"],
"sn": ["Testsson"],
"subject-id": ["subject-id@example.com"],
}
ava = policy.filter(ava, entity_id)
assert _eq(list(ava.keys()), ["eduPersonScopedAffiliation", "schacHomeOrganization"])
assert _eq(ava["eduPersonScopedAffiliation"], ["student@example.com"])
assert _eq(ava["schacHomeOrganization"], ["example.com"])
@pytest.mark.skip("Temporarily disabled")
def test_filter_ava_refeds_pseudonymous_access():
entity_id = "https://pseudonymous.example.edu/saml2/metadata/"
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_pseudonymous_sp.xml"),)]}])
policy_conf = {"default": {"lifetime": {"minutes": 15}, "entity_categories": ["swamid"]}}
policy = Policy(policy_conf, mds)
ava = {
"displayName": ["Test Testsson"],
"eduPersonAssurance": ["http://www.swamid.se/policy/assurance/al1"],
"eduPersonScopedAffiliation": ["student@example.com"],
"eduPersonTargetedID": "foo!bar!xyz",
"givenName": ["Test"],
"mail": ["test@example.com"],
"pairwise-id": ["pairwise-id@example.com"],
"schacHomeOrganization": ["example.com"],
"sn": ["Testsson"],
"subject-id": ["subject-id@example.com"],
}
ava = policy.filter(ava, entity_id)
assert _eq(
list(ava.keys()), ["pairwise-id", "eduPersonScopedAffiliation", "eduPersonAssurance", "schacHomeOrganization"]
)
assert _eq(ava["pairwise-id"], ["pairwise-id@example.com"])
assert _eq(ava["eduPersonScopedAffiliation"], ["student@example.com"])
assert _eq(ava["eduPersonAssurance"], ["http://www.swamid.se/policy/assurance/al1"])
assert _eq(ava["schacHomeOrganization"], ["example.com"])
@pytest.mark.skip("Temporarily disabled")
def test_filter_ava_refeds_personalized_access():
entity_id = "https://personalized.example.edu/saml2/metadata/"
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
mds.imp([{"class": "saml2.mdstore.MetaDataFile", "metadata": [(full_path("entity_personalized_sp.xml"),)]}])
policy_conf = {"default": {"lifetime": {"minutes": 15}, "entity_categories": ["swamid"]}}
policy = Policy(policy_conf, mds)
ava = {
"displayName": ["Test Testsson"],
"eduPersonAssurance": ["http://www.swamid.se/policy/assurance/al1"],
"eduPersonScopedAffiliation": ["student@example.com"],
"eduPersonTargetedID": "foo!bar!xyz",
"givenName": ["Test"],
"mail": ["test@example.com"],
"pairwise-id": ["pairwise-id@example.com"],
"schacHomeOrganization": ["example.com"],
"sn": ["Testsson"],
"subject-id": ["subject-id@example.com"],
}
ava = policy.filter(ava, entity_id)
assert _eq(
list(ava.keys()),
[
"subject-id",
"mail",
"displayName",
"givenName",
"sn",
"eduPersonScopedAffiliation",
"eduPersonAssurance",
"schacHomeOrganization",
],
)
assert _eq(ava["subject-id"], ["subject-id@example.com"])
assert _eq(ava["mail"], ["test@example.com"])
assert _eq(ava["displayName"], ["Test Testsson"])
assert _eq(ava["givenName"], ["Test"])
assert _eq(ava["sn"], ["Testsson"])
assert _eq(ava["eduPersonScopedAffiliation"], ["student@example.com"])
assert _eq(ava["eduPersonAssurance"], ["http://www.swamid.se/policy/assurance/al1"])
assert _eq(ava["schacHomeOrganization"], ["example.com"])
|