mailauth
Email authentication library for Node.js (work in progress)
Setup
Install from NPM
$ npm install mailauth
Authentication
Validate DKIM signatures, SPF and DMARC for an email.
const { authenticate } = require('mailauth');
const { headers } = await authenticate(
message,
{
ip: '217.146.67.33',
helo: 'uvn-67-33.tll01.zonevs.eu',
mta: 'mx.ethereal.email',
sender: 'andris@ekiri.ee'
}
);
process.stdout.write(headers);
process.stdout.write(message);
Example output:
Received-SPF: pass (mx.ethereal.email: domain of andris@ekiri.ee designates
217.146.67.33 as permitted sender) client-ip=217.146.67.33;
Authentication-Results: mx.ethereal.email;
dkim=pass header.i=@ekiri.ee header.s=default header.b="1VSEye1n"
spf=pass (mx.ethereal.email: domain of andris@ekiri.ee designates
217.146.67.33 as permitted sender) smtp.mailfrom=andris@ekiri.ee;
dmarc=none header.from=ekiri.ee
From: ...
DKIM
Signing
const { dkimSign } = require('mailauth/lib/dkim/sign');
const signResult = await dkimSign(
message,
{
algorithm: 'rsa-sha256',
canonicalization: 'relaxed/relaxed',
signTime: new Date(),
signatureData: [
{
signingDomain: 'tahvel.info',
selector: 'test.rsa',
privateKey: fs.readFileSync('./test/fixtures/private-rsa.pem')
}
]
}
);
if (signResult.errors.length) {
console.log(signResult.errors);
}
process.stdout.write(signResult.signatures);
process.stdout.write(message);
Example output:
DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=tahvel.info;
s=test.rsa; b=...
From: ...
Verifying
const { dkimVerify } = require('mailauth/lib/dkim/verify');
const result = await dkimVerify(message);
for (let { info } of result.results) {
console.log(info);
}
Example output:
dkim=neutral (invalid public key) header.i=@tahvel.info header.s=test.invalid header.b="b85yao+1"
dkim=pass header.i=@tahvel.info header.s=test.rsa header.b="BrEgDN4A"
dkim=policy (weak key) header.i=@tahvel.info header.s=test.small header.b="d0jjgPun"
SPF
Verifying
const { spf } = require('mailauth/lib/spf');
let result = await spf({
sender: 'andris@wildduck.email',
ip: '217.146.76.20',
helo: 'foo',
mta: 'mx.myhost.com'
});
console.log(result.header);
Example output:
Received-SPF: pass (mx.myhost.com: domain of andris@wildduck.email
designates 217.146.76.20 as permitted sender) client-ip=217.146.76.20;
envelope-from="andris@wildduck.email";
License
MIT