-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathauthenticate.js
149 lines (134 loc) · 4.2 KB
/
authenticate.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import Boom from 'boom';
import Joi from 'joi';
import { wrapError } from '../../../lib/errors';
import { canRedirectRequest } from '../../../lib/can_redirect_request';
export function initAuthenticateApi(server) {
server.route({
method: 'POST',
path: '/api/security/v1/login',
config: {
auth: false,
validate: {
payload: Joi.object({
username: Joi.string().required(),
password: Joi.string().required()
})
},
response: {
emptyStatusCode: 204,
}
},
async handler(request, h) {
const { username, password } = request.payload;
try {
request.loginAttempt().setCredentials(username, password);
const authenticationResult = await server.plugins.security.authenticate(request);
if (!authenticationResult.succeeded()) {
throw Boom.unauthorized(authenticationResult.error);
}
return h.response();
} catch(err) {
throw wrapError(err);
}
}
});
server.route({
method: 'POST',
path: '/api/security/v1/saml',
config: {
auth: false,
validate: {
payload: Joi.object({
SAMLResponse: Joi.string().required(),
RelayState: Joi.string().allow('')
})
}
},
async handler(request, h) {
try {
// When authenticating using SAML we _expect_ to redirect to the SAML Identity provider.
const authenticationResult = await server.plugins.security.authenticate(request);
if (authenticationResult.redirected()) {
return h.redirect(authenticationResult.redirectURL);
}
return Boom.unauthorized(authenticationResult.error);
} catch (err) {
return wrapError(err);
}
}
});
server.route({
// POST is only allowed for Third Party initiated authentication
method: ['GET', 'POST'],
path: '/api/security/v1/oidc',
config: {
auth: false,
validate: {
query: Joi.object().keys({
iss: Joi.string().uri({ scheme: 'https' }),
login_hint: Joi.string(),
target_link_uri: Joi.string().uri(),
code: Joi.string(),
error: Joi.string(),
error_description: Joi.string(),
error_uri: Joi.string().uri(),
state: Joi.string()
}).unknown()
}
},
async handler(request, h) {
try {
// We handle the fact that the user might get redirected to Kibana while already having an session
// Return an error notifying the user they are already logged in.
const authenticationResult = await server.plugins.security.authenticate(request);
if (authenticationResult.succeeded()) {
return Boom.forbidden(
'Sorry, you already have an active Kibana session. ' +
'If you want to start a new one, please logout from the existing session first.'
);
}
if (authenticationResult.redirected()) {
return h.redirect(authenticationResult.redirectURL);
}
throw Boom.unauthorized(authenticationResult.error);
} catch (err) {
throw wrapError(err);
}
}
});
server.route({
method: 'GET',
path: '/api/security/v1/logout',
config: {
auth: false
},
async handler(request, h) {
if (!canRedirectRequest(request)) {
throw Boom.badRequest('Client should be able to process redirect response.');
}
try {
const deauthenticationResult = await server.plugins.security.deauthenticate(request);
if (deauthenticationResult.failed()) {
throw wrapError(deauthenticationResult.error);
}
return h.redirect(
deauthenticationResult.redirectURL || `${server.config().get('server.basePath')}/`
);
} catch (err) {
throw wrapError(err);
}
}
});
server.route({
method: 'GET',
path: '/api/security/v1/me',
handler(request) {
return request.auth.credentials;
}
});
}