Skip to content

Commit f1742b0

Browse files
committed
Refactor UserIdHasher hash_data method
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent d0f5552 commit f1742b0

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/satosa/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _auth_resp_finish(self, context, internal_response):
151151
# hash all attribute values individually
152152
if attribute in internal_attributes:
153153
hashed_values = [
154-
UserIdHasher.hash_data(self.config["USER_ID_HASH_SALT"], v)
154+
util.hash_data(self.config["USER_ID_HASH_SALT"], v)
155155
for v in internal_attributes[attribute]
156156
]
157157
internal_attributes[attribute] = hashed_values

src/satosa/internal_data.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
for converting from SAML/OAuth/OpenID connect to the internal representation.
44
"""
55
import datetime
6-
import hashlib
76
from enum import Enum
87

8+
import satosa.util as util
9+
910

1011
class UserIdHashType(Enum):
1112
"""
@@ -57,9 +58,7 @@ def hash_data(salt, value):
5758
:param value: value to hash together with the salt
5859
:return: hash value (SHA512)
5960
"""
60-
data = '{value}{salt}'.format(value=value, salt=salt).encode()
61-
hash = hashlib.sha512(data).hexdigest()
62-
return hash
61+
return util.hash_data(salt, value)
6362

6463
@staticmethod
6564
def hash_type(state):

src/satosa/util.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Python package file for util functions.
33
"""
4+
import hashlib
45
import logging
56
import random
67
import string
@@ -11,6 +12,26 @@
1112
logger = logging.getLogger(__name__)
1213

1314

15+
def hash_data(salt, value, hash_alg=None):
16+
"""
17+
Hashes a value together with a salt with the given hash algorithm.
18+
19+
:type salt: str
20+
:type hash_alg: str
21+
:type value: str
22+
:param salt: hash salt
23+
:param hash_alg: the hash algorithm to use (default: SHA512)
24+
:param value: value to hash together with the salt
25+
:return: hashed value
26+
"""
27+
hash_alg = hash_alg or 'sha512'
28+
hasher = hashlib.new(hash_alg)
29+
hasher.update(salt.encode('utf-8'))
30+
hasher.update(value.encode('utf-8'))
31+
value_hashed = hasher.hexdigest()
32+
return value_hashed
33+
34+
1435
def check_set_dict_defaults(dic, spec):
1536
for path, value in spec.items():
1637
keys = path.split('.')
@@ -38,21 +59,25 @@ def check_set_dict_defaults(dic, spec):
3859
{})
3960
return dic
4061

62+
4163
def dict_set_nested(dic, keys, value):
4264
for key in keys[:-1]:
4365
dic = dic.setdefault(key, {})
4466
dic[keys[-1]] = value
4567

68+
4669
def dict_get_nested(dic, keys):
4770
for key in keys[:-1]:
4871
dic = dic.setdefault(key, {})
4972
return dic[keys[-1]]
5073

74+
5175
def get_dict_defaults(d, *keys):
5276
for key in keys:
53-
d = d.get(key, d.get("", d.get("default", {})))
77+
d = d.get(key, d.get("", d.get("default", {})))
5478
return d
5579

80+
5681
def rndstr(size=16, alphabet=""):
5782
"""
5883
Returns a string of random ascii characters or digits

0 commit comments

Comments
 (0)