diff --git a/.gitignore b/.gitignore index 83d0c5f8c2906e5158ef5082d4d0820f17910c92..27bbec0a050c299c0ef9f98ed5e252c8365ed506 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ bin/ lib/ pyvenv.cfg +__pycache__ meta.crt meta.key *.xml diff --git a/mdproxy.py b/mdproxy.py index eaab998897a103fa96ea1d8bd1bcc2566b9ffac3..28816574661663a802f31161da61f337b0888c49 100755 --- a/mdproxy.py +++ b/mdproxy.py @@ -8,24 +8,14 @@ from urllib.parse import unquote from dateutil import parser, tz from datetime import datetime +from utils import hasher, Entity + app = Flask(__name__) # Find all IdP's in edugain metadata cached = {} -signer = 'http://localhost:5001/sign' - - -class Entity(object): - md = None - valid_until = 0 - -def hasher(entity_id): - sha1 = hashlib.sha1() - sha1.update(entity_id.encode()) - sha1_digest = sha1.hexdigest() - sha1_identifier = sha1_digest - return sha1_identifier +signer_url = 'http://localhost:5001/sign' @app.route('/cache/<path:eid>', methods=['GET']) @@ -43,7 +33,7 @@ def cache(eid): return cached[entityID].md else: print(f"request {entityID}") - result = requests.get(f"{signer}/{{sha1}}{entityID}").text + result = requests.get(f"{signer_url}/{{sha1}}{entityID}").text parsed = ET.fromstring(result) validUntil = parsed.get('validUntil') # cacheDuration = parsed.get('cacheDuration') diff --git a/mdserver.py b/mdserver.py index 12b7d669c86da6487a59b52157022f0055ff08ee..fc6890199744a08c33156ab3df4aa717a0825d88 100755 --- a/mdserver.py +++ b/mdserver.py @@ -1,16 +1,16 @@ #!/usr/bin/env python import sys import copy -import hashlib from lxml import etree as ET -from signxml import XMLSigner from flask import Flask from urllib.parse import unquote from dateutil import parser, tz from datetime import datetime import traceback +from utils import hasher, signer, Entity + app = Flask(__name__) @@ -24,27 +24,9 @@ cert = open("meta.crt").read() key = open("meta.key").read() -class Entity(object): - md = None - valid_until = 0 - - -def hasher(entity_id): - sha1 = hashlib.sha1() - sha1.update(entity_id.encode()) - sha1_digest = sha1.hexdigest() - return sha1_digest - - -def signer(xml): - global cert, key - print(xml) - return XMLSigner().sign(xml, key=key, cert=cert) - - @app.route('/sign/<path:eid>', methods=['GET']) def sign(eid): - global idps, signed + global idps, signed, cert, key entityID = unquote(eid) if entityID[:6] == "{sha1}": entityID = entityID[6:] @@ -60,7 +42,7 @@ def sign(eid): if entityID in idps: try: print(f"sign {entityID}") - signed_element = signer(idps[entityID].md) + signed_element = signer(idps[entityID].md, cert, key) signed_xml = ET.tostring(signed_element, pretty_print=True).decode() signed_entity = Entity() signed_entity.md = signed_xml diff --git a/mdsigner.py b/mdsigner.py index 37556436665a645591098040a094dabd3ab076f5..3a3d29b20cc62d53bf504acd19dbf3036d8f98b5 100755 --- a/mdsigner.py +++ b/mdsigner.py @@ -4,10 +4,10 @@ import copy from concurrent.futures import ThreadPoolExecutor from lxml import etree as ET -from signxml import XMLSigner, XMLVerifier -import hashlib # import traceback +from .utils import hasher, signer + # Find all IdP's in edugain metadata idps = [] @@ -20,16 +20,14 @@ key = open("meta.key").read() def sign(xml, name): - global success, failed, cert + global success, failed, cert, key # print("Signer") try: - sha1 = hashlib.sha1() - sha1.update(name.encode('utf-8')) - sha1d = sha1.hexdigest() - signed = XMLSigner().sign(xml, key=key, cert=cert) + sha1 = hasher(name) + signed = signer(xml, cert, key) out = ET.tostring(signed, pretty_print=True).decode() # XMLVerifier().verify(out, x509_cert=cert) - with open(f'output/{sha1d}.xml', 'w') as f: + with open(f'output/{sha1}.xml', 'w') as f: f.write(out) success += 1 except Exception as e: diff --git a/utils.py b/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..23bf931e78ddea080ed2623ca9adfcccd1eec585 --- /dev/null +++ b/utils.py @@ -0,0 +1,18 @@ +import hashlib +from signxml import XMLSigner + + +class Entity(object): + md = None + valid_until = 0 + + +def hasher(entity_id): + sha1 = hashlib.sha1() + sha1.update(entity_id.encode()) + sha1_digest = sha1.hexdigest() + return sha1_digest + + +def signer(xml, cert, key): + return XMLSigner().sign(xml, key=key, cert=cert)