-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (29 loc) · 900 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const boom = require('boom');
/**
* @typedef {Object} Options
* @property {Boolean} enforceHttps
* @property {Array} excludePaths
*/
/**
* @param {Object} server
* @param {Options} options
* @param {Function} next
* @returns {Function}
*/
exports.register = function(server, options, next) {
if (options.enforceHttps) {
server.ext('onRequest', function(request, reply) {
const pathIsExcluded = options.excludePaths && options.excludePaths.includes(request.path);
if (!pathIsExcluded && request.connection.info.protocol !== 'https'
&& request.raw.req.headers['x-forwarded-proto'] !== 'https') {
return reply(boom.badRequest('HTTPS required'));
}
return reply.continue();
});
}
return next();
};
exports.register.attributes = {
pkg: require('./package.json')
};