Skip to content

Commit 64ed196

Browse files
Merge pull request #193 from skoranda/email_and_unspecified_nameid
Support emailAddress and unspecified nameid-formats with the appropriate hashing
2 parents 1ffac81 + 66c321d commit 64ed196

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/satosa/frontends/saml2.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from saml2.config import IdPConfig
1717
from saml2.extension.ui import NAMESPACE as UI_NAMESPACE
1818
from saml2.metadata import create_metadata_string
19-
from saml2.saml import NameID, NAMEID_FORMAT_TRANSIENT, NAMEID_FORMAT_PERSISTENT
19+
from saml2.saml import NameID, NAMEID_FORMAT_TRANSIENT, \
20+
NAMEID_FORMAT_PERSISTENT, NAMEID_FORMAT_EMAILADDRESS, \
21+
NAMEID_FORMAT_UNSPECIFIED
2022
from saml2.samlp import name_id_policy_from_string
2123
from saml2.server import Server
2224

@@ -45,6 +47,10 @@ def saml_name_id_format_to_hash_type(name_format):
4547
"""
4648
if name_format == NAMEID_FORMAT_PERSISTENT:
4749
return UserIdHashType.persistent
50+
elif name_format == NAMEID_FORMAT_EMAILADDRESS:
51+
return UserIdHashType.emailaddress
52+
elif name_format == NAMEID_FORMAT_UNSPECIFIED:
53+
return UserIdHashType.unspecified
4854

4955
return UserIdHashType.transient
5056

@@ -62,6 +68,11 @@ def hash_type_to_saml_name_id_format(hash_type):
6268
return NAMEID_FORMAT_TRANSIENT
6369
elif hash_type is UserIdHashType.persistent:
6470
return NAMEID_FORMAT_PERSISTENT
71+
elif hash_type is UserIdHashType.emailaddress:
72+
return NAMEID_FORMAT_EMAILADDRESS
73+
elif hash_type is UserIdHashType.unspecified:
74+
return NAMEID_FORMAT_UNSPECIFIED
75+
6576
return NAMEID_FORMAT_PERSISTENT
6677

6778

src/satosa/internal_data.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import hashlib
77
from enum import Enum
88

9-
109
class UserIdHashType(Enum):
1110
"""
1211
All different user id hash types
@@ -15,6 +14,8 @@ class UserIdHashType(Enum):
1514
persistent = 2
1615
pairwise = 3
1716
public = 4
17+
emailaddress = 5
18+
unspecified = 6
1819

1920
@classmethod
2021
def from_string(cls, str):
@@ -66,7 +67,8 @@ def hash_type(state):
6667
@staticmethod
6768
def hash_id(salt, user_id, requester, state):
6869
"""
69-
Sets a user id to the internal_response, in the format specified by the internal response
70+
Sets a user id to the internal_response,
71+
in the format specified by the internal response
7072
7173
:type salt: str
7274
:type user_id: str
@@ -83,11 +85,16 @@ def hash_id(salt, user_id, requester, state):
8385
hash_type = UserIdHasher.hash_type(state)
8486
if hash_type == UserIdHashType.transient:
8587
timestamp = datetime.datetime.now().time()
86-
user_id = "{req}{time}{id}".format(req=requester, time=timestamp, id=user_id)
87-
elif hash_type == UserIdHashType.persistent or hash_type == UserIdHashType.pairwise:
88+
user_id = "{req}{time}{id}".format(req=requester, time=timestamp,
89+
id=user_id)
90+
elif (hash_type == UserIdHashType.persistent or
91+
hash_type == UserIdHashType.pairwise):
8892
user_id = "{req}{id}".format(req=requester, id=user_id)
8993
elif hash_type == UserIdHashType.public:
9094
user_id = "{id}".format(id=user_id)
95+
elif (hash_type == UserIdHashType.emailaddress or
96+
hash_type == UserIdHashType.unspecified):
97+
return user_id
9198
else:
9299
raise ValueError("Unknown hash type: '{}'".format(hash_type))
93100

tests/satosa/frontends/test_saml2.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
from saml2.entity_category.edugain import COCO
1515
from saml2.entity_category.refeds import RESEARCH_AND_SCHOLARSHIP
1616
from saml2.entity_category.swamid import SFS_1993_1153, RESEARCH_AND_EDUCATION, EU, HEI, NREN
17-
from saml2.saml import NAMEID_FORMAT_PERSISTENT, NAMEID_FORMAT_TRANSIENT
17+
from saml2.saml import NameID, NAMEID_FORMAT_TRANSIENT, \
18+
NAMEID_FORMAT_PERSISTENT, NAMEID_FORMAT_EMAILADDRESS, \
19+
NAMEID_FORMAT_UNSPECIFIED
1820
from saml2.samlp import NameIDPolicy
1921

2022
from satosa.attribute_mapping import AttributeMapper
@@ -358,10 +360,21 @@ def test_load_idp_dynamic_entity_id(self, idp_conf):
358360

359361
class TestSamlNameIdFormatToHashType:
360362
def test_should_default_to_transient(self):
361-
assert saml_name_id_format_to_hash_type("foobar") == UserIdHashType.transient
363+
assert (saml_name_id_format_to_hash_type("foobar") ==
364+
UserIdHashType.transient)
362365

363366
def test_should_map_transient(self):
364-
assert saml_name_id_format_to_hash_type(NAMEID_FORMAT_TRANSIENT) == UserIdHashType.transient
367+
assert (saml_name_id_format_to_hash_type(NAMEID_FORMAT_TRANSIENT) ==
368+
UserIdHashType.transient)
365369

366370
def test_should_map_persistent(self):
367-
assert saml_name_id_format_to_hash_type(NAMEID_FORMAT_PERSISTENT) == UserIdHashType.persistent
371+
assert (saml_name_id_format_to_hash_type(NAMEID_FORMAT_PERSISTENT) ==
372+
UserIdHashType.persistent)
373+
374+
def test_should_map_email(self):
375+
assert (saml_name_id_format_to_hash_type(NAMEID_FORMAT_EMAILADDRESS) ==
376+
UserIdHashType.emailaddress)
377+
378+
def test_should_map_unspecified(self):
379+
assert (saml_name_id_format_to_hash_type(NAMEID_FORMAT_UNSPECIFIED) ==
380+
UserIdHashType.unspecified)

0 commit comments

Comments
 (0)