Authentication Overview
NATS Server supports multiple authentication mechanisms to verify client identities. You can choose the method that best fits your security requirements and infrastructure.
Authentication Methods
NATS provides four primary authentication methods:
Token-based authentication - Simple shared secret
Username/password authentication - Traditional credentials
NKeys authentication - Cryptographic public-key authentication
JWT-based authentication - Decentralized authentication with claims
Token-Based Authentication
The simplest authentication method using a shared secret token.
Server Configuration
Or via command line:
Client Connection
Token authentication is suitable for development but should not be used in production. Prefer NKeys or JWT authentication.
Username/Password Authentication
Traditional username and password credentials.
Single User
Or via command line:
Multiple Users
Password Hashing with BCrypt
Never store plaintext passwords. Use bcrypt hashing:
The server warns if plaintext passwords are detected in the configuration.
Connection Deadlines
Expire user credentials after a specific time:
NKeys Authentication
Cryptographic authentication using Ed25519 public-key signatures. This is the recommended authentication method.
How NKeys Work
Server generates a random nonce
Client signs the nonce with its private NKey
Server verifies the signature using the public NKey
No passwords transmitted over the network
Generating NKeys
Output:
Store the private key securely. Never commit it to version control.
Server Configuration
Single NKey User
Multiple NKey Users
With Account Assignment
Client Configuration
Clients use a credentials file containing the NKey:
user.creds:
Connect using credentials:
JWT-Based Authentication
JWT (JSON Web Token) authentication enables decentralized auth with signed claims. This is the most sophisticated authentication method.
Operator Mode
Operator mode uses a hierarchical trust model:
Server Configuration
Account JWT Example
Account JWTs define account-level claims:
User JWT Example
User JWTs define user-level claims and permissions:
Dynamic Updates
JWT authentication supports dynamic updates without server restarts:
Update account or user JWT in the resolver
Server automatically picks up changes
Active connections continue with old credentials
New connections use updated credentials
Account Signing Keys
Accounts can delegate user signing to scoped keys:
Authentication Callouts
Authentication can be delegated to an external service via the callout mechanism.
Server Configuration
Callout Flow
Client connects to server
Server publishes authentication request to $SYS.REQ.USER.AUTH
Auth service validates credentials
Auth service responds with signed User JWT
Server grants access based on JWT claims
Encrypted Callouts
Callout requests and responses can be encrypted:
TLS Certificate Mapping
Map TLS certificate properties to users:
Authentication Timeouts
Clients must authenticate within the timeout:
Or via command line: