From 9ccd0b3f401f55c19692c17ad3ed23c43ef8a5b1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 21 Oct 2018 16:46:27 +0200 Subject: [PATCH 1/4] Annotate jwt.algorithms Cf. #1446 --- third_party/3/jwt/algorithms.pyi | 44 ++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/third_party/3/jwt/algorithms.pyi b/third_party/3/jwt/algorithms.pyi index 4576b4169354..b820adc3d808 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -1,3 +1,43 @@ -from typing import Any +import sys +from hashlib import _Hash +from typing import Any, Set, Dict, Optional, ClassVar, Union, Generic, TypeVar -class Algorithm(Any): ... # type: ignore +requires_cryptography = Set[str] + +def get_default_algorithms() -> Dict[str, Algorithm]: ... + +_K = TypeVar("_K") + +class Algorithm(Generic[_K]): + def prepare_key(self, key: _K) -> Any: ... + def sign(self, msg: bytes, key: _K) -> bytes: ... + def verify(self, msg: bytes, key: _K, sig: bytes) -> bool: ... + @staticmethod + def to_jwk(key_obj: _K) -> str: ... + @staticmethod + def from_jwk(jwk: str) -> _K: ... + +class NoneAlgorithm(Algorithm[None]): + def prepare_key(self, key: Optional[str]) -> None: ... + +class _HashAlg: + def __call__(self, arg: Union[bytes, bytearray, memoryview] = ...) -> _Hash: ... + +if sys.version_info >= (3, 6): + _LoadsString = Union[str, bytes, bytearray] +else: + _LoadsString = str + +class HMACAlgorithm(Algorithm[bytes]): + SHA256: ClassVar[_HashAlg] + SHA384: ClassVar[_HashAlg] + SHA512: ClassVar[_HashAlg] + has_alg: _HashAlg + def __init__(self, _HashAlg) -> None: ... + def prepare_key(self, key: Union[str, bytes]) -> bytes: ... + @staticmethod + def to_jwk(key_obj: Union[str, bytes]) -> Any: ... + @staticmethod + def from_jwk(jwk: _LoadsString) -> bytes: ... + +# If cryptography is installed, also has classes RSAAlgorithm and ECAlgorithm. From 1c7be0142d2db2e2380db7e45116bb6357cb40ad Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 21 Oct 2018 16:47:57 +0200 Subject: [PATCH 2/4] Fix return type of Algorithm.prepare_key() --- third_party/3/jwt/algorithms.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/3/jwt/algorithms.pyi b/third_party/3/jwt/algorithms.pyi index b820adc3d808..ff12e60cc01e 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -9,7 +9,7 @@ def get_default_algorithms() -> Dict[str, Algorithm]: ... _K = TypeVar("_K") class Algorithm(Generic[_K]): - def prepare_key(self, key: _K) -> Any: ... + def prepare_key(self, key: _K) -> _K: ... def sign(self, msg: bytes, key: _K) -> bytes: ... def verify(self, msg: bytes, key: _K, sig: bytes) -> bool: ... @staticmethod From 829a28b59a297113133335585642dd09e064fb9b Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 23 Oct 2018 13:57:10 +0200 Subject: [PATCH 3/4] Fix types and attribute name --- third_party/3/jwt/algorithms.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/third_party/3/jwt/algorithms.pyi b/third_party/3/jwt/algorithms.pyi index ff12e60cc01e..4272bd100981 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -13,9 +13,10 @@ class Algorithm(Generic[_K]): def sign(self, msg: bytes, key: _K) -> bytes: ... def verify(self, msg: bytes, key: _K, sig: bytes) -> bool: ... @staticmethod - def to_jwk(key_obj: _K) -> str: ... + def to_jwk(key_obj: Any) -> str: ... # should be key_obj: _K, see python/mypy#1337 @staticmethod - def from_jwk(jwk: str) -> _K: ... + def from_jwk(jwk: str) -> Any: ... # should return _K, see python/mypy#1337 + class NoneAlgorithm(Algorithm[None]): def prepare_key(self, key: Optional[str]) -> None: ... @@ -32,11 +33,11 @@ class HMACAlgorithm(Algorithm[bytes]): SHA256: ClassVar[_HashAlg] SHA384: ClassVar[_HashAlg] SHA512: ClassVar[_HashAlg] - has_alg: _HashAlg + hash_alg: _HashAlg def __init__(self, _HashAlg) -> None: ... def prepare_key(self, key: Union[str, bytes]) -> bytes: ... @staticmethod - def to_jwk(key_obj: Union[str, bytes]) -> Any: ... + def to_jwk(key_obj: Union[str, bytes]) -> str: ... @staticmethod def from_jwk(jwk: _LoadsString) -> bytes: ... From 59bbeccc2e8120f02d60a23a98f46f7d98920982 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 23 Oct 2018 16:46:30 +0200 Subject: [PATCH 4/4] Add cryptography algorithms --- third_party/3/jwt/algorithms.pyi | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/third_party/3/jwt/algorithms.pyi b/third_party/3/jwt/algorithms.pyi index 4272bd100981..7177dc7d1252 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -41,4 +41,43 @@ class HMACAlgorithm(Algorithm[bytes]): @staticmethod def from_jwk(jwk: _LoadsString) -> bytes: ... -# If cryptography is installed, also has classes RSAAlgorithm and ECAlgorithm. +# Only defined if cryptography is installed. Types should be tightened when +# cryptography gets type hints. +# See https://github.com/python/typeshed/issues/2542 +class RSAAlgorithm(Algorithm): + SHA256: ClassVar[Any] + SHA384: ClassVar[Any] + SHA512: ClassVar[Any] + hash_alg: Any + def __init__(self, hash_alg: Any) -> None: ... + def prepare_key(self, key: Any) -> Any: ... + @staticmethod + def to_jwk(key_obj: Any) -> str: ... + @staticmethod + def from_jwk(jwk: _LoadsString) -> Any: ... + def sign(self, msg: bytes, key: Any) -> bytes: ... + def verify(self, msg: bytes, key: Any, sig: bytes) -> bool: ... + +# Only defined if cryptography is installed. Types should be tightened when +# cryptography gets type hints. +# See https://github.com/python/typeshed/issues/2542 +class ECAlgorithm(Algorithm): + SHA256: ClassVar[Any] + SHA384: ClassVar[Any] + SHA512: ClassVar[Any] + hash_alg: Any + def __init__(self, hash_alg: Any) -> None: ... + def prepare_key(self, key: Any) -> Any: ... + @staticmethod + def to_jwk(key_obj: Any) -> str: ... + @staticmethod + def from_jwk(jwk: _LoadsString) -> Any: ... + def sign(self, msg: bytes, key: Any) -> bytes: ... + def verify(self, msg: bytes, key: Any, sig: bytes) -> bool: ... + +# Only defined if cryptography is installed. Types should be tightened when +# cryptography gets type hints. +# See https://github.com/python/typeshed/issues/2542 +class RSAPSSAlgorithm(RSAAlgorithm): + def sign(self, msg: bytes, key: Any) -> bytes: ... + def verify(self, msg: bytes, key: Any, sig: bytes) -> bool: ...