Several fixes from PR review

This commit is contained in:
Salvatore Ingala
2019-10-23 22:33:34 +08:00
parent e80206c2cd
commit b633c89724
2 changed files with 14 additions and 21 deletions

View File

@@ -13,7 +13,7 @@ from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.serialization import load_pem_public_key from cryptography.hazmat.primitives.serialization import load_pem_public_key
from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.exceptions import InvalidSignature from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
from pisa.logger import Logger from pisa.logger import Logger
from pisa.appointment import Appointment from pisa.appointment import Appointment
@@ -56,11 +56,11 @@ def is_appointment_signature_valid(appointment, signature):
with open(PISA_PUBLIC_KEY, "r") as key_file: with open(PISA_PUBLIC_KEY, "r") as key_file:
pubkey_pem = key_file.read().encode("utf-8") pubkey_pem = key_file.read().encode("utf-8")
pisa_public_key = load_pem_public_key(pubkey_pem, backend=default_backend()) pisa_public_key = load_pem_public_key(pubkey_pem, backend=default_backend())
except cryptography.exceptions.UnsupportedAlgorithm: except UnsupportedAlgorithm:
raise ValueError("Could not unserialize the public key (unsupported algorithm).") raise ValueError("Could not unserialize the public key (unsupported algorithm).")
try: try:
sig_bytes = unhexlify(response_json['signature'].encode('utf-8')) sig_bytes = unhexlify(signature.encode('utf-8'))
data = appointment.to_json().encode("utf-8") data = json.dumps(appointment, sort_keys=True, separators=(',', ':')).encode("utf-8")
pisa_public_key.verify(sig_bytes, data, ec.ECDSA(hashes.SHA256())) pisa_public_key.verify(sig_bytes, data, ec.ECDSA(hashes.SHA256()))
except InvalidSignature: except InvalidSignature:
return False return False
@@ -102,7 +102,7 @@ def add_appointment(args):
appointment = build_appointment(appointment_data.get('tx'), appointment_data.get('tx_id'), appointment = build_appointment(appointment_data.get('tx'), appointment_data.get('tx_id'),
appointment_data.get('start_time'), appointment_data.get('end_time'), appointment_data.get('start_time'), appointment_data.get('end_time'),
appointment_data.get('dispute_delta')) appointment_data.get('dispute_delta'))
appointment_json = json.dumps(sort_keys=True, separators=(',', ':')) appointment_json = json.dumps(appointment, sort_keys=True, separators=(',', ':'))
logger.info("Sending appointment to PISA") logger.info("Sending appointment to PISA")
@@ -127,7 +127,7 @@ def add_appointment(args):
else: else:
error = r.json()['error'] error = r.json()['error']
logger.error("The server returned status code {}, and the following error: {}." logger.error("The server returned status code {}, and the following error: {}."
.format(r.status_code), error) .format(r.status_code, error))
except json.JSONDecodeError: except json.JSONDecodeError:
logger.error("The response was not valid JSON.") logger.error("The response was not valid JSON.")
@@ -139,8 +139,8 @@ def add_appointment(args):
logger.error("Can't connect to pisa API. Server cannot be reached.") logger.error("Can't connect to pisa API. Server cannot be reached.")
except FileNotFoundError: except FileNotFoundError:
logger.error("Pisa's public key file not found. Please check your settings.") logger.error("Pisa's public key file not found. Please check your settings.")
except IOError e: except IOError as e:
logger.error("I/O error({0}): {1}".format(e.errno, e.strerror)) logger.error("I/O error({}): {}".format(e.errno, e.strerror))
else: else:
logger.error("The provided locator is not valid.") logger.error("The provided locator is not valid.")
else: else:
@@ -188,15 +188,11 @@ def build_appointment(tx, tx_id, start_block, end_block, dispute_delta):
blob = Blob(tx, cipher, hash_function) blob = Blob(tx, cipher, hash_function)
encrypted_blob = blob.encrypt(tx_id) encrypted_blob = blob.encrypt(tx_id)
return { appointment = {
'locator': locator, 'locator': locator, 'start_block': start_block, 'end_block': end_block, 'dispute_delta': dispute_delta,
'start_block': start_block, 'encrypted_blob': encrypted_blob, 'cipher': cipher, 'hash_function': hash_function }
'end_block': end_block,
'dispute_delta': dispute_delta, return appointment
'encrypted_blob': encrypted_blob,
'cipher': cipher,
'hash_function': hash_function
}
def check_txid_format(txid): def check_txid_format(txid):

View File

@@ -40,10 +40,7 @@ def add_appointment():
if appointment_added: if appointment_added:
rcode = HTTP_OK rcode = HTTP_OK
response = { response = {"locator": appointment.locator, "signature": hexlify(signature).decode('utf-8')}
"locator": appointment.locator,
"signature": hexlify(signature).decode('utf-8')
}
else: else:
rcode = HTTP_SERVICE_UNAVAILABLE rcode = HTTP_SERVICE_UNAVAILABLE
error = "appointment rejected" error = "appointment rejected"