MicroJWT is a production-ready, lightweight JSON Web Token (JWT) library tailored for MicroPython environments. Designed for resource-constrained embedded systems, it offers secure token creation, verification, encryption, and refresh capabilities with minimal dependencies. With advanced security features like AES-256-CBC encryption, PBKDF2 key derivation, and constant-time signature verification, MicroJWT is perfect for IoT applications, secure API authentication, and session management.
- Secure JWT Handling: Implements HMAC-SHA256 (HS256) for signing and verification, compliant with RFC 7519.
- Optional Token Encryption: Encrypts tokens with AES-256-CBC for secure session storage.
- PBKDF2 Key Derivation: Strengthens secret keys for enhanced security.
- Token Refresh: Extends token validity without re-authentication.
- Audience Validation: Restricts token usage to specific recipients via the
audclaim. - Revocation Support: Manages an in-memory token revocation list for invalidating tokens.
- Constant-Time Verification: Prevents timing attacks during signature validation.
- MicroPython Optimized: Uses only standard modules (
hashlib,ucryptolib,ubinascii,json,time,os,binascii). - Configurable Logging: Includes a lightweight
SimpleLoggerwith INFO, WARNING, and ERROR levels, plus silent mode. - Robust Input Validation: Ensures reliability with comprehensive checks on user inputs.
-
Clone or Download:
git clone https://github.com/armanghobadi/microjwt.gitAlternatively, download the
jwt.pyfile directly. -
Copy to MicroPython Device: Use a tool like
ampy,rshell, or WebREPL to transferjwt.pyto your MicroPython device:ampy --port /dev/ttyUSB0 put microjwt.py -
Verify Dependencies: Ensure your MicroPython build includes:
hashlibwithsha256supportucryptolibwith AES (CBC mode)- Standard modules:
ubinascii,json,time,os,binascii
Note: If
ucryptolibis unavailable, disable encryption by settingencrypt=False.
Create and verify a standard JWT token:
from microjwt.jwt import MicroJWT
# Initialize the JWT handler
jwt = MicroJWT(secret_key="your-very-secure-secret-key-32bytes+", algorithm="HS256")
# Create a token
token = jwt.create_token(
username="user123",
role="admin",
additional_claims={"scope": "read:write"},
audience="api.example.com"
)
print("Token:", token)
# Verify the token
payload = jwt.verify_token(token, audience="api.example.com")
if payload:
print("Token is valid:", payload)
else:
print("Token is invalid")
Create and verify an encrypted token:
# Create an encrypted token
encrypted_token = jwt.create_token(
username="user123",
role="admin",
additional_claims={"scope": "read:write"},
audience="api.example.com",
encrypt=True
)
print("Encrypted Token:", encrypted_token)
# Verify the encrypted token
payload = jwt.verify_token(encrypted_token, audience="api.example.com", encrypted=True)
if payload:
print("Encrypted token is valid:", payload)
else:
print("Encrypted token is invalid")
Refresh an existing token to extend its validity:
new_token = jwt.refresh_token(encrypted_token, encrypted=True)
print("Refreshed Token:", new_token)
Invalidate a token before its expiration:
jwt.revoke_token(payload["jti"])
payload = jwt.verify_token(new_token, encrypted=True) # Returns None (revoked)
print("Revoked token is invalid")
| Parameter | Description | Default |
|---|---|---|
secret_key |
Secret key for signing (min 32 bytes) | Required |
algorithm |
HMAC algorithm (only HS256 supported) | HS256 |
ttl |
Token time-to-live in seconds | 3600 (1 hour) |
log_level |
Logging level (INFO, WARNING, ERROR) | INFO |
silent |
Suppress logging if True | False |
encrypt |
Encrypt token with AES-256-CBC | False |
audience |
Restrict token to a specific audience | None |
Below is a screenshot of MicroJWT running on a MicroPython device:
Note: To update the screenshot, run tests on your device and replace the image with your test output.
- Secret Key: Store the secret key securely (e.g., in a hardware security module or configuration file).
- Encryption: Use
encrypt=Truefor tokens stored in sessions to prevent exposure of sensitive data. - Revocation: The in-memory revocation list is non-persistent. For production, integrate with persistent storage (e.g., a file or database).
- PBKDF2 Iterations: The default 1000 iterations balances security and performance. Adjust (e.g., 500 for speed, 2000 for higher security) based on your device’s capabilities.
- Timing Attacks: Constant-time comparison ensures resistance to timing attacks during verification.
- MicroPython Build: Confirm support for
hashlib.sha256anducryptolib.aes. Ifucryptolibis unavailable, disable encryption (encrypt=False). - Performance: Test on your target hardware to ensure memory and CPU constraints are met. AES encryption and PBKDF2 are resource-intensive.
- Persistent Revocation: Implement a persistent storage solution for
revoked_tokensin long-running applications. - Logging: Set
log_level="ERROR"orsilent=Truein production to minimize console output.
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or support, contact the maintainers:
- Email: arman.ghobadi.ag@gmail.com
Thank you for using MicroJWT! We hope it empowers your secure, embedded applications.

