"Missing 'aud' from claims" error when sending Web Push notifications with VAPID in Python

23 Views Asked by At

StackOverflow!

I'm attempting to send Web Push notifications from my Flask application using the pywebpush and py_vapid libraries for authentication with VAPID keys, but I'm encountering an error claiming that the aud field is missing from my VAPID claims.

Here is the code I'm using to send the notification:

from pywebpush import webpush, WebPushException

vapid_private_key_pem = "KE0RL_B2SY1AVcpHRWgTzC68or6cLC4RZaHmkM4ZsLk"

def send_push_notification(subscription_info, data):
    vapid_claims = {
        "sub": "mailto:[email protected]",
        "aud": "https://chat.bitsquad.ru/"
    }

    try:
        response = webpush(
            subscription_info=subscription_info,
            data=data,
            vapid_private_key=vapid_private_key_pem,  
            vapid_claims=vapid_claims
        )
        print("Push sent successfully: ", response)
        return True
    except WebPushException as ex:
        print("Web push failure: ", repr(ex))
        return False

When attempting to execute this function, I receive the following error:

ValueError: Missing 'aud' from claims. 'aud' is the scheme, host and optional port for this transaction e.g. https://example.com:8080

To diagnose and correct the problem, I took several steps:

  1. Double-checking VAPID claims: I re-checked the format and content of vapid_claims, ensuring that aud, sub, and other necessary fields accurately met the specifications.

  2. Updating libraries: I made sure I was using the latest versions of the pywebpush and py_vapid libraries to eliminate the possibility of errors due to outdated software.

  3. Logging: Directly before sending the notification, I added logging for key variables, including vapid_claims and subscription_info, to ensure their correctness.

  4. Checking the private key format: I re-verified the format of my VAPID private key, ensuring it met the expected standards and was correctly used in the code.

Unfortunately, despite all my efforts, the "Missing 'aud' from claims" issue persists, and I'm reaching

0

There are 0 best solutions below