Supported Mechanisms
The SMTP server supports the following SASL authentication mechanisms:- PLAIN - Simple username/password authentication (RFC 4616)
- LOGIN - Legacy username/password authentication
- XOAUTH2 - OAuth 2.0 bearer token authentication
- CRAM-MD5 - Challenge-response authentication (RFC 2195)
- XCLIENT - Proxy-based username validation
Authentication Handler
All authentication mechanisms call the server’sonAuth handler with mechanism-specific parameters.
Handler Parameters
Authentication method being used:
PLAIN, LOGIN, XOAUTH2, CRAM-MD5, or XCLIENTThe username provided by the client
The password provided by the client (not used in XOAUTH2 or CRAM-MD5)
OAuth 2.0 bearer token (XOAUTH2 only)
Server-generated challenge string (CRAM-MD5 only)
Client’s HMAC-MD5 response to challenge (CRAM-MD5 only)
Function to validate password against challenge response (CRAM-MD5 only)
Response Object
User object to attach to session. If falsy, authentication fails.
SMTP response code (default: 535 for errors)
Custom error message for authentication failures
Additional data to return to client (XOAUTH2 only)
PLAIN Authentication
PLAIN authentication transmits credentials in base64-encoded format with null separators.Protocol Flow
- Client sends:
AUTH PLAIN [token]orAUTH PLAIN - If no token provided, server responds with
334challenge - Client sends base64-encoded credentials:
\0username\0password - Server validates and responds with
235on success or535on failure
Implementation
lib/sasl.js
The PLAIN mechanism accepts credentials in the format
authzid\0authcid\0password. If authzid is empty, authcid is used as the username.LOGIN Authentication
LOGIN is a legacy authentication mechanism that prompts for username and password separately.Protocol Flow
- Client sends:
AUTH LOGINorAUTH LOGIN [base64_username] - Server responds with
334 VXNlcm5hbWU6(“Username:” in base64) - Client sends base64-encoded username
- Server responds with
334 UGFzc3dvcmQ6(“Password:” in base64) - Client sends base64-encoded password
- Server validates and responds with
235on success or535on failure
Implementation
lib/sasl.js
XOAUTH2 Authentication
XOAUTH2 uses OAuth 2.0 bearer tokens for authentication, commonly used with Gmail and Microsoft services.Protocol Flow
- Client sends:
AUTH XOAUTH2 [token]orAUTH XOAUTH2 - If no token provided, server responds with
334 - Client sends base64-encoded token:
user=username\1auth=Bearer accessToken\1\1 - Server validates token and responds with
235on success - On failure, server may respond with
334and JSON error data
Token Format
The XOAUTH2 token is parsed from base64-encoded format:lib/sasl.js
Error Handling
XOAUTH2 failures can return JSON error data to the client:XOAUTH2 is the recommended authentication method for modern OAuth-enabled email services.
CRAM-MD5 Authentication
CRAM-MD5 uses challenge-response authentication with HMAC-MD5, avoiding plaintext password transmission.Protocol Flow
- Client sends:
AUTH CRAM-MD5 - Server generates challenge:
<random.timestamp@hostname> - Server responds with
334and base64-encoded challenge - Client computes HMAC-MD5 of challenge using password as key
- Client responds with:
username hmac-digest - Server validates response and sends
235on success or535on failure
Challenge Generation
lib/sasl.js
Validation
The server provides avalidatePassword function to verify the challenge response:
lib/sasl.js
CRAM-MD5 requires the server to have access to plaintext passwords for validation, which may not be suitable for all authentication backends.
XCLIENT Authentication
XCLIENT is a non-standard authentication method used for SMTP proxy scenarios where the proxy validates the username.Implementation
lib/sasl.js
Authentication Abort
Clients can abort authentication by sending* during any multi-step authentication exchange:
Session Updates
On successful authentication, the session is updated with user information:lib/sasl.js
Error Codes
Authentication successful
Continue with authentication (send next credential or challenge)
Invalid userdata format
Syntax error or authentication aborted
Authentication credentials invalid (default failure code)