Skip to main content

A small, opinionated JWT toolkit: create, verify, expire, and handle errors without writing the same PyJWT boilerplate every project.

Project description

fastjwt-kit

A small, opinionated wrapper around PyJWT that handles the boilerplate every project rewrites: token creation, expiry, claim validation, and clean error handling — without you having to import jwt exceptions directly.

Install

pip install fastjwt-kit

Quickstart

from fastjwt_kit import create_access_token, verify_token, TokenExpiredError

token = create_access_token(subject="user-123", secret="change-me")

try:
    claims = verify_token(token, secret="change-me")
    print(claims["sub"])  # "user-123"
except TokenExpiredError:
    print("Token expired — ask the user to log in again")

That's it — no manual exp math, no catching raw jwt.ExpiredSignatureError.

Access + refresh tokens

from fastjwt_kit import create_access_token, create_refresh_token

access = create_access_token(subject="user-123", secret="change-me")          # 15 min default
refresh = create_refresh_token(subject="user-123", secret="change-me")        # 7 days default

Custom expiry, issuer, audience, and extra claims

import datetime as dt
from fastjwt_kit import create_token, verify_token

token = create_token(
    subject="user-123",
    secret="change-me",
    expires_in=dt.timedelta(hours=1),
    issuer="my-app",
    audience="my-app-clients",
    extra_claims={"role": "admin"},
)

claims = verify_token(token, secret="change-me", issuer="my-app", audience="my-app-clients")

Error handling

All errors inherit from TokenError, so you can catch broadly or specifically:

from fastjwt_kit import TokenError, TokenExpiredError, InvalidTokenError, InvalidClaimsError

try:
    claims = verify_token(token, secret="change-me")
except TokenExpiredError:
    ...  # token was valid but has expired
except InvalidClaimsError:
    ...  # issuer/audience didn't match
except InvalidTokenError:
    ...  # malformed or tampered token
except TokenError:
    ...  # catch-all

API

Function Description
create_token(subject, secret, *, expires_in, ...) Create a JWT with full control over claims.
create_access_token(subject, secret, ...) Create a short-lived token (default 15 min), tagged type: access.
create_refresh_token(subject, secret, ...) Create a long-lived token (default 7 days), tagged type: refresh.
verify_token(token, secret, *, algorithm, issuer, audience) Decode and validate a token, raising fastjwt_kit exceptions.

Why not just use PyJWT directly?

You can! This package just wraps the parts everyone ends up rewriting: expiry defaults, issuer/audience checks, and exception types that don't leak the underlying library. If you only need raw encode/decode, PyJWT alone is enough.

Roadmap

  • Password hashing module
  • FastAPI signup/login router + get_current_user dependency
  • Config/env validation helpers

Feedback and issues welcome — this is an early release and will evolve based on real usage.

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

fastjwt_kit-0.1.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

fastjwt_kit-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file fastjwt_kit-0.1.2.tar.gz.

File metadata

  • Download URL: fastjwt_kit-0.1.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for fastjwt_kit-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3442c93b499294d93c4c4fe804a97c70c114b9d7852ae11c16ff73be8e74e7e8
MD5 03f21ff4ef6e1bb68f769206bad71325
BLAKE2b-256 c6ca285188a17425ffc57fd4b255b859a92d8a25028829430fdca63baf7dd085

See more details on using hashes here.

File details

Details for the file fastjwt_kit-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fastjwt_kit-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for fastjwt_kit-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1024b76e03dc7eba5c5fd9ce64be5715b6e04058e2e8ea222d303d95a61ccf77
MD5 bad27f167183b2372084e8cacbe43f49
BLAKE2b-256 095936d7b9697940d904088efd36eaf1d1812f39842b3ee6eedfdd7f583390f3

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