From ad6b04bd785d2c78ecc9963e21c7f0dcd12ab7e7 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Sat, 14 Dec 2019 08:31:23 +0700 Subject: [PATCH] LGTM fixes --- powerdnsadmin/lib/utils.py | 3 --- powerdnsadmin/services/saml.py | 33 +++++++++++++++------------------ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/powerdnsadmin/lib/utils.py b/powerdnsadmin/lib/utils.py index 9c34574..af8dd9b 100644 --- a/powerdnsadmin/lib/utils.py +++ b/powerdnsadmin/lib/utils.py @@ -1,4 +1,3 @@ -import os import re import json import requests @@ -9,8 +8,6 @@ from distutils.version import StrictVersion from urllib.parse import urlparse from datetime import datetime, timedelta -from .certutil import KEY_FILE, CERT_FILE - def auth_from_url(url): auth = None diff --git a/powerdnsadmin/services/saml.py b/powerdnsadmin/services/saml.py index 00b3652..0d9276c 100644 --- a/powerdnsadmin/services/saml.py +++ b/powerdnsadmin/services/saml.py @@ -10,37 +10,34 @@ class SAML(object): if current_app.config['SAML_ENABLED']: from onelogin.saml2.auth import OneLogin_Saml2_Auth from onelogin.saml2.idp_metadata_parser import OneLogin_Saml2_IdPMetadataParser - idp_timestamp = datetime(1970, 1, 1) - idp_data = None + self.idp_timestamp = datetime.now() + self.idp_data = None if 'SAML_IDP_ENTITY_ID' in current_app.config: - idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote( + self.idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote( current_app.config['SAML_METADATA_URL'], entity_id=current_app.config.get('SAML_IDP_ENTITY_ID', None), required_sso_binding=current_app. config['SAML_IDP_SSO_BINDING']) else: - idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote( + self.idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote( current_app.config['SAML_METADATA_URL'], entity_id=current_app.config.get('SAML_IDP_ENTITY_ID', None)) - if idp_data is None: + if self.idp_data is None: current_app.logger.info( 'SAML: IDP Metadata initial load failed') exit(-1) - idp_timestamp = datetime.now() - def get_idp_data(): - global idp_data, idp_timestamp + def get_idp_data(self): lifetime = timedelta( minutes=current_app.config['SAML_METADATA_CACHE_LIFETIME']) - if idp_timestamp + lifetime < datetime.now(): - background_thread = Thread(target=retrieve_idp_data) + if self.idp_timestamp + lifetime < datetime.now(): + background_thread = Thread(target=self.retrieve_idp_data) background_thread.start() - return idp_data + return self.idp_data - def retrieve_idp_data(): - global idp_data, idp_timestamp + def retrieve_idp_data(self): if 'SAML_IDP_SSO_BINDING' in current_app.config: new_idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote( current_app.config['SAML_METADATA_URL'], @@ -52,8 +49,8 @@ class SAML(object): current_app.config['SAML_METADATA_URL'], entity_id=current_app.config.get('SAML_IDP_ENTITY_ID', None)) if new_idp_data is not None: - idp_data = new_idp_data - idp_timestamp = datetime.now() + self.idp_data = new_idp_data + self.idp_timestamp = datetime.now() current_app.logger.info( "SAML: IDP Metadata successfully retrieved from: " + current_app.config['SAML_METADATA_URL']) @@ -61,7 +58,7 @@ class SAML(object): current_app.logger.info( "SAML: IDP Metadata could not be retrieved") - def prepare_flask_request(request): + def prepare_flask_request(self, request): # If server is behind proxys or balancers use the HTTP_X_FORWARDED fields url_data = urlparse(request.url) return { @@ -76,14 +73,14 @@ class SAML(object): 'query_string': request.query_string } - def init_saml_auth(req): + def init_saml_auth(self, req): own_url = '' if req['https'] == 'on': own_url = 'https://' else: own_url = 'http://' own_url += req['http_host'] - metadata = get_idp_data() + metadata = self.get_idp_data() settings = {} settings['sp'] = {} if 'SAML_NAMEID_FORMAT' in current_app.config: