diff --git a/python/py_vapid/__init__.py b/python/py_vapid/__init__.py index bb73655..aa6cf1d 100644 --- a/python/py_vapid/__init__.py +++ b/python/py_vapid/__init__.py @@ -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. diff --git a/python/py_vapid/tests/test_vapid.py b/python/py_vapid/tests/test_vapid.py index 33df4fe..89e03c1 100644 --- a/python/py_vapid/tests/test_vapid.py +++ b/python/py_vapid/tests/test_vapid.py @@ -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:admin@example.com"} result = v.sign(claims, "id=previous") diff --git a/python/requirements.txt b/python/requirements.txt index 0652dbd..d46cb2e 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1,2 +1,2 @@ ecdsa==0.13 -cryptography>=1.8.2,<=2.0.3 +cryptography>=1.8.2 diff --git a/python/setup.py b/python/setup.py index 687cc4f..e306496 100644 --- a/python/setup.py +++ b/python/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages -__version__ = "1.2.6" +__version__ = "1.3.0" def read_from(file):