From 6c7cc61169c367f2fc820445fdaddac35e5b5dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pacheco?= Date: Sat, 21 Sep 2024 23:30:59 -0400 Subject: [PATCH] fix doctest for docs/usage.rst (#986) * fix doctest for docs/usage.rst * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * include crypto extra in docs tox-env --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- docs/usage.rst | 23 +++++++++++++++++------ tox.ini | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 6129f847..efae248b 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -159,7 +159,9 @@ you can set a leeway of 10 seconds in order to have some margin: >>> import time, datetime >>> from datetime import timezone - >>> payload = {"exp": datetime.datetime.now(tz=timezone.utc) + datetime.timedelta(seconds=1)} + >>> payload = { + ... "exp": datetime.datetime.now(tz=timezone.utc) + datetime.timedelta(seconds=1) + ... } >>> token = jwt.encode(payload, "secret") >>> time.sleep(2) >>> # JWT payload is now expired @@ -250,7 +252,9 @@ If multiple audiences are accepted, the ``audience`` parameter for >>> payload = {"some": "payload", "aud": "urn:foo"} >>> token = jwt.encode(payload, "secret") - >>> decoded = jwt.decode(token, "secret", audience=["urn:foo", "urn:bar"], algorithms=["HS256"]) + >>> decoded = jwt.decode( + ... token, "secret", audience=["urn:foo", "urn:bar"], algorithms=["HS256"] + ... ) >>> try: ... jwt.decode(token, "secret", audience=["urn:invalid"], algorithms=["HS256"]) ... except jwt.InvalidAudienceError: @@ -284,9 +288,14 @@ If you wish to require one or more claims to be present in the claimset, you can .. code-block:: pycon - >>> token = jwt.encode({"sub":"1234567890","iat":1371720939}, "secret") + >>> token = jwt.encode({"sub": "1234567890", "iat": 1371720939}, "secret") >>> try: - ... jwt.decode(token, "secret", options={"require": ["exp", "iss", "sub"]}, algorithms=["HS256"]) + ... jwt.decode( + ... token, + ... "secret", + ... options={"require": ["exp", "iss", "sub"]}, + ... algorithms=["HS256"], + ... ) ... except jwt.MissingRequiredClaimError as e: ... print(e) ... @@ -310,7 +319,7 @@ Retrieve RSA signing keys from a JWKS endpoint ... signing_key, ... audience="https://expenses-api", ... options={"verify_exp": False}, - ... algorithms=["RS256"]) + ... algorithms=["RS256"], ... ) {'iss': 'https://dev-87evx9ru.auth0.com/', 'sub': 'aW4Cca79xReLWUz0aE2H6kD0O3cXBVtC@clients', 'aud': 'https://expenses-api', 'iat': 1572006954, 'exp': 1572006964, 'azp': 'aW4Cca79xReLWUz0aE2H6kD0O3cXBVtC', 'gty': 'client-credentials'} @@ -340,7 +349,9 @@ is not built into pyjwt. # example of fetching data from your OIDC server # see: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig oidc_server = ... - oidc_config = requests.get(f"https://{oidc_server}/.well-known/openid-configuration").json() + oidc_config = requests.get( + f"https://{oidc_server}/.well-known/openid-configuration" + ).json() signing_algos = oidc_config["id_token_signing_alg_values_supported"] # setup a PyJWKClient to get the appropriate signing key diff --git a/tox.ini b/tox.ini index 7969baec..81d8e965 100644 --- a/tox.ini +++ b/tox.ini @@ -45,7 +45,7 @@ commands = {envpython} -b -m coverage run -m pytest {posargs} basepython = python3.11 extras = docs - crypto: crypto + crypto commands = sphinx-build -n -T -W -b html -d {envtmpdir}/doctrees docs docs/_build/html sphinx-build -n -T -W -b doctest -d {envtmpdir}/doctrees docs docs/_build/html