Skip to content

Commit 6c969b8

Browse files
authored
Merge pull request #55 from web-push-libs/rust
feat: rust implementaion
2 parents 923e771 + 763d54d commit 6c969b8

File tree

4 files changed

+522
-3
lines changed

4 files changed

+522
-3
lines changed

python/py_vapid/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ def from_file(cls, private_key_file=None):
127127

128128
@classmethod
129129
def verify(cls, key, auth):
130-
# TODO: add v2 validation
130+
"""Verify a VAPID authorization token.
131+
132+
:param key: base64 serialized public key
133+
:type key: str
134+
:param auth: authorization token
135+
type key: str
136+
137+
"""
131138
tokens = auth.rsplit(' ', 1)[1].rsplit('.', 1)
132139
kp = cls().from_raw_public(key.encode())
133140
return kp.verify_token(
@@ -295,5 +302,25 @@ def sign(self, claims, crypto_key=None):
295302
)
296303
}
297304

305+
@classmethod
306+
def verify(cls, auth):
307+
pref_tok = auth.rsplit(' ', 1)
308+
assert pref_tok[0].lower() == cls._schema, (
309+
"Incorrect schema specified")
310+
parts = {}
311+
for tok in pref_tok[1].split(','):
312+
kv = tok.split('=', 1)
313+
parts[kv[0]] = kv[1]
314+
assert 'k' in parts.keys(), (
315+
"Auth missing public key 'k' value")
316+
assert 't' in parts.keys(), (
317+
"Auth missing token set 't' value")
318+
kp = cls().from_raw_public(parts['k'].encode())
319+
tokens = parts['t'].rsplit('.', 1)
320+
return kp.verify_token(
321+
validation_token=tokens[0].encode(),
322+
verification_token=tokens[1]
323+
)
324+
298325

299326
Vapid = Vapid01

python/py_vapid/tests/test_vapid.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,13 @@ def test_integration(self):
169169
# These values were taken from a test page. DO NOT ALTER!
170170
key = ("BDd3_hVL9fZi9Ybo2UUzA284WG5FZR30_95YeZJsiApwXKpNcF1rRPF3foI"
171171
"iBHXRdJI2Qhumhf6_LFTeZaNndIo")
172-
auth = ("WebPush eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJod"
172+
auth = ("eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJod"
173173
"HRwczovL3VwZGF0ZXMucHVzaC5zZXJ2aWNlcy5tb3ppbGxhLmNvbSIsImV"
174174
"4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
175175
"W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
176176
"4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZolQA")
177-
ok_(Vapid01.verify(key=key, auth=auth))
177+
ok_(Vapid01.verify(key=key, auth="webpush {}".format(auth)))
178+
ok_(Vapid02.verify(auth="vapid t={},k={}".format(auth, key)))
178179

179180
def test_bad_integration(self):
180181
# These values were taken from a test page. DO NOT ALTER!

rust/vapid/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "vapid"
3+
version = "0.1.0"
4+
authors = ["jrconlin <[email protected]>"]
5+
6+
[dependencies]
7+
openssl = "0.9.16"
8+
serde_json = "1.0.2"
9+
base64 = "0.6.0"
10+
time = "0.1"

0 commit comments

Comments
 (0)