Browse Source

Clean up the private key creation and lookup

pull/1/head
Matthew Raymer 1 year ago
parent
commit
bd4a38235e
  1. 20
      app.py

20
app.py

@ -29,18 +29,21 @@ def create_app(config_name):
) )
public_key_base64 = base64.b64encode(public_key_bytes).decode() 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, encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8, format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption() 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: except Exception as e:
print(f"Error generating VAPID keys: {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(): def initialize():
@ -51,17 +54,14 @@ def create_app(config_name):
def send_push_notification(subscription_info, message, vapid_key): def send_push_notification(subscription_info, message, vapid_key):
result = True result = True
try: try:
private_key_hex = vapid_key.private_key private_key_base64 = vapid_key.private_key
private_key_der = bytes.fromhex(private_key_hex)
private_key_base64 = base64.b64encode(private_key_der).decode()
webpush( webpush(
subscription_info=subscription_info, subscription_info=subscription_info,
data=json.dumps(message), data=json.dumps(message),
vapid_private_key=private_key_base64, vapid_private_key=private_key_base64,
vapid_claims={ vapid_claims={
"sub": "mailto:matthew.raymer@gmail.com" "sub": "mailto:info@timesafari.org"
} }
) )

Loading…
Cancel
Save