-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (71 loc) · 2.72 KB
/
index.js
File metadata and controls
81 lines (71 loc) · 2.72 KB
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
const hapiAuthCookie = require('hapi-auth-cookie');
const Boom = require('boom');
const Bell = require('bell');
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'),
encryptionKey: Joi.string(),
sessionTimeout: Joi.number().default(30 * 60 * 1000),
provider: Joi.string(),
providerId: Joi.string(),
providerSecret: Joi.string(),
}).default()
},
uiExports: {
chromeNavControls: ['plugins/oauth2/logout_button']
},
init: function (server, options) {
const config = server.config();
if (config.get('oauth2.encryptionKey') == null) throw new Error('oauth2.encryptionKey is required in kibana.yml.');
if (config.get('oauth2.provider') == null || config.get('oauth2.providerId') == null || config.get('oauth2.providerSecret') == null) {
throw new Error('Please set oauth2.provider, oauth2.providerId, and oauth2.providerSecret in kibana.yml.');
}
if (config.get('server.ssl.key') == null || config.get('server.ssl.cert') == null) {
throw new Error('HTTPS is required. Please set server.ssl.key and server.ssl.cert in kibana.yml.');
}
server.register([hapiAuthCookie, Bell], function (error) {
server.auth.strategy('session', 'cookie', 'required', {
cookie: config.get('oauth2.cookieName'),
password: config.get('oauth2.encryptionKey'),
ttl: config.get('oauth2.sessionTimeout'),
path: config.get('server.basePath') + '/',
clearInvalid: true,
keepAlive: true,
redirectTo: `${config.get('server.basePath')}/login`
});
server.auth.strategy('github', 'bell', {
provider: 'github',
password: config.get('oauth2.encryptionKey'),
clientId: config.get('oauth2.providerId'),
clientSecret: config.get('oauth2.providerSecret')
});
});
server.route({
method: ['GET', 'POST'],
path: '/login',
config: {
auth: 'github'
},
handler: function (request, reply) {
if (!request.auth.isAuthenticated) {
return reply(Boom.unauthorized('Authentication failed: ' + request.auth.error.message));
}
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('./');
}
});
}
});
};