diff --git a/src/authentic2_auth_fedict/adapters.py b/src/authentic2_auth_fedict/adapters.py index ee72a76..ae556e8 100644 --- a/src/authentic2_auth_fedict/adapters.py +++ b/src/authentic2_auth_fedict/adapters.py @@ -25,6 +25,7 @@ import requests from django.conf import settings from django.contrib.auth import get_user_model from django.core.files.storage import default_storage +from django.utils.encoding import force_bytes, force_text import lasso @@ -59,12 +60,12 @@ class AuthenticAdapter(DefaultAdapter): os.makedirs(cache_path) for idp in providers: if 'METADATA_URL' in idp and 'METADATA' not in idp: - url_hash = hashlib.sha1(idp['METADATA_URL']).hexdigest() + url_hash = hashlib.sha1(force_bytes(idp['METADATA_URL'])).hexdigest() metadata_cache_filename = os.path.join(cache_path, url_hash) if os.path.exists(metadata_cache_filename): stat_info = os.stat(metadata_cache_filename) if stat_info.st_size and stat_info.st_mtime > (time.time() - 86400): - idp['METADATA'] = open(metadata_cache_filename).read().decode('utf-8') + idp['METADATA'] = force_text(open(metadata_cache_filename).read()) continue verify_ssl_certificate = mellon_utils.get_setting(idp, 'VERIFY_SSL_CERTIFICATE') try: @@ -73,11 +74,11 @@ class AuthenticAdapter(DefaultAdapter): except requests.exceptions.RequestException as e: if os.path.exists(metadata_cache_filename): # accept older cache in case of error - idp['METADATA'] = open(metadata_cache_filename).read().decode('utf-8') + idp['METADATA'] = force_text(open(metadata_cache_filename).read()) continue idp['METADATA'] = response.text - with open(metadata_cache_filename, 'w') as fd: - fd.write(response.text.encode('utf-8')) + with open(metadata_cache_filename, 'wb') as fd: + fd.write(response.content) return providers def lookup_user(self, idp, saml_attributes):