Skip to main content

PyJwt Utils

PyJwt-Utils is a wrapper to facility your token encode/decode.

If you use or like the project, click Star and Watch to generate metrics and i evaluate project continuity.

Install:

pip install pyjwt-utils

Usage:

  1. In your file:
    from jwt_utils.django import JwtEncode, JwtDecode
    ...
    
    token = JwtEncode(
        signing_key='...',  # Default None 
        algorithm='HS256', # Default -> 'HS256', accepts ['HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512']
        iss=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1)
        sub=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2)
        aud=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3)
        exp=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4)
        nbf=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5)
        iat=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6)
        auto_iat=False,  # Default False (generate iat with current time) - (used only if IAT is None)
        jti=None,  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7)
        auto_jti=False,  # Default False (generate jti with random hex) - (used only if JTI is None)
        from_time=None  # Default datetime.utcnow() - Base time from iss and nbf
        payload={}  # Your data
    ).encode()
    
    payload = JwtDecode(
        token='...'
        verify_iss=False,  # Default False (verify jwt iss)
        verify_aud=False,  # Default False (verify jwt aud)
        verify_exp=False,  # Default False (verify jwt exp)
        verify_nbf=False,  # Default False (verify jwt nbf)
        verify_iat=False,  # Default False (verify jwt iat)
        leeway=0  # Default 0 (validate time leeway)
    ).decode(
        raise_except=False  # Default False (check `jwt.exceptions` for available exceptions)
    )
    

Usage (Django):

OBS: This require Django and Django Rest Framework to work.

  1. Add to your INSTALLED_APPS, in settings.py:

    INSTALLED_APPS = [  
        ...
        'jwt_utils.django',
        ...
    ]
    
  2. Add this configuration on settings.py:

    from datetime import timedelta
    ... 
    
    JWT_UTILS = {
        # all settings with None or False are ignored by default.
    
        # Generating Jwt
        'SIGNING_KEY': settings.SECRET_KEY,  # Default -> settings.SECRET_KEY
        'ALGORITHM': 'HS256',  # Default -> 'HS256', accepts ['HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512']
        'ISSUER': 'myapp',  # Default None -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1)
        'SUBJECT': None,  # Default -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2)
        'AUDIENCE': ['web', 'mobile'],  # Default -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3)
        'EXPIRATION': timedelta(minutes=20),  # Default -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4)
        'NOT_BEFORE': timedelta(seconds=0),  # Default -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5)
        'IAT': None,  # Default -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6)
        'AUTO_IAT': True, Auto generate IAT claim (used only if IAT is None)
        'JTI': True,  # Default -> None (https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7)
        'AUTO_JTI': True, Auto generate JTI claim (used only if JTI is None)
    
        # Checking Jwt
        'VERIFY_ISSUER': True,  # Default `ISSUER is not None`
        'VERIFY_AUDIENCE': True,  # Default `AUDIENCE is not None`
        'VERIFY_EXPIRATION': True,  # Default `EXPIRATION is not None`
        'VERIFY_NOT_BEFORE': True,  # Default `NOT_BEFORE is not None`
        'VERIFY_IAT': True,  # Default `IAT is not None or AUTO_IAT`
        'VERIFY_MAX_LEEWAY': 0,  # Default 0, validate time leeway
    }
    
  3. In your file:

    from jwt_utils.django import JwtEncode, JwtDecode
    ...
    
    token = JwtEncode(payload={}).encode()
    
    payload = JwtDecode(token).decode(
        raise_except=False  # Default False (check `jwt.exceptions` for available exceptions)
    )
    

Metadata

Release files for pyjwt-utils 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyjwt-utils 1.0.4
File Size Uploaded Provenance Actions
pyjwt-utils-1.0.4.tar.gz 7.5 kB Without provenance Details for pyjwt-utils-1.0.4.tar.gz

Release files / pyjwt-utils-1.0.4.tar.gz

Download URL pyjwt-utils-1.0.4.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3e0209b66d428cefb4910dfdff0bf343802570fdd7e567f232d64b7407bbdf2a
BLAKE2b-256 checksum
How to use checksums
a39ae10f0b830c34a26802857ce3d21b81da94e10e6cee158294f75c7980403c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

Security for pyjwt-utils 1.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page