wildduck/lib/api/dkim.js

484 lines
16 KiB
JavaScript
Raw Normal View History

2017-12-28 19:45:02 +08:00
'use strict';
2018-01-02 19:46:32 +08:00
const config = require('wild-config');
2017-12-28 19:45:02 +08:00
const Joi = require('../joi');
2018-08-03 20:44:03 +08:00
const MongoPaging = require('mongo-cursor-pagination');
2017-12-28 19:45:02 +08:00
const ObjectID = require('mongodb').ObjectID;
2018-01-02 19:46:32 +08:00
const DkimHandler = require('../dkim-handler');
2018-08-03 20:44:03 +08:00
const tools = require('../tools');
2017-12-28 19:45:02 +08:00
module.exports = (db, server) => {
2018-01-02 19:46:32 +08:00
const dkimHandler = new DkimHandler({
cipher: config.dkim.cipher,
secret: config.dkim.secret,
2018-05-11 19:39:23 +08:00
useOpenSSL: config.dkim.useOpenSSL,
pathOpenSSL: config.dkim.pathOpenSSL,
2018-01-02 19:46:32 +08:00
database: db.database
});
2017-12-28 19:45:02 +08:00
/**
* @api {get} /dkim List registered DKIM keys
* @apiName GetDkim
* @apiGroup DKIM
* @apiHeader {String} X-Access-Token Optional access token if authentication is enabled
* @apiHeaderExample {json} Header-Example:
* {
* "X-Access-Token": "59fc66a03e54454869460e45"
* }
*
* @apiParam {String} [query] Partial match of a Domain name
* @apiParam {Number} [limit=20] How many records to return
* @apiParam {Number} [page=1] Current page number. Informational only, page numbers start from 1
* @apiParam {Number} [next] Cursor value for next page, retrieved from <code>nextCursor</code> response value
* @apiParam {Number} [previous] Cursor value for previous page, retrieved from <code>previousCursor</code> response value
*
* @apiSuccess {Boolean} success Indicates successful response
* @apiSuccess {Number} total How many results were found
* @apiSuccess {Number} page Current page number. Derived from <code>page</code> query argument
* @apiSuccess {String} previousCursor Either a cursor string or false if there are not any previous results
* @apiSuccess {String} nextCursor Either a cursor string or false if there are not any next results
* @apiSuccess {Object[]} results Aliases listing
* @apiSuccess {String} results.id ID of the DKIM
* @apiSuccess {String} results.domain The domain this DKIM key applies to
* @apiSuccess {String} results.selector DKIM selector
* @apiSuccess {String} results.description Key description
* @apiSuccess {String} results.fingerprint Key fingerprint (SHA1)
* @apiSuccess {String} results.created Datestring
*
* @apiError error Description of the error
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:8080/dkim
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": true,
* "total": 1,
* "page": 1,
* "previousCursor": false,
* "nextCursor": false,
* "results": [
* {
* "id": "59ef21aef255ed1d9d790e81",
* "domain": "example.com",
* "selector": "oct17",
* "description": "Key for marketing emails",
* "fingerprint": "6a:aa:d7:ba:e4:99:b4:12:e0:f3:35:01:71:d4:f1:d6:b4:95:c4:f5",
* "created": "2017-10-24T11:19:10.911Z"
* }
* ]
* }
*
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 200 OK
* {
* "error": "Database error"
* }
*/
2018-08-03 20:44:03 +08:00
server.get(
{ name: 'dkim', path: '/dkim' },
tools.asyncifyJson(async (req, res, next) => {
res.charSet('utf-8');
const schema = Joi.object().keys({
query: Joi.string()
.empty('')
.trim()
.max(255),
limit: Joi.number()
.default(20)
.min(1)
.max(250),
next: Joi.string()
.empty('')
.mongoCursor()
.max(1024),
previous: Joi.string()
.empty('')
.mongoCursor()
.max(1024),
page: Joi.number().default(1),
sess: Joi.string().max(255),
ip: Joi.string().ip({
version: ['ipv4', 'ipv6'],
cidr: 'forbidden'
})
2017-12-28 19:45:02 +08:00
});
2018-08-03 20:44:03 +08:00
const result = Joi.validate(req.query, schema, {
abortEarly: false,
convert: true,
allowUnknown: true
});
if (result.error) {
2017-12-28 19:45:02 +08:00
res.json({
2018-08-03 20:44:03 +08:00
error: result.error.message,
code: 'InputValidationError'
2017-12-28 19:45:02 +08:00
});
return next();
}
2018-08-03 20:44:03 +08:00
let query = result.value.query;
let limit = result.value.limit;
let page = result.value.page;
let pageNext = result.value.next;
let pagePrevious = result.value.previous;
let filter = query
? {
domain: {
$regex: query.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'),
$options: ''
}
}
: {};
2018-08-03 21:15:35 +08:00
let total = await db.database.collection('dkim').countDocuments(filter);
2018-08-03 20:44:03 +08:00
2017-12-28 19:45:02 +08:00
let opts = {
limit,
query: filter,
2017-12-28 21:16:42 +08:00
paginatedField: 'domain',
sortAscending: true
2017-12-28 19:45:02 +08:00
};
if (pageNext) {
opts.next = pageNext;
2018-08-03 20:59:33 +08:00
} else if (page > 1 && pagePrevious) {
2017-12-28 19:45:02 +08:00
opts.previous = pagePrevious;
}
2018-08-03 20:44:03 +08:00
let listing;
try {
listing = await MongoPaging.find(db.database.collection('dkim'), opts);
} catch (err) {
res.json({
error: 'MongoDB Error: ' + err.message,
code: 'InternalDatabaseError'
});
2017-12-28 19:45:02 +08:00
return next();
2018-08-03 20:44:03 +08:00
}
if (!listing.hasPrevious) {
page = 1;
}
let response = {
success: true,
query,
total,
page,
previousCursor: listing.hasPrevious ? listing.previous : false,
nextCursor: listing.hasNext ? listing.next : false,
results: (listing.results || []).map(dkimData => ({
id: dkimData._id.toString(),
domain: dkimData.domain,
selector: dkimData.selector,
description: dkimData.description,
fingerprint: dkimData.fingerprint,
created: dkimData.created
}))
};
res.json(response);
return next();
})
);
2017-12-28 19:45:02 +08:00
/**
2017-12-28 21:16:42 +08:00
* @api {post} /dkim Create or update DKIM key for domain
2017-12-28 19:45:02 +08:00
* @apiName PostDkim
* @apiGroup DKIM
2017-12-29 16:29:29 +08:00
* @apiDescription Add a new DKIM key for a Domain or update existing one. There can be single DKIM key
* registered for each domain name.
2017-12-28 19:45:02 +08:00
* @apiHeaderExample {json} Header-Example:
* {
* "X-Access-Token": "59fc66a03e54454869460e45"
* }
*
2017-12-28 21:16:42 +08:00
* @apiParam {String} domain Domain name this DKIM key applies to. Use <code>"\*"</code> as a special value that will be used for domains that do not have their own DKIM key set
2017-12-28 19:45:02 +08:00
* @apiParam {String} selector Selector for the key
* @apiParam {String} [description] Key description
2018-01-03 19:23:42 +08:00
* @apiParam {String} [privateKey] Pem formatted DKIM private key. If not set then a new 2048 bit RSA key is generated, beware though that it can take several seconds to complete.
2017-12-28 19:45:02 +08:00
*
* @apiSuccess {Boolean} success Indicates successful response
* @apiSuccess {String} id ID of the DKIM
* @apiSuccess {String} domain The domain this DKIM key applies to
* @apiSuccess {String} selector DKIM selector
* @apiSuccess {String} description Key description
* @apiSuccess {String} fingerprint Key fingerprint (SHA1)
* @apiSuccess {String} publicKey Public key in DNS format (no prefix/suffix, single line)
* @apiSuccess {Object} dnsTxt Value for DNS TXT entry
* @apiSuccess {String} dnsTxt.name Is the domain name of TXT
* @apiSuccess {String} dnsTxt.value Is the value of TXT
*
* @apiError error Description of the error
*
* @apiExample {curl} Example usage:
* curl -i -XPOST http://localhost:8080/dkim \
* -H 'Content-type: application/json' \
* -d '{
* "domain": "example.com",
* "selector": "oct17",
* "description": "Key for marketing emails",
* "privateKey": "-----BEGIN RSA PRIVATE KEY-----..."
* }'
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": true,
* "id": "59ef21aef255ed1d9d790e81",
* "domain": "example.com",
* "selector": "oct17",
* "description": "Key for marketing emails",
* "fingerprint": "6a:aa:d7:ba:e4:99:b4:12:e0:f3:35:01:71:d4:f1:d6:b4:95:c4:f5",
* "publicKey": "-----BEGIN PUBLIC KEY-----\r\nMIGfMA0...",
* "dnsTxt": {
* "name": "dec20._domainkey.example.com",
* "value": "v=DKIM1;t=s;p=MIGfMA0..."
* }
* }
*
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 200 OK
* {
* "error": "This user does not exist"
* }
*/
server.post('/dkim', (req, res, next) => {
res.charSet('utf-8');
const schema = Joi.object().keys({
domain: Joi.string()
.max(255)
//.hostname()
.required(),
selector: Joi.string()
.max(255)
//.hostname()
.trim()
.required(),
privateKey: Joi.string()
.empty('')
.trim()
2018-02-13 18:20:06 +08:00
.regex(/^-----BEGIN (RSA )?PRIVATE KEY-----/, 'DKIM key format'),
2017-12-28 19:45:02 +08:00
description: Joi.string()
.max(255)
//.hostname()
.trim(),
sess: Joi.string().max(255),
ip: Joi.string().ip({
version: ['ipv4', 'ipv6'],
cidr: 'forbidden'
})
2017-12-28 19:45:02 +08:00
});
const result = Joi.validate(req.params, schema, {
abortEarly: false,
convert: true
});
if (result.error) {
res.json({
error: result.error.message,
code: 'InputValidationError'
});
return next();
}
2018-01-02 19:46:32 +08:00
dkimHandler.set(result.value, (err, response) => {
if (err) {
2017-12-28 21:16:42 +08:00
res.json({
2018-01-02 19:46:32 +08:00
error: err.message,
code: err.code
2017-12-28 19:45:02 +08:00
});
2017-12-28 21:16:42 +08:00
return next();
2017-12-28 19:45:02 +08:00
}
2018-01-02 19:46:32 +08:00
if (response) {
response.success = true;
}
res.json(response);
return next();
});
2017-12-28 19:45:02 +08:00
});
/**
* @api {get} /dkim/:dkim Request DKIM information
* @apiName GetDkimKey
* @apiGroup DKIM
* @apiHeader {String} X-Access-Token Optional access token if authentication is enabled
* @apiHeaderExample {json} Header-Example:
* {
* "X-Access-Token": "59fc66a03e54454869460e45"
* }
*
* @apiParam {String} dkim ID of the DKIM
*
* @apiSuccess {Boolean} success Indicates successful response
* @apiSuccess {String} id ID of the DKIM
* @apiSuccess {String} domain The domain this DKIM key applies to
* @apiSuccess {String} selector DKIM selector
* @apiSuccess {String} description Key description
* @apiSuccess {String} fingerprint Key fingerprint (SHA1)
* @apiSuccess {String} publicKey Public key in DNS format (no prefix/suffix, single line)
* @apiSuccess {Object} dnsTxt Value for DNS TXT entry
* @apiSuccess {String} dnsTxt.name Is the domain name of TXT
* @apiSuccess {String} dnsTxt.value Is the value of TXT
* @apiSuccess {String} created Datestring
*
* @apiError error Description of the error
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:8080/dkim/59ef21aef255ed1d9d790e7a
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": true,
* "id": "59ef21aef255ed1d9d790e7a",
* "domain": "example.com",
* "selector": "oct17",
* "description": "Key for marketing emails",
* "fingerprint": "6a:aa:d7:ba:e4:99:b4:12:e0:f3:35:01:71:d4:f1:d6:b4:95:c4:f5",
* "publicKey": "-----BEGIN PUBLIC KEY-----\r\nMIGfMA0...",
* "dnsTxt": {
* "name": "dec20._domainkey.example.com",
* "value": "v=DKIM1;t=s;p=MIGfMA0..."
* }
* "created": "2017-10-24T11:19:10.911Z"
* }
*
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 200 OK
* {
* "error": "This Alias does not exist"
* }
*/
server.get('/dkim/:dkim', (req, res, next) => {
res.charSet('utf-8');
const schema = Joi.object().keys({
dkim: Joi.string()
.hex()
.lowercase()
.length(24)
.required(),
sess: Joi.string().max(255),
ip: Joi.string().ip({
version: ['ipv4', 'ipv6'],
cidr: 'forbidden'
})
2017-12-28 19:45:02 +08:00
});
const result = Joi.validate(req.params, schema, {
abortEarly: false,
convert: true
});
if (result.error) {
res.json({
error: result.error.message,
code: 'InputValidationError'
});
return next();
}
let dkim = new ObjectID(result.value.dkim);
2018-04-29 03:44:38 +08:00
dkimHandler.get({ _id: dkim }, false, (err, response) => {
2018-01-02 19:46:32 +08:00
if (err) {
2017-12-28 19:45:02 +08:00
res.json({
2018-01-02 19:46:32 +08:00
error: err.message,
code: err.code
2017-12-28 19:45:02 +08:00
});
return next();
}
2018-01-02 19:46:32 +08:00
if (response) {
response.success = true;
}
res.json(response);
return next();
});
2017-12-28 19:45:02 +08:00
});
/**
* @api {delete} /dkim/:dkim Delete a DKIM key
* @apiName DeleteDkim
* @apiGroup DKIM
* @apiHeader {String} X-Access-Token Optional access token if authentication is enabled
* @apiHeaderExample {json} Header-Example:
* {
* "X-Access-Token": "59fc66a03e54454869460e45"
* }
*
* @apiParam {String} dkim ID of the DKIM
*
* @apiSuccess {Boolean} success Indicates successful response
*
* @apiError error Description of the error
*
* @apiExample {curl} Example usage:
* curl -i -XDELETE http://localhost:8080/dkim/59ef21aef255ed1d9d790e81
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": true
* }
*
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 200 OK
* {
* "error": "Database error"
* }
*/
server.del('/dkim/:dkim', (req, res, next) => {
res.charSet('utf-8');
const schema = Joi.object().keys({
dkim: Joi.string()
.hex()
.lowercase()
.length(24)
.required(),
sess: Joi.string().max(255),
ip: Joi.string().ip({
version: ['ipv4', 'ipv6'],
cidr: 'forbidden'
})
2017-12-28 19:45:02 +08:00
});
const result = Joi.validate(req.params, schema, {
abortEarly: false,
convert: true
});
if (result.error) {
res.json({
error: result.error.message,
code: 'InputValidationError'
});
return next();
}
let dkim = new ObjectID(result.value.dkim);
2018-04-29 03:44:38 +08:00
dkimHandler.del({ _id: dkim }, (err, response) => {
2018-01-02 19:46:32 +08:00
if (err) {
2017-12-28 19:45:02 +08:00
res.json({
2018-01-02 19:46:32 +08:00
error: err.message,
code: err.code
2017-12-28 19:45:02 +08:00
});
return next();
}
2018-01-02 19:46:32 +08:00
res.json({
success: response
});
return next();
});
2017-12-28 19:45:02 +08:00
});
};