From bd4a38235efc4cccb7dd8adf9cc1fbbadb4490aa Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Sat, 7 Oct 2023 06:42:23 -0400 Subject: [PATCH] Clean up the private key creation and lookup --- app.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 83ebb6f..5a1aeee 100644 --- a/app.py +++ b/app.py @@ -29,18 +29,21 @@ def create_app(config_name): ) public_key_base64 = base64.b64encode(public_key_bytes).decode() - private_key_hex = binascii.hexlify(private_key.private_bytes( + private_key_bytes = private_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() - )).decode() + ) + + private_key_base64 = base64.b64encode(private_key_bytes).decode() + + key = VAPIDKey(public_key=public_key_base64, private_key=private_key_base64) + db.session.add(key) + db.session.commit() except Exception as e: print(f"Error generating VAPID keys: {e}") - key = VAPIDKey(public_key=public_key_base64, private_key=private_key_hex) - db.session.add(key) - db.session.commit() def initialize(): @@ -51,17 +54,14 @@ def create_app(config_name): def send_push_notification(subscription_info, message, vapid_key): result = True try: - private_key_hex = vapid_key.private_key - private_key_der = bytes.fromhex(private_key_hex) - - private_key_base64 = base64.b64encode(private_key_der).decode() + private_key_base64 = vapid_key.private_key webpush( subscription_info=subscription_info, data=json.dumps(message), vapid_private_key=private_key_base64, vapid_claims={ - "sub": "mailto:matthew.raymer@gmail.com" + "sub": "mailto:info@timesafari.org" } )