Skip to content

static-auth

The static-auth filter checks the AUTH command against one username and password. It accepts the PLAIN and LOGIN methods and refuses XOAUTH2. Wrong credentials drop the flow; the client gets 535 only if no other flow is left (see below).

A relay used by a few applications that share one account, for example in a cluster where the applications are given the credentials as secrets. For many users, or users kept in another system, use http-auth.

The username and password come from the filter options or, when they are not set, from the environment:

Option Environment variable
username SMTP_USERNAME
password SMTP_PASSWORD
salt SMTP_PASSWORD_SALT

The server does not start if the username or the password is missing.

The password is written <algorithm>:<value>:

  • plain:<password>: the password itself. Avoid it outside tests. Only the text up to the next : is kept, so a plain password that contains : never matches: use a hash for such passwords.
  • <hash>:<hex digest>, for example sha256:…: the hex digest of the password with any hash algorithm of Node.js (node -e "console.log(require('crypto').getHashes())" lists them).
  • With a salt, the digest is the HMAC of the password keyed with the salt.

Compute a digest:

Terminal window
HASH=sha256 PASSWORD='my password' node -e 'const c=require("crypto");console.log(`${process.env.HASH}:${c.createHash(process.env.HASH).update(process.env.PASSWORD).digest("hex")}`)'

With a salt:

Terminal window
HASH=sha256 SALT='my salt' PASSWORD='my password' node -e 'const c=require("crypto");console.log(`${process.env.HASH}:${c.createHmac(process.env.HASH,process.env.SALT).update(process.env.PASSWORD).digest("hex")}`)'

The filter only judges the AUTH command; whether clients must send it is a server option.

  • Leave options.authOptional unset: clients must authenticate before MAIL FROM, otherwise they get 530 Error: authentication Required.
  • With authOptional: true, a client that does not authenticate is not judged by static-auth. Alone in its flow, the filter never accepts such a client, whose email is then refused after DATA; combined with a filter that accepts it, for example a whitelist on ips, the client is accepted without credentials.
  • Authentication needs an encrypted connection (STARTTLS or options.secure), unless options.allowInsecureAuth is true. See TLS.

Credentials are refused, with 535, only when no flow is left after the AUTH filters. An AND flow whose AUTH filter refuses them is dropped (its outputs do not run), but the session goes on through the other flows: an OR flow (a refusal does not drop it) or a flow without an AUTH filter lets any credentials through, the client gets 235 and can send through those flows. If no flow has an AUTH filter, any username and password are accepted. To require valid credentials, give every flow an AUTH filter with the default AND operator.

OptionTypeDescription
namestring

Name of the component, used in the logs and the output metrics, defaults to <type>_<position in the list>

passwordstring

Password to accept, as plain:<password> or <hash algorithm>:<hex digest> (e.g. sha256:<hex sha256 of the password>), any algorithm of node crypto.getHashes() is accepted

Read from the SMTP_PASSWORD environment variable when not set

saltstring

Salt of a hashed password: the digest is then the HMAC of the password keyed with this salt

Read from the SMTP_PASSWORD_SALT environment variable when not set

typerequired"static-auth"

Type of the component

usernamestring

Username to accept

Read from the SMTP_USERNAME environment variable when not set

A debug inbox that requires the credentials of SMTP_USERNAME and SMTP_PASSWORD:

configs/fake-smtp-with-auth.jsonc
// Can replace fakeSMTP.jar
{
"$schema": "https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json",
"flows": {
"localhost": {
"filters": [
// Allow only SMTP_USERNAME, SMTP_PASSWORD env
{
"type": "static-auth"
}
],
"outputs": [
{
"type": "file",
// Send store files in emails/
"path": "./received_emails/"
}
]
}
},
// Listen on every interface so the server is reachable from outside its container
"bind": "0.0.0.0",
"options": {
"disableReverseLookup": false,
"allowInsecureAuth": true
}
}
Terminal window
docker run -e SMTP_USERNAME=test -e SMTP_PASSWORD=plain:test -p 10025:10025 \
-v "$(pwd)/emails:/smtp-relay/received_emails" \
loopingz/smtp-relay:latest configs/fake-smtp-with-auth.jsonc

A relay to Amazon SES for authenticated clients, with TLS from the start of the connection:

ses-with-auth.jsonc
{
"$schema": "https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json",
"flows": {
"relay": {
// Credentials from SMTP_USERNAME and SMTP_PASSWORD
"filters": [{ "type": "static-auth" }],
"outputs": [{ "type": "aws", "ses": true }]
}
},
"port": 465,
"bind": "0.0.0.0",
"options": {
"secure": true,
"key": "file:///etc/smtp-relay/tls.key",
"cert": "file:///etc/smtp-relay/tls.crt",
"authMethods": ["PLAIN", "LOGIN"]
}
}

Start the debug inbox above, then open a session with OpenSSL, which upgrades the connection with STARTTLS:

Terminal window
openssl s_client -starttls smtp -connect localhost:10025

Send EHLO, then AUTH LOGIN, and answer the two prompts with the username and the password encoded in Base64:

EHLO client.example.com
AUTH LOGIN
334 VXNlcm5hbWU6
dGVzdA==
334 UGFzc3dvcmQ6
dGVzdA==
235 Authentication successful

printf '%s' 'test' | base64 gives dGVzdA==. Use printf rather than echo or <<<, which add a newline that becomes part of the encoded value.