forked from appuri/kibana-oauth2-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (89 loc) · 3.51 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
const hapiAuthCookie = require('hapi-auth-cookie');
const Boom = require('boom');
const Bell = require('bell');
const _ = require('lodash');
const esRequestInterceptor = require('./server/es_request_interceptor');
module.exports = function (kibana) {
return new kibana.Plugin({
require: ['kibana', 'elasticsearch'],
config(Joi) {
return Joi.object({
enabled: Joi.boolean().default(true),
cookieName: Joi.string().default('sid'),
password: Joi.string(),
sessionTimeout: Joi.number().default(30 * 60 * 1000),
provider: Joi.string(),
clientId: Joi.string(),
redirectUri: Joi.string(),
clientSecret: Joi.string(),
allowedIndices: Joi.array().items(Joi.string()).single(),
allowedDomains: Joi.alternatives().when('provider', {
is: 'google',
then: Joi.array().items(Joi.string()),
otherwise: Joi.any().forbidden()
})
}).default()
},
uiExports: {
chromeNavControls: ['plugins/oauth2/logout_button']
},
init: function (server, options) {
const config = server.config();
if (config.get('oauth2.password') == null) throw new Error('oauth2.password is required in kibana.yml.');
if (config.get('oauth2.provider') == null || config.get('oauth2.clientId') == null || config.get('oauth2.clientSecret') == null) {
throw new Error('Please set oauth2.provider, oauth2.clientId, and oauth2.clientSecret in kibana.yml.');
}
server.register([hapiAuthCookie, Bell], function (error) {
server.auth.strategy('session', 'cookie', 'required', {
cookie: config.get('oauth2.cookieName'),
password: config.get('oauth2.password'),
ttl: config.get('oauth2.sessionTimeout'),
path: config.get('server.basePath') + '/',
clearInvalid: true,
keepAlive: true,
redirectTo: `${config.get('server.basePath')}/login`,
isSecure: !!config.get('server.ssl.cert')
});
server.auth.strategy(config.get('oauth2.provider'), 'bell', {
provider: config.get('oauth2.provider'),
password: config.get('oauth2.password'),
clientId: config.get('oauth2.clientId'),
clientSecret: config.get('oauth2.clientSecret'),
location: config.get('oauth2.redirectUri'),
isSecure: !!config.get('server.ssl.cert')
});
});
server.route({
method: ['GET', 'POST'],
path: '/login',
config: {
auth: config.get('oauth2.provider')
},
handler: function (request, reply) {
if (!request.auth.isAuthenticated) {
return reply(Boom.unauthorized('Authentication failed: ' + request.auth.error.message));
}
var allowedIndices = config.get('oauth2.allowedDomains');
if (allowedIndices && allowedIndices.length) {
if (allowedIndices.indexOf(_.get(request.auth.credentials, 'profile.raw.domain')) === -1) {
return reply(Boom.forbidden('Domain not allowed'));
}
}
request.auth.session.set(request.auth.credentials);
return reply.redirect('./');
}
});
server.route({
method: 'GET',
path: '/logout',
handler: function (request, reply) {
request.auth.session.clear();
reply.redirect('./');
}
});
if (Array.isArray(config.get('oauth2.allowedIndices'))) {
esRequestInterceptor(server);
}
}
});
};