From 9e1436424b68485a92096956c12d8466c5e47283 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Sun, 21 May 2017 12:57:06 -0700 Subject: [PATCH 01/10] Add PyJWT type annotations --- third_party/3/jwt.pyi | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 third_party/3/jwt.pyi diff --git a/third_party/3/jwt.pyi b/third_party/3/jwt.pyi new file mode 100644 index 000000000000..c254fd919135 --- /dev/null +++ b/third_party/3/jwt.pyi @@ -0,0 +1,65 @@ +from typing import Mapping, Any, Optional, Union + + +def decode( + jwt: Union[str, bytes], + key: Union[str, bytes]=..., + verify: bool=..., + algorithms: Optional[Any]=..., + options: Optional[Mapping[Any, Any]]=..., + **kwargs: Any) -> Mapping[str, Any]: + ... + + +def encode( + payload: Mapping[str, Any], + key: Union[str, bytes], + algorithm: str=..., + headers: Optional[Mapping[str, Any]]=..., + json_encoder: Optional[Any]=...) -> bytes: + ... + + +class InvalidTokenError(Exception): + pass + + +class DecodeError(InvalidTokenError): + pass + + +class ExpiredSignatureError(InvalidTokenError): + pass + + +class InvalidAudienceError(InvalidTokenError): + pass + + +class InvalidIssuerError(InvalidTokenError): + pass + + +class InvalidIssuedAtError(InvalidTokenError): + pass + + +class ImmatureSignatureError(InvalidTokenError): + pass + + +class InvalidKeyError(Exception): + pass + + +class InvalidAlgorithmError(InvalidTokenError): + pass + + +class MissingRequiredClaimError(InvalidTokenError): + ... + +# Compatibility aliases (deprecated) +ExpiredSignature = ExpiredSignatureError +InvalidAudience = InvalidAudienceError +InvalidIssuer = InvalidIssuerError From 493044b9bbc630f9420181801205631e8c7b99c7 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Sun, 21 May 2017 14:23:44 -0700 Subject: [PATCH 02/10] Make jwt.pyi more compact, fix flake8 errors. --- third_party/3/jwt.pyi | 63 ++++++++++++------------------------------- 1 file changed, 17 insertions(+), 46 deletions(-) diff --git a/third_party/3/jwt.pyi b/third_party/3/jwt.pyi index c254fd919135..a405b050d454 100644 --- a/third_party/3/jwt.pyi +++ b/third_party/3/jwt.pyi @@ -1,63 +1,34 @@ from typing import Mapping, Any, Optional, Union -def decode( - jwt: Union[str, bytes], - key: Union[str, bytes]=..., - verify: bool=..., - algorithms: Optional[Any]=..., - options: Optional[Mapping[Any, Any]]=..., - **kwargs: Any) -> Mapping[str, Any]: - ... +def decode(jwt: Union[str, bytes], key: Union[str, bytes]=..., + verify: bool=..., algorithms: Optional[Any]=..., + options: Optional[Mapping[Any, Any]]=..., + **kwargs: Any) -> Mapping[str, Any]: ... +def encode(payload: Mapping[str, Any], key: Union[str, bytes], + algorithm: str=..., headers: Optional[Mapping[str, Any]]=..., + json_encoder: Optional[Any]=...) -> bytes: ... -def encode( - payload: Mapping[str, Any], - key: Union[str, bytes], - algorithm: str=..., - headers: Optional[Mapping[str, Any]]=..., - json_encoder: Optional[Any]=...) -> bytes: - ... +class InvalidTokenError(Exception): pass +class DecodeError(InvalidTokenError): pass -class InvalidTokenError(Exception): - pass +class ExpiredSignatureError(InvalidTokenError): pass +class InvalidAudienceError(InvalidTokenError): pass -class DecodeError(InvalidTokenError): - pass +class InvalidIssuerError(InvalidTokenError): pass +class InvalidIssuedAtError(InvalidTokenError): pass -class ExpiredSignatureError(InvalidTokenError): - pass +class ImmatureSignatureError(InvalidTokenError): pass +class InvalidKeyError(Exception): pass -class InvalidAudienceError(InvalidTokenError): - pass +class InvalidAlgorithmError(InvalidTokenError): pass - -class InvalidIssuerError(InvalidTokenError): - pass - - -class InvalidIssuedAtError(InvalidTokenError): - pass - - -class ImmatureSignatureError(InvalidTokenError): - pass - - -class InvalidKeyError(Exception): - pass - - -class InvalidAlgorithmError(InvalidTokenError): - pass - - -class MissingRequiredClaimError(InvalidTokenError): - ... +class MissingRequiredClaimError(InvalidTokenError): ... # Compatibility aliases (deprecated) ExpiredSignature = ExpiredSignatureError From 3c367828bf46139a334c9d1d8770376851b577a5 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Sun, 21 May 2017 14:25:24 -0700 Subject: [PATCH 03/10] Put spaces around '=' for default values. --- third_party/3/jwt.pyi | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/third_party/3/jwt.pyi b/third_party/3/jwt.pyi index a405b050d454..ec6d1579aa70 100644 --- a/third_party/3/jwt.pyi +++ b/third_party/3/jwt.pyi @@ -1,14 +1,14 @@ from typing import Mapping, Any, Optional, Union -def decode(jwt: Union[str, bytes], key: Union[str, bytes]=..., - verify: bool=..., algorithms: Optional[Any]=..., - options: Optional[Mapping[Any, Any]]=..., +def decode(jwt: Union[str, bytes], key: Union[str, bytes] = ..., + verify: bool = ..., algorithms: Optional[Any] = ..., + options: Optional[Mapping[Any, Any]] = ..., **kwargs: Any) -> Mapping[str, Any]: ... def encode(payload: Mapping[str, Any], key: Union[str, bytes], - algorithm: str=..., headers: Optional[Mapping[str, Any]]=..., - json_encoder: Optional[Any]=...) -> bytes: ... + algorithm: str = ..., headers: Optional[Mapping[str, Any]] = ..., + json_encoder: Optional[Any] = ...) -> bytes: ... class InvalidTokenError(Exception): pass From 9f3ac443133157bd11c905232e7de689df0335f3 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Tue, 23 May 2017 09:04:52 -0700 Subject: [PATCH 04/10] Add more public APIs for pyjwt. --- third_party/3/{jwt.pyi => jwt/__init__.pyi} | 15 +++++++++++++++ third_party/3/jwt/algorithms.pyi | 2 ++ third_party/3/jwt/contrib/__init__.pyi | 0 third_party/3/jwt/contrib/algorithms/__init__.pyi | 8 ++++++++ third_party/3/jwt/contrib/algorithms/py_ecdsa.pyi | 10 ++++++++++ third_party/3/jwt/contrib/algorithms/pycrypto.pyi | 10 ++++++++++ 6 files changed, 45 insertions(+) rename third_party/3/{jwt.pyi => jwt/__init__.pyi} (68%) create mode 100644 third_party/3/jwt/algorithms.pyi create mode 100644 third_party/3/jwt/contrib/__init__.pyi create mode 100644 third_party/3/jwt/contrib/algorithms/__init__.pyi create mode 100644 third_party/3/jwt/contrib/algorithms/py_ecdsa.pyi create mode 100644 third_party/3/jwt/contrib/algorithms/pycrypto.pyi diff --git a/third_party/3/jwt.pyi b/third_party/3/jwt/__init__.pyi similarity index 68% rename from third_party/3/jwt.pyi rename to third_party/3/jwt/__init__.pyi index ec6d1579aa70..049dc88f15cc 100644 --- a/third_party/3/jwt.pyi +++ b/third_party/3/jwt/__init__.pyi @@ -1,5 +1,8 @@ from typing import Mapping, Any, Optional, Union +# This is not actually in the module namespace, so we'll +# prefix it with an underscore. +from .algorithms import Algorithm as _Algorithm def decode(jwt: Union[str, bytes], key: Union[str, bytes] = ..., verify: bool = ..., algorithms: Optional[Any] = ..., @@ -10,6 +13,10 @@ def encode(payload: Mapping[str, Any], key: Union[str, bytes], algorithm: str = ..., headers: Optional[Mapping[str, Any]] = ..., json_encoder: Optional[Any] = ...) -> bytes: ... +def register_algorithm(alg_id: str, alg_obj: _Algorithm) -> None: ... + +def unregister_algorithm(alg_id: str) -> None: ... + class InvalidTokenError(Exception): pass class DecodeError(InvalidTokenError): pass @@ -34,3 +41,11 @@ class MissingRequiredClaimError(InvalidTokenError): ... ExpiredSignature = ExpiredSignatureError InvalidAudience = InvalidAudienceError InvalidIssuer = InvalidIssuerError + +# These aren't actually documented, but the package +# exports them in __init__.py, so we should at least +# make sure that mypy doesn't raise spurious errors +# if they're used. +get_unverified_header = ... # type: Any +PyJWT = ... # type: Any +PyJWS = ... # type: Any diff --git a/third_party/3/jwt/algorithms.pyi b/third_party/3/jwt/algorithms.pyi new file mode 100644 index 000000000000..947a22380fcb --- /dev/null +++ b/third_party/3/jwt/algorithms.pyi @@ -0,0 +1,2 @@ +class Algorithm: + ... diff --git a/third_party/3/jwt/contrib/__init__.pyi b/third_party/3/jwt/contrib/__init__.pyi new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/third_party/3/jwt/contrib/algorithms/__init__.pyi b/third_party/3/jwt/contrib/algorithms/__init__.pyi new file mode 100644 index 000000000000..5ca2bce8659d --- /dev/null +++ b/third_party/3/jwt/contrib/algorithms/__init__.pyi @@ -0,0 +1,8 @@ +from typing import Callable, Any, Union +from hashlib import _DataType + +# In reality, _HashAlg is a function of the type +# that hashlib.sha256/384/512 are, but there doesn't +# seem to be a consistent exportable name that we can reference +# for that, so we'll just say it's a Callable here. +_HashAlg = Callable[[_DataType], Any] diff --git a/third_party/3/jwt/contrib/algorithms/py_ecdsa.pyi b/third_party/3/jwt/contrib/algorithms/py_ecdsa.pyi new file mode 100644 index 000000000000..cd2926f4235c --- /dev/null +++ b/third_party/3/jwt/contrib/algorithms/py_ecdsa.pyi @@ -0,0 +1,10 @@ +from typing import Any +from jwt.algorithms import Algorithm + +from . import _HashAlg + +class ECAlgorithm(Algorithm): + SHA256 = ... # type: _HashAlg + SHA384 = ... # type: _HashAlg + SHA512 = ... # type: _HashAlg + def __init__(self, hash_alg: _HashAlg) -> None: ... diff --git a/third_party/3/jwt/contrib/algorithms/pycrypto.pyi b/third_party/3/jwt/contrib/algorithms/pycrypto.pyi new file mode 100644 index 000000000000..08ae8ba2d00a --- /dev/null +++ b/third_party/3/jwt/contrib/algorithms/pycrypto.pyi @@ -0,0 +1,10 @@ +from typing import Any +from jwt.algorithms import Algorithm + +from . import _HashAlg + +class RSAAlgorithm(Algorithm): + SHA256 = ... # type: _HashAlg + SHA384 = ... # type: _HashAlg + SHA512 = ... # type: _HashAlg + def __init__(self, hash_alg: _HashAlg) -> None: ... From 40601917ebc4f8f42cd56a2de088939ec8038dd9 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Wed, 24 May 2017 10:23:36 -0700 Subject: [PATCH 05/10] Fix typings. --- third_party/3/jwt/__init__.pyi | 7 +++---- third_party/3/jwt/algorithms.pyi | 5 +++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/third_party/3/jwt/__init__.pyi b/third_party/3/jwt/__init__.pyi index 049dc88f15cc..010f451a64a4 100644 --- a/third_party/3/jwt/__init__.pyi +++ b/third_party/3/jwt/__init__.pyi @@ -1,8 +1,6 @@ from typing import Mapping, Any, Optional, Union -# This is not actually in the module namespace, so we'll -# prefix it with an underscore. -from .algorithms import Algorithm as _Algorithm +from . import algorithms def decode(jwt: Union[str, bytes], key: Union[str, bytes] = ..., verify: bool = ..., algorithms: Optional[Any] = ..., @@ -13,7 +11,8 @@ def encode(payload: Mapping[str, Any], key: Union[str, bytes], algorithm: str = ..., headers: Optional[Mapping[str, Any]] = ..., json_encoder: Optional[Any] = ...) -> bytes: ... -def register_algorithm(alg_id: str, alg_obj: _Algorithm) -> None: ... +def register_algorithm(alg_id: str, + alg_obj: algorithms.Algorithm) -> None: ... def unregister_algorithm(alg_id: str) -> None: ... diff --git a/third_party/3/jwt/algorithms.pyi b/third_party/3/jwt/algorithms.pyi index 947a22380fcb..28c5214de1f4 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -1,2 +1,3 @@ -class Algorithm: - ... +from typing import Any + +class Algorithm: Any From 38d932cee1960f6088973f98e06c54fee8f83e45 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Wed, 24 May 2017 10:27:32 -0700 Subject: [PATCH 06/10] Add type: ignore to Algorithm. --- 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 28c5214de1f4..db04477a15ce 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -1,3 +1,3 @@ from typing import Any -class Algorithm: Any +class Algorithm: Any # type: ignore From 3e20c57cc3b050145f7f0fc4d21816cf33807e48 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Wed, 24 May 2017 10:54:45 -0700 Subject: [PATCH 07/10] Make Algorithm a subclass of Any. --- 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 db04477a15ce..4576b4169354 100644 --- a/third_party/3/jwt/algorithms.pyi +++ b/third_party/3/jwt/algorithms.pyi @@ -1,3 +1,3 @@ from typing import Any -class Algorithm: Any # type: ignore +class Algorithm(Any): ... # type: ignore From c0930291332514cd3c8e862b077c822e7985f749 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Mon, 29 May 2017 08:57:50 -0400 Subject: [PATCH 08/10] Remove blank lines between exception classes. --- third_party/3/jwt/__init__.pyi | 9 --------- 1 file changed, 9 deletions(-) diff --git a/third_party/3/jwt/__init__.pyi b/third_party/3/jwt/__init__.pyi index 010f451a64a4..3161c91425f8 100644 --- a/third_party/3/jwt/__init__.pyi +++ b/third_party/3/jwt/__init__.pyi @@ -17,23 +17,14 @@ def register_algorithm(alg_id: str, def unregister_algorithm(alg_id: str) -> None: ... class InvalidTokenError(Exception): pass - class DecodeError(InvalidTokenError): pass - class ExpiredSignatureError(InvalidTokenError): pass - class InvalidAudienceError(InvalidTokenError): pass - class InvalidIssuerError(InvalidTokenError): pass - class InvalidIssuedAtError(InvalidTokenError): pass - class ImmatureSignatureError(InvalidTokenError): pass - class InvalidKeyError(Exception): pass - class InvalidAlgorithmError(InvalidTokenError): pass - class MissingRequiredClaimError(InvalidTokenError): ... # Compatibility aliases (deprecated) From a609aaee8d658ff430593be5080a22215773bd44 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Sat, 3 Jun 2017 10:00:35 -0400 Subject: [PATCH 09/10] import _Hash as _HashAlg. --- third_party/3/jwt/contrib/algorithms/__init__.pyi | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/third_party/3/jwt/contrib/algorithms/__init__.pyi b/third_party/3/jwt/contrib/algorithms/__init__.pyi index 5ca2bce8659d..4b231c89b23b 100644 --- a/third_party/3/jwt/contrib/algorithms/__init__.pyi +++ b/third_party/3/jwt/contrib/algorithms/__init__.pyi @@ -1,8 +1,2 @@ from typing import Callable, Any, Union -from hashlib import _DataType - -# In reality, _HashAlg is a function of the type -# that hashlib.sha256/384/512 are, but there doesn't -# seem to be a consistent exportable name that we can reference -# for that, so we'll just say it's a Callable here. -_HashAlg = Callable[[_DataType], Any] +from hashlib import _Hash as _HashAlg From c805fcb4d93b57904bb93c193da35e6ec02a9731 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Sat, 3 Jun 2017 10:06:03 -0400 Subject: [PATCH 10/10] Get rid of unused typing imports. --- third_party/3/jwt/contrib/algorithms/__init__.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/third_party/3/jwt/contrib/algorithms/__init__.pyi b/third_party/3/jwt/contrib/algorithms/__init__.pyi index 4b231c89b23b..b2bb1f643a3e 100644 --- a/third_party/3/jwt/contrib/algorithms/__init__.pyi +++ b/third_party/3/jwt/contrib/algorithms/__init__.pyi @@ -1,2 +1 @@ -from typing import Callable, Any, Union from hashlib import _Hash as _HashAlg