Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_tokens_jwt Discord

swarmauri_tokens_jwt

A standard JWT token service for the Swarmauri framework. This service implements minting and verifying JSON Web Tokens and exposes a JWKS endpoint for public key discovery.

Installation

Install the service with your preferred Python packaging tool:

pip install swarmauri_tokens_jwt
poetry add swarmauri_tokens_jwt
uv pip install swarmauri_tokens_jwt

Features

  • Mint and verify JWS/JWT tokens backed by any :class:~swarmauri_core.key_providers.IKeyProvider
  • Supports algorithms like HS256, RS256, ES256, PS256 and EdDSA
  • Adds standard temporal claims (iat, nbf, and optional exp) plus issuer, subject, audience and scope defaults when minting tokens
  • Validates expiration, not-before, issuer and audience claims during verification
  • Publishes a JWKS endpoint for public key discovery through your key provider
  • Exposes JWTClaimsValidator for transport-independent required-claim, temporal, issuer, audience, scope, role, and custom claim policy
  • Install the optional cryptography dependency to enable RSA, ECDSA and EdDSA signing keys

Usage

JWTTokenService requires an asynchronous IKeyProvider to supply signing material. The example below shows how to mint and verify a symmetric HS256 token using a minimal in-memory key provider.

import asyncio
import base64
from swarmauri_tokens_jwt import JWTTokenService
from swarmauri_core.key_providers import (
    ExportPolicy,
    IKeyProvider,
    KeyRef,
    KeyUse,
)
from swarmauri_core.crypto.types import JWAAlg, KeyType


class InMemoryKeyProvider(IKeyProvider):
    def __init__(self) -> None:
        self.secret = b"secret"
        self.kid = "sym"
        self.version = 1

    def supports(self) -> dict[str, list[str]]:
        return {}

    async def create_key(self, spec):
        raise NotImplementedError

    async def import_key(self, spec, material, *, public=None):
        raise NotImplementedError

    async def rotate_key(self, kid, *, spec_overrides=None):
        raise NotImplementedError

    async def destroy_key(self, kid, version=None) -> bool:
        return False

    async def get_key(self, kid, version=None, *, include_secret=False) -> KeyRef:
        material = self.secret if include_secret else None
        return KeyRef(
            kid=self.kid,
            version=self.version,
            type=KeyType.OPAQUE,
            uses=(KeyUse.SIGN,),
            export_policy=ExportPolicy.SECRET_WHEN_ALLOWED,
            material=material,
        )

    async def list_versions(self, kid):
        return (self.version,)

    async def get_public_jwk(self, kid, version=None):
        return {}

    async def jwks(self) -> dict:
        k = base64.urlsafe_b64encode(self.secret).rstrip(b"=").decode()
        return {"keys": [{"kty": "oct", "kid": f"{self.kid}.{self.version}", "k": k}]}

    async def random_bytes(self, n: int) -> bytes:
        return b"\x00" * n

    async def hkdf(self, ikm: bytes, *, salt: bytes, info: bytes, length: int) -> bytes:
        return b"\x00" * length


async def main() -> None:
    svc = JWTTokenService(InMemoryKeyProvider(), default_issuer="issuer")
    token = await svc.mint(
        {"sub": "alice"},
        alg=JWAAlg.HS256,
        kid="sym",
        lifetime_s=600,  # override the default one-hour lifetime if needed
    )
    claims = await svc.verify(token, issuer="issuer")
    assert claims["sub"] == "alice"


asyncio.run(main())

verify retrieves the JSON Web Key Set from the provider and enforces expiration, not-before, issuer and audience checks before returning the decoded claims. Expose the service's :meth:jwks coroutine to publish the active public keys from your provider.

Use JWTClaimsValidator when claims have already been authenticated by a JWS verifier and need application policy independent of HTTP middleware:

from swarmauri_tokens_jwt import JWTClaimsValidator

validator = JWTClaimsValidator(
    required_claims={"sub", "iat", "exp"},
    issuer="https://identity.example.com",
    audience="swarmauri-api",
    required_scopes={"documents:read"},
)
claims = validator.validate(authenticated_claims)

The service also supports asymmetric algorithms such as RS256, ES256 and EdDSA when the key provider exposes the appropriate keys. See the docstrings in :mod:swarmauri_tokens_jwt for additional details on the API surface.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

Metadata

Release files for swarmauri_tokens_jwt 0.11.0.dev3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swarmauri_tokens_jwt 0.11.0.dev3
File Size Uploaded Provenance Actions
swarmauri_tokens_jwt-0.11.0.dev3.tar.gz 10.9 kB Without provenance Details for swarmauri_tokens_jwt-0.11.0.dev3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swarmauri_tokens_jwt 0.11.0.dev3
File Interpreter ABI Platform Actions
swarmauri_tokens_jwt-0.11.0.dev3-py3-none-any.whl Python 3 none any Details for swarmauri_tokens_jwt-0.11.0.dev3-py3-none-any.whl

Total release size: 23.4 kB

Release files / swarmauri_tokens_jwt-0.11.0.dev3.tar.gz

Download URL swarmauri_tokens_jwt-0.11.0.dev3.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4e123718090d7cecb57376d480ab4ed2397a6767636a885b4f66de7670cb9f2c
BLAKE2b-256 checksum
How to use checksums
72e26bc004d150ba797bcfcce04d32d927f24c41b920f938b082eb6c7c59c655
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / swarmauri_tokens_jwt-0.11.0.dev3-py3-none-any.whl

Download URL swarmauri_tokens_jwt-0.11.0.dev3-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
911e96c69c7be87b3a9491e1739d66be6f92c94db742c7ac5f9317b63d3af159
BLAKE2b-256 checksum
How to use checksums
a014847e7d3df2b87f37c3c72c28aa9bd6c98b9ce810fe39b92a801781ca19dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for swarmauri_tokens_jwt 0.11.0.dev3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page