misc: remove metadata cache as django-mellon has that now (#88579)
gitea/authentic2-auth-fedict/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2024-03-23 18:15:38 +01:00
parent e257c5551d
commit a3753c9bd2
1 changed files with 1 additions and 37 deletions

View File

@ -15,20 +15,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
import hashlib
import logging
import os
import time
import lasso
import mellon.utils as mellon_utils
import requests
from authentic2.a2_rbac.utils import get_default_ou
from authentic2.models import Attribute
from django.conf import settings
from django.core.files.storage import default_storage
from django.utils.encoding import force_bytes, force_text
from mellon.adapters import DefaultAdapter, app_settings
from mellon.adapters import DefaultAdapter
try:
import authentic2.utils.misc as a2_utils_misc
@ -54,36 +48,6 @@ class AuthenticAdapter(DefaultAdapter):
def auth_login(self, request, user):
a2_utils_misc.login(request, user, 'fedict')
def get_identity_providers_setting(self):
providers = app_settings.IDENTITY_PROVIDERS
cache_path = default_storage.path('fedict-cache')
if not os.path.exists(cache_path):
os.makedirs(cache_path)
for idp in providers:
if 'METADATA_URL' in idp and 'METADATA' not in idp:
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):
with open(metadata_cache_filename) as fd:
idp['METADATA'] = force_text(fd.read())
continue
verify_ssl_certificate = mellon_utils.get_setting(idp, 'VERIFY_SSL_CERTIFICATE')
try:
response = requests.get(idp['METADATA_URL'], verify=verify_ssl_certificate, timeout=30)
response.raise_for_status()
except requests.exceptions.RequestException:
if os.path.exists(metadata_cache_filename):
# accept older cache in case of error
with open(metadata_cache_filename) as fd:
idp['METADATA'] = force_text(fd.read())
continue
idp['METADATA'] = response.text
with open(metadata_cache_filename, 'wb') as fd:
fd.write(response.content)
return providers
def lookup_user(self, idp, saml_attributes):
if 'email' in saml_attributes:
# XXX: remove email from received attributes for now, this