-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (157 loc) · 4.92 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
/**
* @module 'hapi-common-auth'
* @description
* An auth wrapper for hapi.js.
*/
const jwtAuth = require('hapi-auth-jwt2');
const bearerAuth = require('hapi-auth-bearer-token');
const Joi = require('joi');
/**
* @typedef {Object} JwtOptions
* @property {string} publicKey
* @property {Array.<string>} nonExpiringIds Non expiring Id's list
*/
/**
* @typedef {Object} BearerOptions
* @property {Object.<string, string>} tokens
* @property {Object} additionalCredentials
*/
/**
* @typedef {Object} Plan3KeyOptions
* @property {Object.<string, string>} tokens
* @property {Object} additionalCredentials
*/
/**
* @typedef {Object} AuthOptions
* @property {Object} defaultAuth
* @property {JwtOptions} jwt
* @property {BearerOptions} bearer
* @property {Plan3KeyOptions} plan3Key
*/
/**
* @param {string} base64
* @return {string}
*/
const base64toPem = function(base64) {
let result = '';
for (let lines = 0; result.length - lines < base64.length; lines++) {
result += base64.substr(result.length - lines, 64) + '\n';
}
return '-----BEGIN PUBLIC KEY-----\n' + result + '-----END PUBLIC KEY-----';
};
/**
* @param {Object} server
* @param {JwtOptions} options
* @return {Promise}
*/
const registerJwtStrategy = function(server, options) {
return server.register(jwtAuth)
.then(() => {
server.auth.strategy('jwt', 'jwt', {
key: base64toPem(options.publicKey),
validateFunc: (decoded, request, callback) => {
if (!decoded.exp) {
const isValid = options.nonExpiringIds
&& decoded.jti && options.nonExpiringIds.includes(decoded.jti);
return callback(null, isValid);
}
return callback(null, true);
},
verifyOptions: {algorithms: ['RS256', 'RS384', 'RS512']},
tokenType: 'Plan3JWT'
});
return null;
});
};
/**
* @param {Function} startWith
* @param {Object} server
* @param {BearerOptions} options
* @return {Promise}
*/
const registerBearerStrategy = function(startWith, server, options) {
return startWith()
.then(() => {
server.auth.strategy('bearer', 'bearer-access-token', {
validateFunc: (token, callback) => {
if (options.tokens.hasOwnProperty(token)) {
return callback(null, true, Object.assign({newsroom: options.tokens[token]},
options.additionalCredentials));
}
return callback(null, false);
}
});
return null;
});
};
/**
* @param {Function} startWith
* @param {Object} server
* @param {Plan3KeyOptions} options
* @return {Promise}
*/
const registerPlan3KeyStrategy = function(startWith, server, options) {
return startWith()
.then(() => {
server.auth.strategy('plan3Key', 'bearer-access-token', {
tokenType: 'Plan3Key',
validateFunc: (token, callback) => {
if (options.tokens.hasOwnProperty(token)) {
return callback(null, true, Object.assign({newsroom: options.tokens[token]},
options.additionalCredentials));
}
return callback(null, false);
}
});
return null;
});
};
/**
* @param {Object} server
* @param {AuthOptions} options
* @param {function} next
*/
module.exports.register = function(server, options, next) {
let bearerRegistered = false;
const registerBearer = function() {
if (bearerRegistered) {
return Promise.resolve();
}
bearerRegistered = true;
return server.register(bearerAuth);
};
const result = Joi.validate(options, require('./schemas'));
if (result.error) {
const error = new Error('Provided configuration is invalid');
error.previous = result.error;
next(error);
return;
}
const strategies = [];
const defaultAuth = {
strategies: []
};
if (options.jwt) {
strategies.push(registerJwtStrategy(server, options.jwt));
defaultAuth.strategies.push('jwt');
}
if (options.bearer) {
strategies.push(registerBearerStrategy(registerBearer, server, options.bearer));
defaultAuth.strategies.push('bearer');
}
if (options.plan3Key) {
strategies.push(registerPlan3KeyStrategy(registerBearer, server, options.plan3Key));
defaultAuth.strategies.push('plan3Key');
}
Promise.all(strategies)
.then(() => {
server.auth.default(Object.assign(defaultAuth, options.defaultAuth));
return null;
})
.then(next)
.catch(next);
};
module.exports.register.attributes = {
pkg: require('./package.json')
};