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.dev38.tar.gz.

File metadata

  • Download URL: swarmauri_keyprovider_remote_jwks-0.2.0.dev38.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.dev38.tar.gz
Algorithm Hash digest
SHA256 159745fc40fc2eae612e3037a0db452b22215d2aee6810a69b3b1d8880de9a5a
MD5 e2b55f6b35d0740fd184b69e517eb521
BLAKE2b-256 43b8befe47e04a7345dec99390a26040efb7a1433f2204651dd202ee2a024c95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_keyprovider_remote_jwks-0.2.0.dev38-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.dev38-py3-none-any.whl
Algorithm Hash digest
SHA256 bb6a3d7f1361f25b8e5d3f7d4fb98d93e8e09c922f9d520270532e213ed74806
MD5 08f69b1e23ba6925fafac3ecbf09d7d3
BLAKE2b-256 3a1fe88c14eadf8ff9a13b4e91d39185453912fa20109624893af7c4267d00ff

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