27
27
https://github.com/adafruit/Adafruit_CircuitPython_RSA
28
28
29
29
"""
30
+ try :
31
+ from typing import Tuple , Union , Optional
32
+ from circuitpython_typing import ReadableBuffer
33
+ except ImportError :
34
+ pass
35
+
30
36
import io
31
37
import json
32
38
from adafruit_rsa import PrivateKey , sign
@@ -47,7 +53,7 @@ class JWT:
47
53
"""
48
54
49
55
@staticmethod
50
- def validate (jwt ) :
56
+ def validate (jwt : str ) -> Tuple [ str , dict ] :
51
57
"""Validates a provided JWT. Does not support validating
52
58
nested signing. Returns JOSE Header and claim set.
53
59
:param str jwt: JSON Web Token.
@@ -77,7 +83,12 @@ def validate(jwt):
77
83
return (jose_header , claims )
78
84
79
85
@staticmethod
80
- def generate (claims , private_key_data = None , algo = None , headers = None ):
86
+ def generate (
87
+ claims : dict ,
88
+ private_key_data : Optional [str ] = None ,
89
+ algo : Optional [str ] = None ,
90
+ headers : Optional [dict ] = None ,
91
+ ) -> str :
81
92
"""Generates and returns a new JSON Web Token.
82
93
:param dict claims: JWT claims set
83
94
:param str private_key_data: Decoded RSA private key data.
@@ -140,7 +151,7 @@ class STRING_TOOLS:
140
151
printable = digits + ascii_letters + punctuation + whitespace
141
152
142
153
@staticmethod
143
- def urlsafe_b64encode (payload ) :
154
+ def urlsafe_b64encode (payload : ReadableBuffer ) -> str :
144
155
"""Encode bytes-like object using the URL- and filesystem-safe alphabet,
145
156
which substitutes - instead of + and _ instead of / in
146
157
the standard Base64 alphabet, and return the encoded bytes.
@@ -151,15 +162,15 @@ def urlsafe_b64encode(payload):
151
162
)
152
163
153
164
@staticmethod
154
- def urlsafe_b64decode (payload ) :
165
+ def urlsafe_b64decode (payload : Union [ ReadableBuffer , str ]) -> str :
155
166
"""Decode bytes-like object or ASCII string using the URL
156
167
and filesystem-safe alphabet
157
168
:param bytes payload: bytes-like object or ASCII string
158
169
"""
159
170
return a2b_base64 (STRING_TOOLS ._bytes_from_decode_data (payload )).decode ("utf-8" )
160
171
161
172
@staticmethod
162
- def _bytes_from_decode_data (str_data ) :
173
+ def _bytes_from_decode_data (str_data : Union [ ReadableBuffer , str ]) -> bytes :
163
174
# Types acceptable as binary data
164
175
bit_types = (bytes , bytearray )
165
176
if isinstance (str_data , str ):
@@ -180,7 +191,7 @@ def _bytes_from_decode_data(str_data):
180
191
# Port of CPython str.translate to Pure-Python by Johan Brichau, 2019
181
192
# https://github.com/jbrichau/TrackingPrototype/blob/master/Device/lib/string.py
182
193
@staticmethod
183
- def translate (s , table ) :
194
+ def translate (s : str , table : dict ) -> str :
184
195
"""Return a copy of the string in which each character
185
196
has been mapped through the given translation table.
186
197
:param string s: String to-be-character-table.
0 commit comments