/
MidpointAuthentication.java
310 lines (267 loc) · 10.8 KB
/
MidpointAuthentication.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
310
/*
* Copyright (C) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.model.api.authentication;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.Validate;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import com.evolveum.midpoint.schema.util.SecurityPolicyUtil;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceModuleNecessityType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceModuleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceType;
/**
* @author skublik
*/
public class MidpointAuthentication extends AbstractAuthenticationToken {
private final List<AuthenticationSequenceModuleType> modules;
private final AuthenticationSequenceType sequence;
private final List<ModuleAuthentication> authentications = new ArrayList<>();
private AuthenticationChannel authenticationChannel;
private List<AuthModule> authModules;
private Object principal;
private Object credential;
private String sessionId;
private Collection<? extends GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;
public MidpointAuthentication(AuthenticationSequenceType sequence) {
super(null);
this.modules = SecurityPolicyUtil.getSortedModules(sequence);
this.sequence = sequence;
}
public List<AuthModule> getAuthModules() {
return authModules;
}
public void setAuthModules(List<AuthModule> authModules) {
this.authModules = authModules;
}
public AuthenticationSequenceType getSequence() {
return sequence;
}
public AuthenticationChannel getAuthenticationChannel() {
return authenticationChannel;
}
public void setAuthenticationChannel(AuthenticationChannel authenticationChannel) {
this.authenticationChannel = authenticationChannel;
}
public List<ModuleAuthentication> getAuthentications() {
return authentications;
}
public void addAuthentications(ModuleAuthentication authentication) {
getAuthentications().add(authentication);
}
@Override
public Collection<GrantedAuthority> getAuthorities() {
//noinspection unchecked
return (Collection<GrantedAuthority>) authorities;
}
public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
this.authorities = authorities;
}
public List<AuthenticationSequenceModuleType> getModules() {
return Collections.unmodifiableList(modules);
}
@Override
public Object getCredentials() {
return credential;
}
public void setCredential(Object credential) {
this.credential = credential;
}
@Override
public Object getPrincipal() {
return principal;
}
public void setPrincipal(Object principal) {
this.principal = principal;
}
@Override
public void setAuthenticated(boolean authenticated) {
throw new IllegalArgumentException("This method is not supported");
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getSessionId() {
return sessionId;
}
@Override
public boolean isAuthenticated() {
List<AuthenticationSequenceModuleType> modules = getModules();
if (modules.isEmpty()) {
return false;
}
for (AuthenticationSequenceModuleType module : modules) {
ModuleAuthentication authentication = getAuthenticationByName(module.getName());
if (authentication == null) {
continue;
}
//TODO we will complete after supporting of full "necessity"
if (AuthenticationSequenceModuleNecessityType.SUFFICIENT.equals(module.getNecessity())) {
if (StateOfModule.SUCCESSFULLY.equals(authentication.getState())) {
return true;
}
}
}
return false;
}
public ModuleAuthentication getAuthenticationByName(String moduleName) {
for (ModuleAuthentication authentication : getAuthentications()) {
if (authentication.getNameOfModule().equals(moduleName)) {
return authentication;
}
}
return null;
}
public boolean isProcessing() {
if (!getAuthentications().isEmpty()) {
for (ModuleAuthentication authModule : getAuthentications()) {
if (StateOfModule.LOGIN_PROCESSING.equals(authModule.getState())) {
return true;
}
}
}
return false;
}
public int getIndexOfProcessingModule(boolean createEmptyAuthenticationIfNeeded) {
if (getAuthentications().isEmpty()) {
if (createEmptyAuthenticationIfNeeded) {
addAuthentications(getAuthModules().get(0).getBaseModuleAuthentication());
}
return 0;
}
ModuleAuthentication authentication = getProcessingModuleAuthentication();
if (authentication != null) {
return getIndexOfModule(authentication);
}
if (isAuthenticated()) {
return -2;
}
int actualSize = getAuthentications().size();
int endSize = getAuthModules().size();
if (actualSize < endSize) {
if (createEmptyAuthenticationIfNeeded) {
addAuthentications(getAuthModules().get(actualSize).getBaseModuleAuthentication());
}
return actualSize;
}
return -1;
}
public int getIndexOfModule(ModuleAuthentication authentication) {
Validate.notNull(authentication);
for (int i = 0; i < getModules().size(); i++) {
if (getModules().get(i).getName().equals(authentication.getNameOfModule())) {
return i;
}
}
return -1;
}
public ModuleAuthentication getProcessingModuleAuthentication() {
for (ModuleAuthentication authentication : getAuthentications()) {
if (authentication.getState().equals(StateOfModule.LOGIN_PROCESSING)
|| authentication.getState().equals(StateOfModule.LOGOUT_PROCESSING)) {
return authentication;
}
}
return null;
}
public boolean isAuthenticationFailed() {
return !isAuthenticated()
&& getProcessingModuleAuthentication() == null
&& getAuthentications().size() == getAuthModules().size();
}
@Override
public String getName() {
if (getPrincipal() instanceof MidPointPrincipal) {
return ((MidPointPrincipal) getPrincipal()).getUsername();
}
return "";
}
public List<ModuleAuthentication> getParallelProcessingModules() {
int indexOfProcessingModule = getIndexOfProcessingModule(false);
if (indexOfProcessingModule == -2) {
return new ArrayList<>();
}
return getParallelProcessingModules(indexOfProcessingModule);
}
private List<ModuleAuthentication> getParallelProcessingModules(int actualIndex) {
List<ModuleAuthentication> parallelProcessingModules = new ArrayList<>();
ModuleAuthentication processingModule = getAuthentications().get(actualIndex);
AuthenticationSequenceModuleType processingModuleType = getModules().get(actualIndex);
if (processingModule == null) {
return parallelProcessingModules;
}
if (actualIndex > 0) {
for (int i = actualIndex - 1; i >= 0; i--) {
if (getModules().get(i) != null
&& processingModuleType.getOrder().equals(getModules().get(i).getOrder())) {
parallelProcessingModules.add(getAuthModules().get(i).getBaseModuleAuthentication());
} else {
break;
}
}
}
parallelProcessingModules.add(processingModule);
for (int i = actualIndex + 1; i < getModules().size(); i++) {
if (getModules().get(i) != null
&& processingModuleType.getOrder().equals(getModules().get(i).getOrder())) {
parallelProcessingModules.add(getAuthModules().get(i).getBaseModuleAuthentication());
}
}
return parallelProcessingModules;
}
public int resolveParallelModules(HttpServletRequest request, int actualIndex) {
String header = request.getHeader("Authorization");
if (header == null) {
return actualIndex;
}
String type = header.split(" ")[0];
List<ModuleAuthentication> parallelProcessingModules = getParallelProcessingModules(actualIndex);
int resolvedIndex = -1;
for (ModuleAuthentication parallelProcessingModule : parallelProcessingModules) {
int usedIndex = getAuthentications().indexOf(parallelProcessingModule);
if (parallelProcessingModule.getNameOfModuleType().toLowerCase().equals(type.toLowerCase())
&& resolvedIndex == -1) {
parallelProcessingModule.setState(StateOfModule.LOGIN_PROCESSING);
if (usedIndex != -1) {
resolvedIndex = usedIndex;
} else {
resolvedIndex = getAuthentications().size();
}
} else {
parallelProcessingModule.setState(StateOfModule.FAILURE);
}
if (usedIndex == -1) {
getAuthentications().add(parallelProcessingModule);
} else {
getAuthentications().set(usedIndex, parallelProcessingModule);
}
}
if (resolvedIndex == -1) {
throw new IllegalArgumentException("Couldn't find module with type '" + type + "' in sequence '"
+ getSequence().getName() + "'");
}
return resolvedIndex;
}
public boolean isLast(ModuleAuthentication moduleAuthentication) {
if (getAuthentications().isEmpty()) {
return false;
}
int index = getIndexOfModule(moduleAuthentication);
if (index == -1) {
return false;
}
if (index == getModules().size() - 1) {
return true;
}
return getModules().get(index).getOrder().equals(getModules().get(getModules().size() - 1).getOrder());
}
}