Skip to main content

Minimal JWT creation and validation with zero dependencies.

Project description

philiprehberger-jwt-lite

Tests PyPI version GitHub release Last updated License Bug Reports Feature Requests Sponsor

Minimal JWT creation and validation with zero dependencies.

Installation

pip install philiprehberger-jwt-lite

Usage

from philiprehberger_jwt_lite import create_token, verify_token

token = create_token({"sub": "user123"}, "my-secret")
payload = verify_token(token, "my-secret")

Token Expiration

from philiprehberger_jwt_lite import create_token, verify_token, ExpiredTokenError

token = create_token({"sub": "user123"}, "my-secret", expires_in=3600)

try:
    payload = verify_token(token, "my-secret")
except ExpiredTokenError:
    print("Token has expired")

Custom Algorithm

from philiprehberger_jwt_lite import create_token, verify_token

token = create_token({"sub": "user123"}, "my-secret", algorithm="HS512")
payload = verify_token(token, "my-secret", algorithm="HS512")

Custom Claims Validation

from philiprehberger_jwt_lite import create_token, verify_token

token = create_token({"sub": "user123", "role": "admin"}, "my-secret")

payload = verify_token(
    token,
    "my-secret",
    validators={"role": lambda r: r == "admin"},
)

Token Refresh

from philiprehberger_jwt_lite import create_token, refresh_token

token = create_token({"sub": "user123"}, "my-secret", expires_in=3600)
new_token = refresh_token(token, "my-secret", extends_by=7200)

JTI Auto-Generation

from philiprehberger_jwt_lite import create_token, decode_token

token = create_token({"sub": "user123"}, "my-secret", include_jti=True)
payload = decode_token(token)
print(payload["jti"])  # e.g. "a1b2c3d4-..."

Token Revocation

from philiprehberger_jwt_lite import create_token, verify_token, TokenRevokedError

revoked: set[str] = set()
token = create_token({"sub": "user123"}, "my-secret", include_jti=True)

# Later, revoke the token by its jti
# revoked.add(jti)

try:
    payload = verify_token(token, "my-secret", is_revoked=lambda jti: jti in revoked)
except TokenRevokedError:
    print("Token has been revoked")

Decode Without Verification

from philiprehberger_jwt_lite import decode_unverified

header, payload = decode_unverified(token)
print(header["alg"])  # "HS256"

API

Function / Class Description
create_token(payload, secret, algorithm, expires_in, include_jti) Create a signed JWT token
verify_token(token, secret, algorithm, validators, is_revoked) Verify signature and expiration, run custom claim validators, return payload
refresh_token(token, secret, extends_by, algorithm) Verify and re-sign a token with a new expiration
decode_token(token) Decode payload without signature verification
decode_unverified(token) Decode header and payload without signature validation
ExpiredTokenError Raised when a token's exp claim is in the past
InvalidTokenError Raised when a token is malformed or signature is invalid
TokenRevokedError Raised when a token has been revoked

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.

LinkedIn More packages

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_jwt_lite-0.3.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_jwt_lite-0.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_jwt_lite-0.3.0.tar.gz.

File metadata

  • Download URL: philiprehberger_jwt_lite-0.3.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_jwt_lite-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0f46ab707c053ccc8f0a701db1c096260b7823fc3ee53de1b7ef59c82cb8f671
MD5 770e373943c97ff2c31342e0292a382f
BLAKE2b-256 d962cdb8dbae1957ec274d923c443db552d488a1e42216640f8eb6dd91a5533b

See more details on using hashes here.

File details

Details for the file philiprehberger_jwt_lite-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_jwt_lite-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24219a4a14b308d1144e6f8de1c7fb540ea93b34143a868ae1eacb097b174286
MD5 91dfbfc5bed7726d0ad50e77585df450
BLAKE2b-256 4aa3b3a800ae9756b2f8b897c1b9769af3b4a897379db23d97e6eb2565c85110

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page