Overview
Authentication extensions verify client identities when they connect to HiveMQ. The Extension SDK supports:- Simple Authentication - Username/password authentication for MQTT 3.x and 5.0
- Enhanced Authentication - MQTT 5.0 challenge/response authentication (SASL)
- Multiple Authenticators - Chain multiple authentication mechanisms
- Async Processing - Non-blocking authentication for high performance
Authentication Flow
- Client sends CONNECT packet
- HiveMQ calls
AuthenticatorProvider.getAuthenticator()for each extension (by priority) - Authenticator validates credentials using
authenticate()method - Authentication result determines if client connects successfully
PluginAuthenticatorService.java:40 for internal authentication service.
Implementing an Authenticator
Step 1: Create AuthenticatorProvider
Register your authenticator inextensionStart():
Step 2: Implement SimpleAuthenticator
Authentication Methods
Success
Allow the client to connect:Failure
Reject the client connection:Continue Authentication
Delegate to the next authenticator in the chain:- Your authenticator doesn’t apply to this client
- You want to support multiple authentication methods
- Falling back to default behavior
Async Authentication
For non-blocking authentication (e.g., database or HTTP lookups):TimeoutFallback.FAILURE- Fail authentication on timeoutTimeoutFallback.SUCCESS- Allow connection on timeout (use with caution)
Enhanced Authentication (MQTT 5.0)
For challenge/response authentication using MQTT 5.0 AUTH packets:Enhanced Authentication Methods
Continue Authentication:Client Information Access
Access client and connection details during authentication:Database Authentication Example
Authenticate against a database:Best Practices
Security
- Never Log Passwords - Avoid logging credentials in plain text
- Use Secure Hashing - Store password hashes (BCrypt, PBKDF2, Argon2)
- Rate Limiting - Implement rate limiting to prevent brute force attacks
- Constant Time Comparison - Use constant-time comparison for passwords
- TLS Required - Enforce TLS for credential transmission
Performance
- Use Async Mode - Always use async for I/O operations
- Connection Pooling - Pool database connections
- Caching - Cache authentication results when appropriate
- Set Reasonable Timeouts - Don’t block client connections indefinitely
Error Handling
- Fail Securely - On errors, fail authentication rather than allowing access
- Generic Error Messages - Don’t leak information in error messages
- Log Authentication Failures - Monitor failed authentication attempts
- Handle Null Values - Always check for missing credentials
Testing Authentication
Test your authenticator with an MQTT client:Troubleshooting
Authentication Always Fails
- Check that
authenticateSuccessfully()is called - Verify credentials are extracted correctly from CONNECT packet
- Review HiveMQ logs for authentication errors
- Ensure authenticator provider is registered in
extensionStart()
Slow Authentication
- Use async mode for I/O operations
- Check database query performance
- Review timeout settings
- Monitor authentication latency metrics
Multiple Authenticators
When multiple extensions provide authenticators:- Authenticators execute in extension priority order (lower priority number first)
- Use
nextExtensionOrDefault()to chain authenticators - First authenticator that returns success/failure wins
- Default behavior applies if all authenticators call
nextExtensionOrDefault()
Authenticators.java:42 for authenticator registration.
Next Steps
Authorization
Control client publish and subscribe permissions
Client Initializers
Initialize client context after authentication
Extension SDK
Learn more about the Extension SDK API
Packet Interceptors
Intercept MQTT packets