-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageRegistrationConfirmation.java
260 lines (207 loc) · 9.68 KB
/
PageRegistrationConfirmation.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
package com.evolveum.midpoint.web.page.login;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.prism.delta.ContainerDelta;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.security.api.ConnectionEnvironment;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.Producer;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.util.MidPointPageParametersEncoder;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CredentialsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NonceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
//CONFIRMATION_LINK = "http://localhost:8080/midpoint/confirm/registration/";
@PageDescriptor(url = "/confirm", encoder = MidPointPageParametersEncoder.class)
public class PageRegistrationConfirmation extends PageRegistrationBase {
private static final String DOT_CLASS = PageRegistrationConfirmation.class.getName() + ".";
private static final String ID_LABEL_SUCCESS = "successLabel";
private static final String ID_LABEL_ERROR = "errorLabel";
private static final String ID_LINK_LOGIN = "linkToLogin";
private static final String ID_SUCCESS_PANEL = "successPanel";
private static final String ID_ERROR_PANEL = "errorPanel";
private static final String OPERATION_ASSIGN_DEFAULT_ROLES = DOT_CLASS + ".assignDefaultRoles";
private static final String OPERATION_FINISH_REGISTRATION = DOT_CLASS + "finishRegistration";
private static final long serialVersionUID = 1L;
public PageRegistrationConfirmation() {
super();
init(null);
}
public PageRegistrationConfirmation(PageParameters params) {
super();
init(params);
}
private void init(final PageParameters pageParameters) {
PageParameters params = pageParameters;
if (params == null) {
params = getPageParameters();
}
StringValue userNameValue = params.get(SchemaConstants.REGISTRATION_ID);
Validate.notEmpty(userNameValue.toString());
StringValue tokenValue = params.get(SchemaConstants.REGISTRATION_TOKEN);
Validate.notEmpty(tokenValue.toString());
OperationResult result = new OperationResult(OPERATION_FINISH_REGISTRATION);
UsernamePasswordAuthenticationToken token = authenticateUser(userNameValue.toString(), tokenValue.toString(), result);
if (token == null) {
initLayout(result);
return;
}
final MidPointPrincipal principal = (MidPointPrincipal) token.getPrincipal();
result = assignDefaultRoles(principal.getOid());
if (result.getStatus() == OperationResultStatus.FATAL_ERROR) {
initLayout(result);
return;
}
final NonceType nonceClone = principal.getUser().getCredentials().getNonce().clone();
result = removeNonce(principal.getOid(), nonceClone);
assignAdditionalRoleIfPresent(principal.getOid(), token, nonceClone, result);
initLayout(result);
}
private UsernamePasswordAuthenticationToken authenticateUser(String username, String nonce, OperationResult result){
ConnectionEnvironment connEnv = new ConnectionEnvironment();
connEnv.setChannel(SchemaConstants.CHANNEL_GUI_SELF_REGISTRATION_URI);
try {
return getAuthenticationEvaluator().authenticateUserNonce(connEnv, username,
nonce, getSelfRegistrationConfiguration().getNoncePolicy());
} catch (AuthenticationException ex) {
getSession()
.error(createStringResource("PageRegistrationConfirmation.bad.credentials").getString());
result.recordFatalError("Failed to validate user");
return null;
}
}
private OperationResult assignDefaultRoles(final String userOid){
List<ContainerDelta<AssignmentType>> assignments = new ArrayList<>();
for (ObjectReferenceType defaultRole : getSelfRegistrationConfiguration().getDefaultRoles()) {
AssignmentType assignment = new AssignmentType();
assignment.setTargetRef(defaultRole);
try {
getPrismContext().adopt(assignment);
assignments.add(ContainerDelta.createModificationAdd(UserType.F_ASSIGNMENT, UserType.class, getPrismContext(), assignment));
} catch (SchemaException e) {
//nothing to do
}
}
final ObjectDelta<UserType> delta = ObjectDelta.createModifyDelta(userOid, assignments, UserType.class, getPrismContext());
return runPrivileged(new Producer<OperationResult>() {
@Override
public OperationResult run() {
OperationResult result = new OperationResult(OPERATION_ASSIGN_DEFAULT_ROLES);
Task task = createAnonymousTask(OPERATION_ASSIGN_DEFAULT_ROLES);
WebModelServiceUtils.save(delta, result, task, PageRegistrationConfirmation.this);
result.computeStatusIfUnknown();
return result;
}
});
}
private OperationResult removeNonce(final String userOid, final NonceType nonce){
return runPrivileged(new Producer<OperationResult>() {
@Override
public OperationResult run() {
OperationResult result = new OperationResult("assignDefaultRoles");
Task task = createAnonymousTask("assignDefaultRoles");
ObjectDelta<UserType> userAssignmentsDelta;
try {
userAssignmentsDelta = ObjectDelta.createModificationDeleteContainer(UserType.class, userOid, new ItemPath(UserType.F_CREDENTIALS, CredentialsType.F_NONCE), getPrismContext(), nonce);
userAssignmentsDelta.addModificationReplaceProperty(UserType.F_LIFECYCLE_STATE, SchemaConstants.LIFECYCLE_ACTIVE);
} catch (SchemaException e) {
result.recordFatalError("Could not create delta");
return result;
}
WebModelServiceUtils.save(userAssignmentsDelta, result, task, PageRegistrationConfirmation.this);
result.computeStatusIfUnknown();
return result;
}
});
}
private void assignAdditionalRoleIfPresent(String userOid, UsernamePasswordAuthenticationToken token, NonceType nonceType, OperationResult result){
SecurityContextHolder.getContext().setAuthentication(token);
List<ItemDelta> userDeltas = new ArrayList<>();
if (nonceType.getName() != null) {
Task task = createSimpleTask(OPERATION_FINISH_REGISTRATION);
ObjectDelta<UserType> assignRoleDelta = null;
try {
AssignmentType assignment = new AssignmentType();
assignment.setTargetRef(
ObjectTypeUtil.createObjectRef(nonceType.getName(), ObjectTypes.ABSTRACT_ROLE));
getPrismContext().adopt(assignment);
userDeltas.add((ItemDelta) ContainerDelta.createModificationAdd(UserType.F_ASSIGNMENT,
UserType.class, getPrismContext(), assignment));
assignRoleDelta = ObjectDelta.createModifyDelta(userOid, userDeltas,
UserType.class, getPrismContext());
assignRoleDelta.setPrismContext(getPrismContext());
} catch (SchemaException e) {
result.recordFatalError("Could not create delta");
return;
}
WebModelServiceUtils.save(assignRoleDelta, result, task, PageRegistrationConfirmation.this);
result.computeStatusIfUnknown();
}
SecurityContextHolder.getContext().setAuthentication(null);
}
private void initLayout(final OperationResult result) {
WebMarkupContainer successPanel = new WebMarkupContainer(ID_SUCCESS_PANEL);
add(successPanel);
successPanel.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return result.getStatus() != OperationResultStatus.FATAL_ERROR;
}
@Override
public boolean isEnabled() {
return result.getStatus() != OperationResultStatus.FATAL_ERROR;
}
});
Label successMessage = new Label(ID_LABEL_SUCCESS,
createStringResource("PageRegistrationConfirmation.confirmation.successful"));
successPanel.add(successMessage);
AjaxLink<String> continueToLogin = new AjaxLink<String>(ID_LINK_LOGIN) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
setResponsePage(PageLogin.class);
}
};
successPanel.add(continueToLogin);
WebMarkupContainer errorPanel = new WebMarkupContainer(ID_ERROR_PANEL);
add(errorPanel);
errorPanel.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isEnabled() {
return result.getStatus() == OperationResultStatus.FATAL_ERROR;
}
@Override
public boolean isVisible() {
return result.getStatus() == OperationResultStatus.FATAL_ERROR;
}
});
Label errorMessage = new Label(ID_LABEL_ERROR,
createStringResource("PageRegistrationConfirmation.confirmation.error"));
errorPanel.add(errorMessage);
}
}