Skip to content

added typing on six different funcs: validate, generate, urlsafe_b64e… #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions adafruit_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -151,15 +162,15 @@ 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
"""
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):
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# SPDX-License-Identifier: Unlicense

Adafruit-Blinka
adafruit-circuitpython-typing
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# Author details
author="Adafruit Industries",
author_email="[email protected]",
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
Expand Down