Skip to content

aamalev/rsjwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rsjwt

Python versions Documentation Status

https://img.shields.io/badge/Rustc-1.80.0-blue?logo=rust https://img.shields.io/badge/cargo-clippy-blue?logo=rust Linter: ruff Code style: ruff Code style: Mypy Hatch project
Python wrapper for:

Features

  • Encode and decode JWT
  • Support HS256, HS384, HS512
  • encode datetime.timedelta to float (now + delta)
  • encode datetime.datetime with timezone (example datetime.now(timezone.utc))

Install

pip install rsjwt

Using

from datetime import timedelta

import rsjwt


v = rsjwt.JWT("my-secret")
data = {
    "exp": timedelta(hours=8),
    "s": "123",
    "a": ["123", 123],
    "m": {"a": 1},
}
token = v.encode(data)
assert isinstance(token, str)

td = v.decode(token)
assert td["a"] == data["a"]
assert td["s"] == data["s"]
assert td["m"] == data["m"]
assert isinstance(td["exp"], float)

Bench

% hatch run bench:py
───────────────────────────────────────────────────────────────────────────────── bench.py3.9 ─────────────────────────────────────────────────────────────────────────────────
Python: 3.9.13 (v3.9.13:6de2ca5339, May 17 2022, 11:37:23)
[Clang 13.0.0 (clang-1300.0.29.30)]
Algorithm: HS256
Iterations: 1000000

|      package |   secs  |   n    |
| ------------ | ------- | ------ |
|        rsjwt |  2.2350 |  1.000 |
|        pyjwt |  9.4796 |  4.241 |
|      authlib | 12.0257 |  5.381 |
|  python-jose | 14.7699 |  6.608 |
|     jwcrypto | 61.5658 | 27.546 |
──────────────────────────────────────────────────────────────────────────────── bench.py3.13 ─────────────────────────────────────────────────────────────────────────────────
Python: 3.13.0 (main, Oct  7 2024, 05:02:14) [Clang 16.0.0 (clang-1600.0.26.4)]
Algorithm: HS256
Iterations: 1000000

|      package |   secs  |   n    |
| ------------ | ------- | ------ |
|        rsjwt |  2.1902 |  1.000 |
|        pyjwt |  6.2054 |  2.833 |
|      authlib |  7.2337 |  3.303 |
|     jwcrypto | 41.4919 | 18.944 |

Development

cargo fmt
cargo clippy
maturin develop

or use hatch envs:

hatch run fmt
hatch run check
hatch run build
hatch run test