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)

Decode Without Verification

from philiprehberger_jwt_lite import decode_token

payload = decode_token(token)

API

Function / Class Description
create_token(payload, secret, algorithm, expires_in) Create a signed JWT token
verify_token(token, secret, algorithm, validators) 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
ExpiredTokenError Raised when a token's exp claim is in the past
InvalidTokenError Raised when a token is malformed or signature is invalid

Development

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

Support

If you find this package useful, consider starring the repository.

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.2.0.tar.gz (6.2 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.2.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_jwt_lite-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 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.2.0.tar.gz
Algorithm Hash digest
SHA256 1471035b0de041de9d31aa761fb9557d67b99fa68d44b2ccf8473e5062dc6b72
MD5 a0b02c2f700a6f543f1c43eb0ee26373
BLAKE2b-256 d0b405c7cb4e8868eb4517cc9f2a1a215c87cfe581c82083cd5c927279a5ab59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_jwt_lite-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 daf997ac5c1de1ad9353152f126c39d9a0683219df1800eda6250ca2a3343175
MD5 9b639208395a568848b0d57dd8555351
BLAKE2b-256 d74ff7f96d6bcbca2770a3b734a0d395c88f969c73bea1152d64da486df6cb56

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