Skip to content

Commit ff77f5c

Browse files
committed
docs: update docs for JWSRegistry.guess_alg
1 parent 8553afb commit ff77f5c

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Changelog
2020
- Exporting all algorithms in ``joserfc.jwa`` module.
2121
- Allow re-using ``JWTClaimsRegistry`` instance, via :issue:`68`.
2222
- Added ``claim`` attribute on claim errors, via :issue:`69`.
23+
- Added ``JWSRegistry.guess_alg`` method, via :issue:`49`.
2324

2425
**Breaking changes**:
2526

docs/guide/jws.rst

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,11 @@ JWS (JSON Web Signature) to remain unencoded, without using base64 encoding.
325325
To enable this option, you need to set the ``b64`` header parameter to ``false``
326326
in the JWS header.
327327

328-
To utilize the unencoded payload option in joserfc, you must import the
329-
serialize and deserialize methods from ``joserfc.rfc7797``.
330-
331328
Here are examples demonstrating the usage of the ``b64`` option:
332329

333330
.. code-block:: python
334331
335-
from joserfc.rfc7797 import serialize_compact, deserialize_compact
332+
from joserfc.jws import serialize_compact, deserialize_compact
336333
from joserfc.jwk import OctKey
337334
338335
key = OctKey.import_key("secret")
@@ -351,7 +348,7 @@ characters, the compact serialization will detach the payload:
351348

352349
.. code-block:: python
353350
354-
from joserfc.rfc7797 import serialize_compact, deserialize_compact
351+
from joserfc.jws import serialize_compact, deserialize_compact
355352
from joserfc.jwk import OctKey
356353
357354
key = OctKey.import_key("secret")
@@ -362,5 +359,19 @@ characters, the compact serialization will detach the payload:
362359
# payload when calling deserialize_compact
363360
deserialize_compact(value, key, payload="$.02")
364361
365-
There are also methods for JSON serialization: ``serialize_json`` and
362+
You can also use ``b64`` header for JSON serialization: ``serialize_json`` and
366363
``deserialize_json``.
364+
365+
Guess Algorithms via Key
366+
------------------------
367+
368+
If you are unsure which algorithm to use but already have a key, you can call the
369+
:meth:`jws.JWSRegistry.guess_alg` method to determine a suitable algorithm:
370+
371+
.. code-block:: python
372+
373+
from joserfc.jws import JWSRegistry, serialize_compact
374+
375+
alg = JWSRegistry.guess_alg(key, JWSRegistry.Strategy.RECOMMENDED)
376+
protected = {"alg": alg}
377+
serialize_compact(protected, b"payload", key)

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ It follows RFCs with extensible API. The module has implementations of:
4949
- RFC7519: :ref:`JSON Web Token <jwt>`
5050
- RFC7520: Examples of Protecting Content Using JSON Object Signing and Encryption
5151
- RFC7638: ``thumbprint`` for JWK
52+
- RFC7797: JSON Web Signature (JWS) :ref:`Unencoded Payload Option <rfc7797>`
5253
- RFC8037: :ref:`OKPKey` and ``EdDSA`` algorithm
5354
- RFC8812: ``ES256K`` algorithm
54-
- RFC9278: ``thumbprint_uri`` for JWK
55+
- RFC9278: JWK Thumbprint URI (``thumbprint_uri``)
5556

5657
And draft RFCs implementation of:
5758

src/joserfc/_rfc7515/registry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class JWSRegistry:
3131
"""
3232

3333
class Strategy(Enum):
34+
#: find the recommended algorithm
3435
RECOMMENDED = 1
36+
#: find the most secure algorithm
3537
SECURITY = 2
3638

3739
default_header_registry: HeaderRegistryDict = JWS_HEADER_REGISTRY

src/joserfc/_rfc7519/claims.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class JWTClaimsRegistry(ClaimsRegistry):
8787
8888
:param now: timestamp of "now" time
8989
:param leeway: leeway time in seconds
90-
:param **kwargs: claims options
90+
:param kwargs: claims options
9191
"""
9292

9393
def __init__(self, now: int | Callable[[], int] | None = None, leeway: int = 0, **kwargs: ClaimsOption) -> None:

0 commit comments

Comments
 (0)