-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.ts
167 lines (149 loc) · 5.53 KB
/
index.ts
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
/*
* 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 {
ClusterClient,
CoreSetup,
KibanaRequest,
LoggerFactory,
} from '../../../../../src/core/server';
import { AuthenticatedUser } from '../../common/model';
import { ConfigType } from '../config';
import { getErrorStatusCode } from '../errors';
import { Authenticator, ProviderSession } from './authenticator';
import { LegacyAPI } from '../plugin';
import { APIKeys, CreateAPIKeyParams, InvalidateAPIKeyParams } from './api_keys';
export { canRedirectRequest } from './can_redirect_request';
export { Authenticator, ProviderLoginAttempt } from './authenticator';
export { AuthenticationResult } from './authentication_result';
export { DeauthenticationResult } from './deauthentication_result';
export { OIDCAuthenticationFlow } from './providers';
export { CreateAPIKeyResult } from './api_keys';
interface SetupAuthenticationParams {
core: CoreSetup;
clusterClient: PublicMethodsOf<ClusterClient>;
config: ConfigType;
loggers: LoggerFactory;
getLegacyAPI(): LegacyAPI;
}
export async function setupAuthentication({
core,
clusterClient,
config,
loggers,
getLegacyAPI,
}: SetupAuthenticationParams) {
const authLogger = loggers.get('authentication');
const isSecurityFeatureDisabled = () => {
const xpackInfo = getLegacyAPI().xpackInfo;
return xpackInfo.isAvailable() && !xpackInfo.feature('security').isEnabled();
};
/**
* Retrieves currently authenticated user associated with the specified request.
* @param request
*/
const getCurrentUser = async (request: KibanaRequest) => {
if (isSecurityFeatureDisabled()) {
return null;
}
return (await clusterClient
.asScoped(request)
.callAsCurrentUser('shield.authenticate')) as AuthenticatedUser;
};
const authenticator = new Authenticator({
clusterClient,
basePath: core.http.basePath,
config: { sessionTimeout: config.sessionTimeout, authc: config.authc },
isSystemAPIRequest: (request: KibanaRequest) => getLegacyAPI().isSystemAPIRequest(request),
loggers,
sessionStorageFactory: await core.http.createCookieSessionStorageFactory({
encryptionKey: config.encryptionKey,
isSecure: config.secureCookies,
name: config.cookieName,
validate: (sessionValue: ProviderSession) =>
!(sessionValue.expires && sessionValue.expires < Date.now()),
}),
});
authLogger.debug('Successfully initialized authenticator.');
core.http.registerAuth(async (request, response, t) => {
// If security is disabled continue with no user credentials and delete the client cookie as well.
if (isSecurityFeatureDisabled()) {
return t.authenticated();
}
let authenticationResult;
try {
authenticationResult = await authenticator.authenticate(request);
} catch (err) {
authLogger.error(err);
return response.internalError();
}
if (authenticationResult.succeeded()) {
return t.authenticated({
state: authenticationResult.user,
requestHeaders: authenticationResult.authHeaders,
responseHeaders: authenticationResult.authResponseHeaders,
});
}
if (authenticationResult.redirected()) {
// Some authentication mechanisms may require user to be redirected to another location to
// initiate or complete authentication flow. It can be Kibana own login page for basic
// authentication (username and password) or arbitrary external page managed by 3rd party
// Identity Provider for SSO authentication mechanisms. Authentication provider is the one who
// decides what location user should be redirected to.
return response.redirected({
headers: {
location: authenticationResult.redirectURL!,
},
});
}
if (authenticationResult.failed()) {
authLogger.info(`Authentication attempt failed: ${authenticationResult.error!.message}`);
const error = authenticationResult.error!;
// proxy Elasticsearch "native" errors
const statusCode = getErrorStatusCode(error);
if (typeof statusCode === 'number') {
return response.customError({
body: error,
statusCode,
headers: authenticationResult.authResponseHeaders,
});
}
return response.unauthorized({
headers: authenticationResult.authResponseHeaders,
});
}
authLogger.debug('Could not handle authentication attempt');
return response.unauthorized({
headers: authenticationResult.authResponseHeaders,
});
});
authLogger.debug('Successfully registered core authentication handler.');
const apiKeys = new APIKeys({
clusterClient,
logger: loggers.get('api-key'),
isSecurityFeatureDisabled,
});
return {
login: authenticator.login.bind(authenticator),
logout: authenticator.logout.bind(authenticator),
getCurrentUser,
createAPIKey: (request: KibanaRequest, params: CreateAPIKeyParams) =>
apiKeys.create(request, params),
invalidateAPIKey: (request: KibanaRequest, params: InvalidateAPIKeyParams) =>
apiKeys.invalidate(request, params),
isAuthenticated: async (request: KibanaRequest) => {
try {
await getCurrentUser(request);
} catch (err) {
// Don't swallow server errors.
if (getErrorStatusCode(err) !== 401) {
throw err;
}
return false;
}
return true;
},
};
}