4
4
5
5
"""
6
6
`adafruit_jwt`
7
- ================================================================================
7
+ ==============
8
8
9
9
JSON Web Token Authentication
10
10
27
27
https://github.com/adafruit/Adafruit_CircuitPython_RSA
28
28
29
29
"""
30
+
30
31
try :
31
32
from typing import Tuple , Union , Optional
32
33
from circuitpython_typing import ReadableBuffer
36
37
import io
37
38
import json
38
39
from adafruit_rsa import PrivateKey , sign
39
-
40
40
from adafruit_binascii import b2a_base64 , a2b_base64
41
41
42
42
@@ -48,14 +48,15 @@ class JWT:
48
48
"""JSON Web Token helper for CircuitPython. Warning: JWTs are
49
49
credentials, which can grant access to resources. Be careful
50
50
where you paste them!
51
- :param str algo: Encryption algorithm used for claims. Can be None.
52
51
52
+ :param str algo: Encryption algorithm used for claims. Can be None.
53
53
"""
54
54
55
55
@staticmethod
56
56
def validate (jwt : str ) -> Tuple [str , dict ]:
57
57
"""Validates a provided JWT. Does not support validating
58
58
nested signing. Returns JOSE Header and claim set.
59
+
59
60
:param str jwt: JSON Web Token.
60
61
:returns: The message's decoded JOSE header and claims.
61
62
:rtype: tuple
@@ -90,6 +91,7 @@ def generate(
90
91
headers : Optional [dict ] = None ,
91
92
) -> str :
92
93
"""Generates and returns a new JSON Web Token.
94
+
93
95
:param dict claims: JWT claims set
94
96
:param str private_key_data: Decoded RSA private key data.
95
97
:param str algo: algorithm to be used. One of None, RS256, RS384 or RS512.
@@ -155,6 +157,7 @@ def urlsafe_b64encode(payload: ReadableBuffer) -> str:
155
157
"""Encode bytes-like object using the URL- and filesystem-safe alphabet,
156
158
which substitutes - instead of + and _ instead of / in
157
159
the standard Base64 alphabet, and return the encoded bytes.
160
+
158
161
:param bytes payload: bytes-like object.
159
162
"""
160
163
return STRING_TOOLS .translate (
@@ -165,6 +168,7 @@ def urlsafe_b64encode(payload: ReadableBuffer) -> str:
165
168
def urlsafe_b64decode (payload : Union [ReadableBuffer , str ]) -> str :
166
169
"""Decode bytes-like object or ASCII string using the URL
167
170
and filesystem-safe alphabet
171
+
168
172
:param bytes payload: bytes-like object or ASCII string
169
173
"""
170
174
return a2b_base64 (STRING_TOOLS ._bytes_from_decode_data (payload )).decode ("utf-8" )
@@ -194,6 +198,7 @@ def _bytes_from_decode_data(str_data: Union[ReadableBuffer, str]) -> bytes:
194
198
def translate (s : str , table : dict ) -> str :
195
199
"""Return a copy of the string in which each character
196
200
has been mapped through the given translation table.
201
+
197
202
:param string s: String to-be-character-table.
198
203
:param dict table: Translation table.
199
204
"""
0 commit comments