update metadata encoding handling

This commit is contained in:
Frédéric Péters 2020-04-09 20:07:00 +02:00
parent 4c2d2e11e9
commit 79871d20f0
1 changed files with 6 additions and 5 deletions

View File

@ -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):