Skip to content

Commit

Permalink
feat: add new class method to suss out key from string
Browse files Browse the repository at this point in the history
Closes #59
  • Loading branch information
jrconlin committed Oct 3, 2017
1 parent 26d2465 commit 37d825f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
16 changes: 16 additions & 0 deletions python/py_vapid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ def from_file(cls, private_key_file=None):
logging.error("Could not open private key file: %s", repr(exc))
raise VapidException(exc)

@classmethod
def from_string(cls, private_key):
"""Initialize VAPID using a string containing the private key. This
will try to determine if the key is in RAW or DER format.
:param private_key: String containing the key info
:type private_key: str
"""

pkey = private_key.encode().replace(b"\n", b"")
key = b64urldecode(pkey)
if len(key) == 32:
return cls.from_raw(pkey)
return cls.from_der(pkey)

@classmethod
def verify(cls, key, auth):
"""Verify a VAPID authorization token.
Expand Down
8 changes: 7 additions & 1 deletion python/py_vapid/tests/test_vapid.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ def test_from_raw(self):
v = Vapid01.from_raw(T_RAW)
self.check_keys(v)

def test_from_string(self):
v1 = Vapid01.from_string(T_DER)
v2 = Vapid01.from_string(T_RAW.decode())
self.check_keys(v1)
self.check_keys(v2)

def test_sign_01(self):
v = Vapid01.from_file("/tmp/private")
v = Vapid01.from_string(T_DER)
claims = {"aud": "https://example.com",
"sub": "mailto:[email protected]"}
result = v.sign(claims, "id=previous")
Expand Down
2 changes: 1 addition & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ecdsa==0.13
cryptography>=1.8.2,<=2.0.3
cryptography>=1.8.2
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import setup, find_packages

__version__ = "1.2.6"
__version__ = "1.3.0"


def read_from(file):
Expand Down

0 comments on commit 37d825f

Please sign in to comment.