From a16227fa04ef96fd1234d9610e27092b773ed603 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 14:55:19 -0600 Subject: [PATCH 1/4] added typing on six different funcs: validate, generate, urlsafe_b64encode, urlsafeb64_decode, _bytes_from_decode_data, translate --- adafruit_jwt.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/adafruit_jwt.py b/adafruit_jwt.py index e067563..db1b4fb 100644 --- a/adafruit_jwt.py +++ b/adafruit_jwt.py @@ -27,6 +27,12 @@ https://github.com/adafruit/Adafruit_CircuitPython_RSA """ +try: + from typing import Tuple, Union, Optional + from circuitpython_typing import ReadableBuffer +except ImportError: + pass + import io import json from adafruit_rsa import PrivateKey, sign @@ -47,7 +53,7 @@ class JWT: """ @staticmethod - def validate(jwt): + def validate(jwt: str) -> Tuple[str, dict]: """Validates a provided JWT. Does not support validating nested signing. Returns JOSE Header and claim set. :param str jwt: JSON Web Token. @@ -77,7 +83,12 @@ def validate(jwt): return (jose_header, claims) @staticmethod - def generate(claims, private_key_data=None, algo=None, headers=None): + def generate( + claims: dict, + private_key_data: Optional[str] = None, + algo: Optional[str] = None, + headers: Optional[dict] = None, + ) -> str: """Generates and returns a new JSON Web Token. :param dict claims: JWT claims set :param str private_key_data: Decoded RSA private key data. @@ -140,7 +151,7 @@ class STRING_TOOLS: printable = digits + ascii_letters + punctuation + whitespace @staticmethod - def urlsafe_b64encode(payload): + def urlsafe_b64encode(payload: ReadableBuffer) -> str: """Encode bytes-like object using the URL- and filesystem-safe alphabet, which substitutes - instead of + and _ instead of / in the standard Base64 alphabet, and return the encoded bytes. @@ -151,7 +162,7 @@ def urlsafe_b64encode(payload): ) @staticmethod - def urlsafe_b64decode(payload): + def urlsafe_b64decode(payload: Union[ReadableBuffer, str]) -> str: """Decode bytes-like object or ASCII string using the URL and filesystem-safe alphabet :param bytes payload: bytes-like object or ASCII string @@ -159,7 +170,7 @@ def urlsafe_b64decode(payload): return a2b_base64(STRING_TOOLS._bytes_from_decode_data(payload)).decode("utf-8") @staticmethod - def _bytes_from_decode_data(str_data): + def _bytes_from_decode_data(str_data: Union[ReadableBuffer, str]) -> bytes: # Types acceptable as binary data bit_types = (bytes, bytearray) if isinstance(str_data, str): @@ -180,7 +191,7 @@ def _bytes_from_decode_data(str_data): # Port of CPython str.translate to Pure-Python by Johan Brichau, 2019 # https://github.com/jbrichau/TrackingPrototype/blob/master/Device/lib/string.py @staticmethod - def translate(s, table): + def translate(s: str, table: dict) -> str: """Return a copy of the string in which each character has been mapped through the given translation table. :param string s: String to-be-character-table. From c2e3c2f30d206636418c817a0f7ecdecfb9bc2d3 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 15:13:08 -0600 Subject: [PATCH 2/4] fixed dependency issues to bring in circuitpython_typing --- requirements.txt | 1 + setup.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 17a850d..a6b448b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ # SPDX-License-Identifier: Unlicense Adafruit-Blinka +adafruit-circuitpython-typing \ No newline at end of file diff --git a/setup.py b/setup.py index 43ed599..7e538c2 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ # Author details author="Adafruit Industries", author_email="circuitpython@adafruit.com", - install_requires=["Adafruit-Blinka"], + install_requires=["Adafruit-Blinka", "adafruit-circuitpython-typing"], # Choose your license license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers From da5828ea4b5e012e62f50928b9c97e19b9ac15df Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 15:27:45 -0600 Subject: [PATCH 3/4] committing to fix pre-commit issues, I hope --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index a6b448b..9e2a4b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,5 @@ # # SPDX-License-Identifier: Unlicense -Adafruit-Blinka +Adafruit-Blink adafruit-circuitpython-typing \ No newline at end of file From 37e4041ea3278af40bb3c7e8910670c898846c0f Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 15:29:37 -0600 Subject: [PATCH 4/4] committing to run pre-commit --- requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9e2a4b2..d8231e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,5 @@ # # SPDX-License-Identifier: Unlicense -Adafruit-Blink -adafruit-circuitpython-typing \ No newline at end of file +Adafruit-Blinka +adafruit-circuitpython-typing