Skip to main content

Remote JWKS-backed key provider for verification-only use

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_keyprovider_remote_jwks


Swarmauri Remote JWKS Key Provider

Key provider backed by a remote JWKS endpoint with local key management.

Features

  • Accepts either a direct JWKS URL or an OpenID Connect issuer and resolves the discovery document automatically.
  • Caches the remote JWKS in memory with TTL support, conditional requests, and thread-safe refreshes through refresh(force=True).
  • Supports versioned key identifiers such as kid.version when discovering remote public keys.
  • Embeds the standard LocalKeyProvider so that services can create, rotate, import, and destroy local keys without leaving memory.
  • Exposes convenience helpers like random_bytes() and hkdf() for local cryptographic operations alongside the remote verification flow.

Installation

Install the package with your preferred Python packaging tool:

pip install swarmauri_keyprovider_remote_jwks
poetry add swarmauri_keyprovider_remote_jwks
uv pip install swarmauri_keyprovider_remote_jwks

Usage

The provider fetches verification keys from a remote JWKS URL or through an OpenID Connect (OIDC) issuer. It also embeds an in-memory key provider to create and manage local keys. The example below fetches a JWK from a JWKS endpoint and prints its public fields:

import asyncio
from swarmauri_keyprovider_remote_jwks import RemoteJwksKeyProvider


async def main() -> None:
    provider = RemoteJwksKeyProvider(
        jwks_url="https://example.com/.well-known/jwks.json"
    )

    # Optional: pre-fetch the JWKS; otherwise the first key lookup triggers it
    provider.refresh(force=True)

    jwk = await provider.get_public_jwk("test", version=1)
    print(jwk)


asyncio.run(main())

You can also construct the provider from an OIDC issuer. The provider resolves the issuer's discovery document to find the JWKS URL:

RemoteJwksKeyProvider(issuer="https://issuer.example.com")

Locally created keys are available via the standard key provider APIs and are included alongside remote keys when calling jwks().

Local vs remote keys

Remote keys and locally created keys serve different purposes:

  • Remote keys are read-only public keys discovered from the JWKS endpoint. They normally belong to an external identity provider and are used only for verification. The provider never has access to their private material.
  • Local keys are generated by the embedded LocalKeyProvider. They include secret material (when permitted by the ExportPolicy) and can be used for signing, encryption, or key agreement. These keys live only in memory unless you export them.

The following examples illustrate both flows.

Working with remote keys

import asyncio
import jwt
from swarmauri_keyprovider_remote_jwks import RemoteJwksKeyProvider


async def main() -> None:
    provider = RemoteJwksKeyProvider(
        jwks_url="https://example.com/.well-known/jwks.json",
    )

    # Fetch a remote public key
    jwk = await provider.get_public_jwk("remote-kid")
    public_key = jwt.algorithms.RSAAlgorithm.from_jwk(jwk)

    token = "..."  # JWT issued by the remote service
    payload = jwt.decode(token, public_key, algorithms=["RS256"], audience="api")
    print(payload)


asyncio.run(main())

Managing local keys

Local keys can be created, rotated, and destroyed without network calls. They are ideal when your service needs to issue tokens or perform encryption.

The snippet below signs and verifies a JWT using an HMAC key that never leaves memory:

import asyncio
import jwt
from swarmauri_keyprovider_remote_jwks import RemoteJwksKeyProvider
from swarmauri_core.key_providers.types import KeySpec, KeyClass, KeyAlg, ExportPolicy
from swarmauri_core.crypto.types import KeyUse


async def main() -> None:
    provider = RemoteJwksKeyProvider(
        jwks_url="https://example.com/.well-known/jwks.json",
    )

    spec = KeySpec(
        klass=KeyClass.symmetric,
        alg=KeyAlg.HMAC_SHA256,
        uses=(KeyUse.SIGN,),
        export_policy=ExportPolicy.SECRET_WHEN_ALLOWED,
    )
    key = await provider.create_key(spec)
    ref = await provider.get_key(key.kid, include_secret=True)

    token = jwt.encode({"sub": "123"}, ref.material, algorithm="HS256")
    decoded = jwt.decode(token, ref.material, algorithms=["HS256"])
    print(decoded)


asyncio.run(main())

Locally generated keys appear next to remote keys when exporting a JWKS:

jwks = await provider.jwks()
print(jwks)

Refreshing remote keys

Remote keys are cached for cache_ttl_s seconds. Call refresh(force=True) to fetch the latest JWKS before a latency-sensitive operation:

provider = RemoteJwksKeyProvider(
    jwks_url="https://example.com/.well-known/jwks.json",
    cache_ttl_s=60,            # default: 300 seconds
    request_timeout_s=2,       # default: 5 seconds
    user_agent="MyGateway/1.0",
)

# Block until the JWKS has been refreshed
provider.refresh(force=True)

Want to help?

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

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

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

File details

Details for the file swarmauri_keyprovider_remote_jwks-0.2.0.dev41.tar.gz.

File metadata

  • Download URL: swarmauri_keyprovider_remote_jwks-0.2.0.dev41.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

File hashes

Hashes for swarmauri_keyprovider_remote_jwks-0.2.0.dev41.tar.gz
Algorithm Hash digest
SHA256 b4070af07fb1e5418f178411232f02f4c634a54020bc284ee572f47fe955d3b2
MD5 3f30e6f10ca5445d54158813f0ec0697
BLAKE2b-256 972566c95abc9f624ba6688f9a3c2c8eca9da6aff700db2eaa13d1d5fbce5d0a

See more details on using hashes here.

File details

Details for the file swarmauri_keyprovider_remote_jwks-0.2.0.dev41-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_keyprovider_remote_jwks-0.2.0.dev41-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

File hashes

Hashes for swarmauri_keyprovider_remote_jwks-0.2.0.dev41-py3-none-any.whl
Algorithm Hash digest
SHA256 51d42969cc9c43028ce15cc88b1ae63af1540496b04a07e999a04ddd452f1ccc
MD5 a9ea788419ad409d40a4635e9f6cce07
BLAKE2b-256 aba33c6f9a8c7a6aaf7ddc1d5b692287d9fbeab78367beea887afc03ce746b64

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