Skip to content

Commit 01075d6

Browse files
authoredApr 18, 2017
Merge pull request #38 from web-push-libs/feat/pub
feat: Add --applicationServerKey dumper
2 parents 349c12f + b920c87 commit 01075d6

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed
 

‎python/py_vapid/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def b64urldecode(data):
2121
return base64.urlsafe_b64decode(data + "===="[:len(data) % 4])
2222

2323

24+
def b64urlencode(bstring):
25+
return binascii.b2a_base64(
26+
bstring).decode('utf8').replace('\n', '').replace(
27+
'+', '-').replace('/', '_').replace('=', '')
28+
29+
2430
class VapidException(Exception):
2531
"""An exception wrapper for Vapid."""
2632
pass
@@ -205,11 +211,6 @@ def _base_sign(self, claims):
205211
"'sub' is your admin email as a mailto: link.")
206212
return claims
207213

208-
def encode(self, bstring):
209-
return binascii.b2a_base64(
210-
bstring).decode('utf8').replace('\n', '').replace(
211-
'+', '-').replace('/', '_').replace('=', '')
212-
213214
def sign(self, claims, crypto_key=None):
214215
"""Sign a set of claims.
215216
:param claims: JSON object containing the JWT claims to use.
@@ -228,7 +229,7 @@ def sign(self, claims, crypto_key=None):
228229
pubkey = self.public_key.to_string()
229230
if len(pubkey) == 64:
230231
pubkey = b'\04' + pubkey
231-
pkey += self.encode(pubkey)
232+
pkey += b64urlencode(pubkey)
232233
if crypto_key:
233234
crypto_key = crypto_key + ';' + pkey
234235
else:
@@ -257,7 +258,7 @@ def sign(self, claims, crypto_key=None):
257258
"Authorization": "{schema} t={t},k={k}".format(
258259
schema=self._schema,
259260
t=sig,
260-
k=self.encode(pkey)
261+
k=b64urlencode(pkey)
261262
)
262263
}
263264

‎python/py_vapid/main.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import json
88

9-
from py_vapid import Vapid01, Vapid02
9+
from py_vapid import Vapid01, Vapid02, b64urlencode
1010

1111

1212
def main():
@@ -21,6 +21,9 @@ def main():
2121
default=True, action="store_true")
2222
parser.add_argument('--json', help="dump as json",
2323
default=False, action="store_true")
24+
parser.add_argument('--applicationServerKey',
25+
help="show applicationServerKey value",
26+
default=False, action="store_true")
2427
args = parser.parse_args()
2528
Vapid = Vapid01
2629
if args.version2:
@@ -56,6 +59,10 @@ def main():
5659
print("Generating public_key.pem")
5760
vapid.save_public_key('public_key.pem')
5861
claim_file = args.sign
62+
result = dict()
63+
if args.applicationServerKey:
64+
print("Application Server Key = {}\n\n".format(
65+
b64urlencode(vapid.public_key.to_string())))
5966
if claim_file:
6067
if not os.path.exists(claim_file):
6168
print("No {} file found.".format(claim_file))
@@ -82,7 +89,7 @@ def main():
8289
exit
8390
try:
8491
claims = json.loads(open(claim_file).read())
85-
result = vapid.sign(claims)
92+
result.update(vapid.sign(claims))
8693
except Exception as exc:
8794
print("Crap, something went wrong: {}".format(repr(exc)))
8895
raise exc

‎python/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from setuptools import setup, find_packages
55

6-
__version__ = "1.0.0"
6+
__version__ = "1.1.0"
77

88

99
def read_from(file):

0 commit comments

Comments
 (0)
Failed to load comments.