-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
DefaultRegisteredServiceAccessStrategy.java
309 lines (279 loc) · 11.5 KB
/
DefaultRegisteredServiceAccessStrategy.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package org.apereo.cas.services;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apereo.cas.util.CollectionUtils;
import org.apereo.cas.util.RegexUtils;
import javax.persistence.PostLoad;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* This is {@link DefaultRegisteredServiceAccessStrategy}
* that allows the following rules:
* <ul>
* <li>A service may be disallowed to use CAS for authentication</li>
* <li>A service may be disallowed to take part in CAS single sign-on such that
* presentation of credentials would always be required.</li>
* <li>A service may be prohibited from receiving a service ticket
* if the existing principal attributes don't contain the required attributes
* that otherwise grant access to the service.</li>
* </ul>
*
* @author Misagh Moayyed
* @since 4.1
*/
@Slf4j
@ToString
@Getter
@EqualsAndHashCode
@Setter
public class DefaultRegisteredServiceAccessStrategy implements RegisteredServiceAccessStrategy {
private static final long serialVersionUID = 1245279151345635245L;
/**
* The sorting/execution order of this strategy.
*/
protected int order;
/**
* Is the service allowed at all?
*/
protected boolean enabled = true;
/**
* Is the service allowed to use SSO?
*/
protected boolean ssoEnabled = true;
/**
* The Unauthorized redirect url.
*/
protected URI unauthorizedRedirectUrl;
/**
* The delegated authn policy.
*/
protected RegisteredServiceDelegatedAuthenticationPolicy delegatedAuthenticationPolicy =
new DefaultRegisteredServiceDelegatedAuthenticationPolicy();
/**
* Defines the attribute aggregation behavior when checking for required attributes.
* Default requires that all attributes be present and match the principal's.
*/
protected boolean requireAllAttributes = true;
/**
* Collection of required attributes
* for this service to proceed.
*/
protected Map<String, Set<String>> requiredAttributes = new HashMap<>();
/**
* Collection of attributes
* that will be rejected which will cause this
* policy to refuse access.
*/
protected Map<String, Set<String>> rejectedAttributes = new HashMap<>();
/**
* Indicates whether matching on required attribute values
* should be done in a case-insensitive manner.
*/
protected boolean caseInsensitive;
/**
* Instantiates a new Default registered service authorization strategy.
* By default, rules indicate that services are both enabled
* and can participate in SSO.
*/
public DefaultRegisteredServiceAccessStrategy() {
this(true, true);
}
/**
* Instantiates a new Default registered service authorization strategy.
*
* @param enabled the enabled
* @param ssoEnabled the sso enabled
*/
public DefaultRegisteredServiceAccessStrategy(final boolean enabled, final boolean ssoEnabled) {
this.enabled = enabled;
this.ssoEnabled = ssoEnabled;
}
/**
* Instantiates a new Default registered service access strategy.
*
* @param requiredAttributes the required attributes
* @param rejectedAttributes the rejected attributes
*/
public DefaultRegisteredServiceAccessStrategy(final Map<String, Set<String>> requiredAttributes, final Map<String, Set<String>> rejectedAttributes) {
this();
this.requiredAttributes = requiredAttributes;
this.rejectedAttributes = rejectedAttributes;
}
/**
* Instantiates a new Default registered service access strategy.
*
* @param requiredAttributes the required attributes
*/
public DefaultRegisteredServiceAccessStrategy(final Map<String, Set<String>> requiredAttributes) {
this();
this.requiredAttributes = requiredAttributes;
}
/**
* Post load.
*/
@PostLoad
public void postLoad() {
this.delegatedAuthenticationPolicy = ObjectUtils.defaultIfNull(this.delegatedAuthenticationPolicy,
new DefaultRegisteredServiceDelegatedAuthenticationPolicy());
}
/**
* Expose underlying attributes for auditing purposes.
*
* @return required attributes
*/
@Override
public Map<String, Set<String>> getRequiredAttributes() {
return requiredAttributes;
}
@JsonIgnore
@Override
public boolean isServiceAccessAllowedForSso() {
if (!this.ssoEnabled) {
LOGGER.trace("Service is not authorized to participate in SSO.");
return false;
}
return true;
}
@JsonIgnore
@Override
public boolean isServiceAccessAllowed() {
if (!this.enabled) {
LOGGER.trace("Service is not enabled in service registry.");
return false;
}
return true;
}
@JsonIgnore
@Override
public void setServiceAccessAllowed(final boolean value) {
this.enabled = value;
}
@Override
public boolean doPrincipalAttributesAllowServiceAccess(final String principal, final Map<String, Object> principalAttributes) {
if (this.rejectedAttributes.isEmpty() && this.requiredAttributes.isEmpty()) {
LOGGER.debug("Skipping access strategy policy, since no attributes rules are defined");
return true;
}
if (!enoughAttributesAvailableToProcess(principal, principalAttributes)) {
LOGGER.debug("Access is denied. There are not enough attributes available to satisfy requirements");
return false;
}
if (doRejectedAttributesRefusePrincipalAccess(principalAttributes)) {
LOGGER.debug("Access is denied. The principal carries attributes that would reject service access");
return false;
}
if (!doRequiredAttributesAllowPrincipalAccess(principalAttributes, this.requiredAttributes)) {
LOGGER.debug("Access is denied. The principal does not have the required attributes [{}] specified by this strategy", this.requiredAttributes);
return false;
}
return true;
}
/**
* Do required attributes allow principal access boolean.
*
* @param principalAttributes the principal attributes
* @param requiredAttributes the required attributes
* @return the boolean
*/
protected boolean doRequiredAttributesAllowPrincipalAccess(final Map<String, Object> principalAttributes, final Map<String, Set<String>> requiredAttributes) {
LOGGER.debug("These required attributes [{}] are examined against [{}] before service can proceed.", requiredAttributes, principalAttributes);
if (requiredAttributes.isEmpty()) {
return true;
}
return requiredAttributesFoundInMap(principalAttributes, requiredAttributes);
}
/**
* Do rejected attributes refuse principal access boolean.
*
* @param principalAttributes the principal attributes
* @return the boolean
*/
protected boolean doRejectedAttributesRefusePrincipalAccess(final Map<String, Object> principalAttributes) {
LOGGER.debug("These rejected attributes [{}] are examined against [{}] before service can proceed.", rejectedAttributes, principalAttributes);
if (rejectedAttributes.isEmpty()) {
return false;
}
return requiredAttributesFoundInMap(principalAttributes, rejectedAttributes);
}
/**
* Enough attributes available to process? Check collection sizes and determine
* if we have enough data to move on.
*
* @param principal the principal
* @param principalAttributes the principal attributes
* @return true /false
*/
protected boolean enoughAttributesAvailableToProcess(final String principal, final Map<String, Object> principalAttributes) {
if (!enoughRequiredAttributesAvailableToProcess(principalAttributes, this.requiredAttributes)) {
return false;
}
if (principalAttributes.size() < this.rejectedAttributes.size()) {
LOGGER.debug("The size of the principal attributes that are [{}] does not match defined rejected attributes, "
+ "which means the principal is not carrying enough data to grant authorization", principalAttributes);
return false;
}
return true;
}
/**
* Enough required attributes available to process? Check collection sizes and determine
* if we have enough data to move on.
*
* @param principalAttributes the principal attributes
* @param requiredAttributes the required attributes
* @return true /false
*/
protected boolean enoughRequiredAttributesAvailableToProcess(final Map<String, Object> principalAttributes, final Map<String, Set<String>> requiredAttributes) {
if (principalAttributes.isEmpty() && !requiredAttributes.isEmpty()) {
LOGGER.debug("No principal attributes are found to satisfy defined attribute requirements");
return false;
}
if (principalAttributes.size() < requiredAttributes.size()) {
LOGGER.debug("The size of the principal attributes that are [{}] does not match defined required attributes, "
+ "which indicates the principal is not carrying enough data to grant authorization", principalAttributes);
return false;
}
return true;
}
/**
* Check whether required attributes are found in the given map.
*
* @param principalAttributes the principal attributes
* @param requiredAttributes the attributes
* @return the boolean
*/
protected boolean requiredAttributesFoundInMap(final Map<String, Object> principalAttributes, final Map<String, Set<String>> requiredAttributes) {
final Set<String> difference = requiredAttributes.keySet().stream().filter(a -> principalAttributes.keySet().contains(a)).collect(Collectors.toSet());
LOGGER.debug("Difference of checking required attributes: [{}]", difference);
if (this.requireAllAttributes && difference.size() < requiredAttributes.size()) {
return false;
}
if (this.requireAllAttributes) {
return difference.stream().allMatch(key -> {
return requiredAttributeFound(key, principalAttributes, requiredAttributes);
});
} else {
return difference.stream().anyMatch(key -> {
return requiredAttributeFound(key, principalAttributes, requiredAttributes);
});
}
}
private boolean requiredAttributeFound(final String attributeName, final Map<String, Object> principalAttributes, final Map<String, Set<String>> requiredAttributes) {
final Set<String> values = requiredAttributes.get(attributeName);
final Set<Object> availableValues = CollectionUtils.toCollection(principalAttributes.get(attributeName));
final Pattern pattern = RegexUtils.concatenate(values, this.caseInsensitive);
LOGGER.debug("Checking [{}] against [{}] with pattern [{}] for attribute [{}]", values, availableValues, pattern, attributeName);
if (pattern != RegexUtils.MATCH_NOTHING_PATTERN) {
return availableValues.stream().map(Object::toString).anyMatch(pattern.asPredicate());
}
return availableValues.stream().anyMatch(values::contains);
}
}