-
Notifications
You must be signed in to change notification settings - Fork 383
/
SecurityUtils.js
207 lines (188 loc) · 7.22 KB
/
SecurityUtils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright 2016, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
const ConfigUtils = require('./ConfigUtils');
const URL = require('url');
const assign = require('object-assign');
const {head, isNil} = require('lodash');
/**
* This utility class will get information about the current logged user directly from the store.
*/
const SecurityUtils = {
/**
* Stores the logged user security information.
*/
setStore: function(store) {
this.store = store;
},
/**
* Gets security state form the store.
*/
getSecurityInfo() {
return this.store && this.store.getState().security || {};
},
/**
* Returns the current user or undefined if not available.
*/
getUser() {
const securityInfo = this.getSecurityInfo();
return securityInfo && securityInfo.user;
},
/**
* Returns the current user basic authentication header value.
*/
getBasicAuthHeader() {
const securityInfo = this.getSecurityInfo();
return securityInfo && securityInfo.authHeader;
},
/**
* Returns the current user access token value.
*/
getToken() {
const securityInfo = this.getSecurityInfo();
return securityInfo && securityInfo.token;
},
/**
* Returns the current user refresh token value.
* The refresh token is used to get a new access token.
*/
getRefreshToken() {
const securityInfo = this.getSecurityInfo();
return securityInfo && securityInfo.refresh_token;
},
/**
* Return the user attributes as an array. If the user is undefined or
* doesn't have any attributes an empty array is returned.
*/
getUserAttributes: function(providedUser) {
const user = providedUser ? providedUser : this.getUser();
if (!user || !user.attribute) {
// not user defined or the user doesn't have any attributes
return [];
}
let attributes = user.attribute;
// if the user has only one attribute we need to put it in an array
return Array.isArray(attributes) ? attributes : [attributes];
},
/**
* Search in the user attributes an attribute that matches the provided
* attribute name. The search will not be case sensitive. Undefined is
* returned if the attribute could not be found.
*/
findUserAttribute: function(attributeName) {
// getting the user attributes
let userAttributes = this.getUserAttributes();
if (!userAttributes || !attributeName ) {
// the user as no attributes or the provided attribute name is undefined
return undefined;
}
return head(userAttributes.filter(attribute => attribute.name
&& attribute.name.toLowerCase() === attributeName.toLowerCase()));
},
/**
* Search in the user attributes an attribute that matches the provided
* attribute name. The search will not be case sensitive. Undefined is
* returned if the attribute could not be found otherwise the attribute
* value is returned.
*/
findUserAttributeValue: function(attributeName) {
let userAttribute = this.findUserAttribute(attributeName);
return userAttribute && userAttribute.value;
},
/**
* Returns an array with the configured authentication rules. If no rules
* were configured an empty array is returned.
*/
getAuthenticationRules: function() {
return ConfigUtils.getConfigProp('authenticationRules') || [];
},
/**
* Checks if authentication is activated or not.
*/
isAuthenticationActivated: function() {
return ConfigUtils.getConfigProp('useAuthenticationRules') || false;
},
/**
* Returns the authentication method that should be used for the provided URL.
* We go through the authentication rules and find the first one that matches
* the provided URL, if no rule matches the provided URL undefined is returned.
*/
getAuthenticationMethod: function(url) {
const foundRule = head(this.getAuthenticationRules().filter(
rule => rule && rule.urlPattern && url.match(new RegExp(rule.urlPattern, "i"))));
return foundRule ? foundRule.method : undefined;
},
/**
* Returns the authentication rule that should be used for the provided URL.
* We go through the authentication rules and find the first one that matches
* the provided URL, if no rule matches the provided URL undefined is returned.
*/
getAuthenticationRule: function(url) {
return head(this.getAuthenticationRules().filter(
rule => rule && rule.urlPattern && url.match(new RegExp(rule.urlPattern, "i"))));
},
/**
* This method will add query parameter based authentications to an url.
*/
addAuthenticationToUrl: function(url) {
if (!url || !this.isAuthenticationActivated()) {
return url;
}
const parsedUrl = URL.parse(url, true);
parsedUrl.query = this.addAuthenticationParameter(url, parsedUrl.query);
// we need to remove this to force the use of query
delete parsedUrl.search;
return URL.format(parsedUrl);
},
/**
* This method will add query parameter based authentications to an object
* containing query parameters.
*/
addAuthenticationParameter: function(url, parameters, securityToken) {
if (!url || !this.isAuthenticationActivated()) {
return parameters;
}
switch (this.getAuthenticationMethod(url)) {
case 'authkey': {
const token = !isNil(securityToken) ? securityToken : this.getToken();
if (!token) {
return parameters;
}
const authParam = this.getAuthKeyParameter(url);
return assign(parameters || {}, {[authParam]: token});
}
case 'test': {
const rule = this.getAuthenticationRule(url);
const token = rule ? rule.token : "";
const authParam = this.getAuthKeyParameter(url);
return assign(parameters || {}, { [authParam]: token });
}
default:
// we cannot handle the required authentication method
return parameters;
}
},
addAuthenticationToSLD: function(layerOptions, options) {
if (layerOptions.SLD) {
const parsed = URL.parse(layerOptions.SLD, true);
const params = SecurityUtils.addAuthenticationParameter(layerOptions.SLD, parsed.query, options.securityToken);
return assign({}, layerOptions, {
SLD: URL.format(assign({}, parsed, {
query: params,
search: undefined
}))
});
}
return layerOptions;
},
getAuthKeyParameter: function(url) {
const foundRule = this.getAuthenticationRule(url);
return foundRule && foundRule.authkeyParamName ? foundRule.authkeyParamName : 'authkey';
},
cleanAuthParamsFromURL: (url) => ConfigUtils.filterUrlParams(url, [SecurityUtils.getAuthKeyParameter(url)].filter(p => p))
};
module.exports = SecurityUtils;