Minimal JWT creation and validation with zero dependencies.
Project description
philiprehberger-jwt-lite
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.
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1471035b0de041de9d31aa761fb9557d67b99fa68d44b2ccf8473e5062dc6b72
|
|
| MD5 |
a0b02c2f700a6f543f1c43eb0ee26373
|
|
| BLAKE2b-256 |
d0b405c7cb4e8868eb4517cc9f2a1a215c87cfe581c82083cd5c927279a5ab59
|
File details
Details for the file philiprehberger_jwt_lite-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_jwt_lite-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daf997ac5c1de1ad9353152f126c39d9a0683219df1800eda6250ca2a3343175
|
|
| MD5 |
9b639208395a568848b0d57dd8555351
|
|
| BLAKE2b-256 |
d74ff7f96d6bcbca2770a3b734a0d395c88f969c73bea1152d64da486df6cb56
|