Skip to content

Commit f831f5e

Browse files
committed
docs: update for OctKey.import_key
1 parent 2bf6328 commit f831f5e

File tree

10 files changed

+45
-47
lines changed

10 files changed

+45
-47
lines changed

docs/guide/algorithms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ algorithms either by the ``algorithms`` parameter, or ``registry`` parameter.
6565
from joserfc import jws
6666
from joserfc.jwk import OctKey
6767
68-
key = OctKey.import_key("secret")
68+
key = OctKey.import_key("your-secret-key")
6969
# HS384 is a non-recommended algorithm
7070
jws.serialize_compact({"alg": "HS384"}, b"payload", key, algorithms=["HS384"])
7171

docs/guide/index.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ Encode and decode JWT
1111

1212
.. code-block:: python
1313
14-
>>> from joserfc import jwt
15-
>>> from joserfc.jwk import OctKey
16-
>>> key = OctKey.import_key("secret")
17-
>>> encoded_jwt = jwt.encode({"alg": "HS256"}, {"key": "value"}, key)
14+
>>> from joserfc import jwt, jwk
15+
>>> key = jwk.import_key("your-secret-key", "oct")
16+
>>> encoded_jwt = jwt.encode({"alg": "HS256"}, {"k": "value"}, key)
1817
>>> encoded_jwt
19-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWx1ZSJ9.FG-8UppwHaFp1LgRYQQeS6EDQF7_6-bMFegNucHjmWg'
18+
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrIjoidmFsdWUifQ._M8ViO_GK6TnZ9G9eqdlS7IpNWzhoGwaYYDQ3hEwwmA'
2019
>>> token = jwt.decode(encoded_jwt, key)
2120
>>> token.header
2221
{'alg': 'HS256', 'typ': 'JWT'}

docs/guide/jwk.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ You can import an ``OctKey`` from string, bytes and a JWK (in dict).
4646
OctKey.import_key(b"a-random-bytes-as-key")
4747
OctKey.import_key({
4848
"kty": "oct",
49-
"k": "Zm9v",
49+
"k": "eW91ci1zZWNyZXQta2V5",
5050
})
5151
5252
When importing a key, you can add extra parameters into a key:
5353

5454
.. code-block:: python
5555
5656
>>> from joserfc.jwk import OctKey
57-
>>> key = OctKey.import_key("foo", {"use": "sig"})
57+
>>> key = OctKey.import_key("your-secret-key", {"use": "sig"})
5858
>>> key.as_dict()
59-
{'k': 'Zm9v', 'use': 'sig', 'kty': 'oct'}
59+
{'k': 'eW91ci1zZWNyZXQta2V5', 'use': 'sig', 'kty': 'oct'}
6060
6161
.. _RSAKey:
6262

@@ -354,12 +354,12 @@ Call this method to make sure the key contains a ``kid``. If the key has no
354354
.. code-block:: python
355355
356356
>>> from joserfc.jwk import OctKey
357-
>>> key = OctKey.import_key("foo")
357+
>>> key = OctKey.import_key("your-secret-key")
358358
>>> key.kid
359359
None
360360
>>> key.ensure_kid()
361361
>>> key.kid
362-
'8-e-qGDS2nDpfZzOPtD8Sb7NkifUbw70MeqOKIqyaRw'
362+
'IGfff6pETIMRgwAOHtzIQ_Sflgv5wzWl6SYZlcZ_JDU'
363363
364364
``as_dict``
365365
~~~~~~~~~~~

docs/guide/jws.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ You can call :meth:`jws.serialize_compact` to construct a compact JWS serializat
4545
from joserfc import jws
4646
from joserfc.jwk import OctKey
4747
48-
key = OctKey.import_key("secret")
48+
key = OctKey.import_key("your-secret-key")
4949
protected = {"alg": "HS256"}
5050
jws.serialize_compact(protected, "hello", key)
51-
# => 'eyJhbGciOiJIUzI1NiJ9.aGVsbG8.UYmO_lPAY5V0Wf4KZsfhiYs1SxqXPhxvjuYqellDV5A'
51+
# => 'eyJhbGciOiJIUzI1NiJ9.aGVsbG8.i_RrfHeMxwHrhk5Xi3J_bU9B9O-gjkMaQtagtqiCndM'
5252
5353
A compact JWS is constructed by protected header, payload and a private key. In the above
5454
example, ``protected`` is the "protected header" part, `"hello"` is the payload part, and
@@ -65,8 +65,8 @@ serialization with a public key.
6565
from joserfc import jws
6666
from joserfc.jwk import OctKey
6767
68-
text = "eyJhbGciOiJIUzI1NiJ9.aGVsbG8.UYmO_lPAY5V0Wf4KZsfhiYs1SxqXPhxvjuYqellDV5A"
69-
key = OctKey.import_key("secret")
68+
text = "eyJhbGciOiJIUzI1NiJ9.aGVsbG8.i_RrfHeMxwHrhk5Xi3J_bU9B9O-gjkMaQtagtqiCndM"
69+
key = OctKey.import_key("your-secret-key")
7070
obj = jws.deserialize_compact(text, key)
7171
# obj.protected => {"alg": "HS256"}
7272
# obj.payload => b"hello"
@@ -298,7 +298,7 @@ either by the ``algorithms`` parameter, or ``registry`` parameter.
298298
299299
>>> from joserfc import jws
300300
>>> from joserfc.jwk import OctKey
301-
>>> key = OctKey.import_key("secret")
301+
>>> key = OctKey.import_key("your-secret-key")
302302
>>> jws.serialize_compact({"alg": "HS384"}, b"payload", key, algorithms=["HS384"])
303303
'eyJhbGciOiJIUzM4NCJ9.cGF5bG9hZA.TJEvlp74g89hNRNGNZxCQvB7YDEAWP5vFAjgu1O9Qr5BLMj0NtvbxvYkVYPGp-xQ'
304304
@@ -309,7 +309,7 @@ of using :ref:`registry`.
309309
310310
>>> from joserfc import jws
311311
>>> from joserfc.jwk import OctKey
312-
>>> key = OctKey.import_key("secret")
312+
>>> key = OctKey.import_key("your-secret-key")
313313
>>> registry = jws.JWSRegistry(algorithms=["HS384"])
314314
>>> jws.serialize_compact({"alg": "HS384"}, b"payload", key, registry=registry)
315315
'eyJhbGciOiJIUzM4NCJ9.cGF5bG9hZA.TJEvlp74g89hNRNGNZxCQvB7YDEAWP5vFAjgu1O9Qr5BLMj0NtvbxvYkVYPGp-xQ'
@@ -332,7 +332,7 @@ Here are examples demonstrating the usage of the ``b64`` option:
332332
from joserfc.jws import serialize_compact, deserialize_compact
333333
from joserfc.jwk import OctKey
334334
335-
key = OctKey.import_key("secret")
335+
key = OctKey.import_key("your-secret-key")
336336
protected = {"alg": "HS256", "b64": False, "crit": ["b64"]}
337337
value = serialize_compact(protected, "hello", key)
338338
# => 'eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19.hello.mdPbZLtc3tqQ6NCV1pKF-qfEx-3jtR6rv109phKAc4I'
@@ -351,10 +351,10 @@ characters, the compact serialization will detach the payload:
351351
from joserfc.jws import serialize_compact, deserialize_compact
352352
from joserfc.jwk import OctKey
353353
354-
key = OctKey.import_key("secret")
354+
key = OctKey.import_key("your-secret-key")
355355
protected = {"alg": "HS256", "b64": False, "crit": ["b64"]}
356356
value = serialize_compact(protected, "$.02", key)
357-
# => 'eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..GbtzAD3Cwe6snTZnaAxapwQz5QftEz7agx_6aMtZ4w0'
357+
# => 'eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..6iWgAK_TZLgwzk1mhtxs6Imw-dJM1cRstsOKVQ5MjFQ'
358358
# since the payload is detached, you need to specify the
359359
# payload when calling deserialize_compact
360360
deserialize_compact(value, key, payload="$.02")

docs/guide/jwt.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ It encodes the payload with the given ``alg`` in header:
3030
3131
header = {"alg": "HS256"}
3232
claims = {"iss": "https://authlib.org"}
33-
key = OctKey.import_key("secret")
33+
key = OctKey.import_key("your-secret-key")
3434
text = jwt.encode(header, claims, key)
3535
3636
The returned value of ``text`` in above example is:
@@ -451,7 +451,7 @@ this algorithm:
451451
>>> from joserfc import jwt, jwk
452452
>>> header = {"alg": "HS384"}
453453
>>> claims = {"iss": "https://authlib.org"}
454-
>>> key = jwk.OctKey.import_key("secret")
454+
>>> key = jwk.OctKey.import_key("your-secret-key")
455455
>>> jwt.encode(header, claims, key, algorithms=["HS384"])
456456
457457
If not specifying the ``algorithms`` parameter, the ``encode`` method will
@@ -474,7 +474,7 @@ be raised.
474474
>>> import uuid
475475
>>> from joserfc import jwt, jwk
476476
>>>
477-
>>> key = jwk.OctKey.import_key("secret")
477+
>>> key = jwk.OctKey.import_key("your-secret-key")
478478
>>> claims = {"sub": uuid.uuid4()}
479479
>>> jwt.encode({"alg": "HS256"}, claims, key)
480480
Traceback (most recent call last):
@@ -512,7 +512,7 @@ To resolve this issue, you can pass a custom ``JSONEncoder`` using the ``encoder
512512
return str(o)
513513
return super().default(o)
514514
515-
key = jwk.OctKey.import_key("secret")
515+
key = jwk.OctKey.import_key("your-secret-key")
516516
claims = {"sub": uuid.uuid4()}
517517
jwt.encode({"alg": "HS256"}, claims, key, encoder_cls=MyEncoder)
518518

docs/guide/registry.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ the value type.
6969
7070
>>> from joserfc import jws
7171
>>> from joserfc.jwk import OctKey
72-
>>> key = OctKey.import_key("secret")
72+
>>> key = OctKey.import_key("your-secret-key")
7373
>>> jws.serialize_compact({"alg": "HS256", "kid": 123}, "hello", key)
7474
Traceback (most recent call last):
7575
File "<stdin>", line 1, in <module>
@@ -95,7 +95,7 @@ indicating that they must be present. For example:
9595
9696
>>> from joserfc import jws
9797
>>> from joserfc.jwk import OctKey
98-
>>> key = OctKey.import_key("secret")
98+
>>> key = OctKey.import_key("your-secret-key")
9999
>>> jws.serialize_compact({"alg": "HS256", "crit": ["kid"]}, "hello", key)
100100
Traceback (most recent call last):
101101
File "<stdin>", line 1, in <module>
@@ -120,7 +120,7 @@ Any additional header beyond those supported by the algorithm will result in an
120120
121121
>>> from joserfc import jws
122122
>>> from joserfc.jwk import OctKey
123-
>>> key = OctKey.import_key("secret")
123+
>>> key = OctKey.import_key("your-secret-key")
124124
>>> jws.serialize_compact({"alg": "HS256", "custom": "hi"}, "hello", key)
125125
Traceback (most recent call last):
126126
File "<stdin>", line 1, in <module>
@@ -143,7 +143,7 @@ to recognize and validate those parameters instead of raising an error.
143143
from joserfc.registry import HeaderParameter
144144
from joserfc.jwk import OctKey
145145
146-
key = OctKey.import_key("secret")
146+
key = OctKey.import_key("your-secret-key")
147147
148148
additional_header_registry = {
149149
"custom": HeaderParameter("Custom message", "str", required=True),

docs/index.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ A quick and simple JWT encoding and decoding would look something like this:
1717

1818
.. code-block:: python
1919
20-
>>> from joserfc import jwt
21-
>>> from joserfc.jwk import OctKey
22-
>>> key = OctKey.import_key("secret")
20+
>>> from joserfc import jwt, jwk
21+
>>> key = jwk.import_key("your-secret-key", "oct")
2322
>>> encoded = jwt.encode({"alg": "HS256"}, {"k": "value"}, key)
2423
>>> encoded
25-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrIjoidmFsdWUifQ.ni-MJXnZHpFB_8L9P9yllj3RNDfzmD4yBKAyefSctMY'
24+
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrIjoidmFsdWUifQ._M8ViO_GK6TnZ9G9eqdlS7IpNWzhoGwaYYDQ3hEwwmA'
2625
>>> token = jwt.decode(encoded, key)
2726
>>> token.header
2827
{'alg': 'HS256', 'typ': 'JWT'}

docs/migrations/authlib.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In both libraries, you can encode a JWT using the ``jwt.encode(header, payload,
2424
2525
from authlib.jose import jwt
2626
27-
jwt.encode({"alg": "HS256"}, {"iss": "https://jose.authlib.org"}, "secret")
27+
jwt.encode({"alg": "HS256"}, {"iss": "https://jose.authlib.org"}, "your-secret-key")
2828
2929
3030
.. code-block:: python
@@ -33,7 +33,7 @@ In both libraries, you can encode a JWT using the ``jwt.encode(header, payload,
3333
from joserfc import jwt
3434
from joserfc.jwk import OctKey
3535
36-
key = OctKey.import_key("secret")
36+
key = OctKey.import_key("your-secret-key")
3737
jwt.encode({"alg": "HS256"}, {"iss": "https://jose.authlib.org"}, key)
3838
3939
jwt.decode
@@ -86,16 +86,16 @@ follow these steps:
8686
8787
protected = {'alg': 'HS256'}
8888
payload = b"example"
89-
value = jws.serialize_compact(protected, payload, "secret")
90-
jws.deserialize_compact(value, "secret")
89+
value = jws.serialize_compact(protected, payload, "your-secret-key")
90+
jws.deserialize_compact(value, "your-secret-key")
9191
9292
.. code-block:: python
9393
:caption: joserfc
9494
9595
from joserfc import jws
9696
from joserfc.jwk import OctKey
9797
98-
key = OctKey.import_key("secret")
98+
key = OctKey.import_key("your-secret-key")
9999
protected = {"alg': 'HS256"}
100100
payload = b"example"
101101
value = jws.serialize_compact(protected, payload, key)

docs/migrations/pyjwt.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ structure differs between the two libraries.
1818
1919
import jwt
2020
# jwt.encode(payload, key, algorithm)
21-
encoded_jwt = jwt.encode({"some": "payload"}, "secret", algorithm="HS256")
21+
encoded_jwt = jwt.encode({"some": "payload"}, "your-secret-key", algorithm="HS256")
2222
2323
.. code-block:: python
2424
:caption: joserfc
2525
2626
from joserfc import jwt
2727
from joserfc.jwk import OctKey
2828
29-
key = OctKey.import_key("secret") # use an explicit key
29+
key = OctKey.import_key("your-secret-key") # use an explicit key
3030
# jwt.encode(header, payload, key)
3131
encoded_jwt = jwt.encode({"alg": "HS256"}, {"some": "payload"}, key)
3232
@@ -39,7 +39,7 @@ but the parameter structure differs.
3939
.. code-block:: python
4040
:caption: PyJWT
4141
42-
token = jwt.decode(encoded_jwt, "secret", algorithms=["HS256"])
42+
token = jwt.decode(encoded_jwt, "your-secret-key", algorithms=["HS256"])
4343
# => {"some": "payload"}
4444
4545
.. code-block:: python
@@ -48,7 +48,7 @@ but the parameter structure differs.
4848
from joserfc import jwt
4949
from joserfc.jwk import OctKey
5050
51-
key = OctKey.import_key("secret")
51+
key = OctKey.import_key("your-secret-key")
5252
token = jwt.decode(encoded_jwt, key)
5353
# => token.header : {"alg": "HS256"}
5454
# => token.claims : {"some": "payload"}

docs/migrations/python-jose.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ and ``jws.deserialize_compact`` for deserialization.
2424
2525
from jose import jws
2626
27-
signed = jws.sign({'a': 'b'}, 'secret', algorithm='HS256')
28-
jws.verify(signed, 'secret', algorithms='HS256')
27+
signed = jws.sign({'a': 'b'}, 'your-secret-key', algorithm='HS256')
28+
jws.verify(signed, 'your-secret-key', algorithms='HS256')
2929
# the verify only returns the payload
3030
3131
.. code-block:: python
@@ -35,7 +35,7 @@ and ``jws.deserialize_compact`` for deserialization.
3535
from joserfc import jws
3636
from joserfc.jwk import OctKey
3737
38-
key = OctKey.import_key("secret")
38+
key = OctKey.import_key("your-secret-key")
3939
protected = {"alg": "HS256"}
4040
signed = jws.serialize_compact(protected, json.dumps({'a': 'b'}), key)
4141
obj = jws.deserialize_compact(text, key)
@@ -94,8 +94,8 @@ differ significantly in terms of structure and flexibility.
9494
9595
from jose import jwt
9696
97-
encoded = jwt.encode({'a': 'b'}, 'secret', algorithm='HS256')
98-
jwt.decode(encoded, 'secret', algorithms='HS256')
97+
encoded = jwt.encode({'a': 'b'}, 'your-secret-key', algorithm='HS256')
98+
jwt.decode(encoded, 'your-secret-key', algorithms='HS256')
9999
# => {'a': 'b'}
100100
101101
.. code-block:: python
@@ -104,7 +104,7 @@ differ significantly in terms of structure and flexibility.
104104
from joserfc import jwt
105105
from joserfc.jwk import OctKey
106106
107-
key = OctKey.import_key("secret")
107+
key = OctKey.import_key("your-secret-key")
108108
# jwt.encode(header, payload, key)
109109
encoded = jwt.encode({"alg": "HS256"}, {'a': 'b'}, key)
110110
token = jwt.decode(encoded, key)

0 commit comments

Comments
 (0)