Skip to main content

nats-callout

A Python library for implementing NATS Auth Callout consumers, enabling dynamic authentication and authorization for NATS clients.

What is Auth Callout?

Auth callout is a mechanism in NATS that delegates authentication and authorization to an external service, instead of relying only on static configuration. This allows you to integrate existing identity and access systems like databases, OAuth providers, LDAP, or even simple local checks.

It’s especially valuable in dynamic environments (e.g., browser WebSocket clients) where you need to grant fine-grained and time-sensitive permissions to specific subjects, streams, or key-value stores, rather than relying on preconfigured user definitions.

How it works:

  1. A client connects to the NATS server using basic credentials (e.g., token, password).
  2. The NATS server issues an authentication request by publishing a message on the system subject: $SYS.REQ.USER.AUTH
  3. An external auth consumer subscribes to this subject, receives the request, and decides if the client is allowed.
  4. The consumer replies with an authorization response, including permissions and limitations that will be enforced for that client session.


Installation

Install with uv:

uv add nats-callout

Or with pip:

pip install nats-callout

Usage Example (FastStream Framework)

Below is an example of how to use nats-callout with FastStream to implement an Auth Callout consumer:

from faststream import FastStream, NatsBroker, Depends
from nats_callout import BaseAuthCalloutService, AdaptixEncoder, AuthError
from nats_callout.claims import AuthRequestData, UserData, PubSubPermissions

broker = NatsBroker("nats://localhost:4222")
app = FastStream(broker)

# encoder for auth callout service
# by default, we use adaptix with standard json encoding
encoder = AdaptixEncoder()

# Implement your own AuthCalloutService
class AuthCalloutService(BaseAuthCalloutService):
    def __init__(
            self,
            nats_config: NatsConfig,
            db_context: DBContext,
    ):
        self.encoder = encoder
        self.nkey_seed = nats_config.nkey_seed
        self.db_context = db_context

    async def _handle_auth_request_data(self, auth_request_data: AuthRequestData) -> UserData:
        connect_opts = auth_request_data.connect_opts

        # any checks here
        token = connect_opts.auth_token
        if not token:
            raise AuthError("no token provided")

        user = await self.db_context.get_user_by_token(token)

        if not user:
            raise AuthError("invalid token")

        user_data = UserData(
            pub=PubSubPermissions(allow=["foo"], deny=[]),
            sub=PubSubPermissions(allow=["bar"], deny=[]),
            version=auth_request_data.version,
            tags=auth_request_data.tags,
        )
        return user_data


# inject your service and other dependencies to the handler conveniently (e.g., using FastStream Depends or Dishka)
@broker.subscriber("$SYS.REQ.USER.AUTH")
async def handle_auth_request(body: str, auth_callout_service: AuthCalloutService):
    response = await auth_callout_service(body)
    return response


if __name__ == "__main__":
    app.run()

TODOs

  • Add support for synchronous (sync) callout service
  • Add the possibility to include custom JWT claims (e.g., nbf and others)
  • Make the adaptix an optional dependency

Reference


License

MIT

Metadata

Release files for nats-callout 0.1.1

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

Source distribution (sdist)

Source distribution for nats-callout 0.1.1
File Size Uploaded Provenance Actions
nats_callout-0.1.1.tar.gz 19.0 kB Without provenance Details for nats_callout-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nats-callout 0.1.1
File Interpreter ABI Platform Actions
nats_callout-0.1.1-py3-none-any.whl Python 3 none any Details for nats_callout-0.1.1-py3-none-any.whl

Total release size: 33.7 kB

Release files / nats_callout-0.1.1.tar.gz

Download URL nats_callout-0.1.1.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2123a7a5ca2e89b9164b6a48884b25d0e3b7d85a1ccfa6769e2fcc5c9bd0a4d3
BLAKE2b-256 checksum
How to use checksums
358e32fd83a092ef07ee6ea67759b94ab14bc94d318c0a565d20383799713aee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.1

Release files / nats_callout-0.1.1-py3-none-any.whl

Download URL nats_callout-0.1.1-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f77a01b66d7e483ef960ba3e3aa9e64087a8a93de61676f7223a0897be579d67
BLAKE2b-256 checksum
How to use checksums
356699ebfb63cb0309f63142e8e24b9a5520bbc62d8d51dc217f085687078fae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.1

Security for nats-callout 0.1.1

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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