Last active
March 2, 2016 22:59
-
-
Save edwindotcom/ef47fdb4e970e394d012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 -S | |
# This Source Code Form is subject to the terms of the Mozilla Public | |
# License, v. 2.0. If a copy of the MPL was not distributed with this | |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
# pip install jws | |
# pip install python-jose | |
# This Source Code Form is subject to the terms of the Mozilla Public | |
# License, v. 2.0. If a copy of the MPL was not distributed with this | |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
import base64 | |
import time | |
import json | |
import ecdsa | |
from jose import jws | |
AUD = "https://people.mozilla.org/~ewong2/push-notification-test/" | |
SUB = "mailto:[email protected]" | |
def make_jwt(header, claims, key): | |
vk = key.get_verifying_key() | |
jwt = jws.sign( | |
claims, | |
key, | |
algorithm=header.get("alg", "ES256")).strip("=") | |
public_key = base64.urlsafe_b64encode( | |
"\x04" + vk.to_string()).strip("=") | |
return (jwt, public_key) | |
def main(): | |
# This is a standard header for all VAPID objects: | |
header = {"typ": "JWT", "alg": "ES256"} | |
# These are our customized claims. | |
claims = {"aud": AUD, | |
"exp": int(time.time()) + 86400, | |
"sub": SUB} | |
my_key = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p) | |
# You can store the private key by writing | |
# my_key.to_pem() to a file. | |
# You can reload the private key by reading | |
# my_key.from_pem(file_contents) | |
(jwt, public_key) = make_jwt(header, claims, my_key) | |
# Return the headers we'll use | |
headers = { | |
"Authorization": "Bearer %s" % jwt, | |
"Crypto-Key": "p256ecdsa=%s" % public_key, | |
} | |
print json.dumps(headers, sort_keys=True, indent=4) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment