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).
When to use it
Section titled “When to use it”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.
Credentials
Section titled “Credentials”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 examplesha256:…: 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:
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:
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")}`)'Making authentication required
Section titled “Making authentication required”The filter only judges the AUTH command; whether clients must send it is a server option.
- Leave
options.authOptionalunset: clients must authenticate beforeMAIL FROM, otherwise they get530 Error: authentication Required. - With
authOptional: true, a client that does not authenticate is not judged bystatic-auth. Alone in its flow, the filter never accepts such a client, whose email is then refused afterDATA; combined with a filter that accepts it, for example awhitelistonips, the client is accepted without credentials. - Authentication needs an encrypted connection (
STARTTLSoroptions.secure), unlessoptions.allowInsecureAuthistrue. 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.
Options
Section titled “Options”Example
Section titled “Example”A debug inbox that requires the credentials of SMTP_USERNAME and SMTP_PASSWORD:
// 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 }}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.jsoncA relay to Amazon SES for authenticated clients, with TLS from the start of the connection:
{ "$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"] }}Testing by hand
Section titled “Testing by hand”Start the debug inbox above, then open a session with OpenSSL, which upgrades the connection with STARTTLS:
openssl s_client -starttls smtp -connect localhost:10025Send EHLO, then AUTH LOGIN, and answer the two prompts with the username and the password encoded in Base64:
EHLO client.example.comAUTH LOGIN334 VXNlcm5hbWU6dGVzdA==334 UGFzc3dvcmQ6dGVzdA==235 Authentication successfulprintf '%s' 'test' | base64 gives dGVzdA==. Use printf rather than echo or <<<, which add a newline that
becomes part of the encoded value.