-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageEmailNonse.java
380 lines (303 loc) · 15.9 KB
/
PageEmailNonse.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (c) 2010-2019 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.web.page.login;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.authentication.MidpointAuthentication;
import com.evolveum.midpoint.model.api.authentication.ModuleAuthentication;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.query.ObjectQuery;
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.task.api.Task;
import com.evolveum.midpoint.util.Producer;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.application.Url;
import com.evolveum.midpoint.web.component.AjaxSubmitButton;
import com.evolveum.midpoint.web.component.form.Form;
import com.evolveum.midpoint.web.component.prism.DynamicFormPanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.error.PageError;
import com.evolveum.midpoint.web.page.forgetpassword.PageForgotPassword;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.web.security.module.authentication.MailNonceModuleAuthentication;
import com.evolveum.midpoint.web.security.util.SecurityUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.request.cycle.RequestCycle;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.WebAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* @author lskublik
*/
@PageDescriptor(urls = {
@Url(mountUrl = "/emailNonce", matchUrlForSecurity = "/emailNonce")
}, permitAll = true, loginPage = true)
public class PageEmailNonse extends PageAuthenticationBase {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(PageEmailNonse.class);
private static final String DOT_CLASS = com.evolveum.midpoint.web.page.forgetpassword.PageSecurityQuestions.class.getName() + ".";
private static final String OPERATION_LOAD_USER = DOT_CLASS + "loaduser";
private static final String OPERATION_LOAD_QUESTION_POLICY = DOT_CLASS + "LOAD Question Policy";
private static final String ID_STATIC_LAYOUT = "staticLayout";
private static final String ID_EMAIL = "email";
private static final String ID_MAIN_FORM = "mainForm";
private static final String ID_BACK_BUTTON = "back";
private static final String ID_SUBMIT = "submit";
private static final String ID_PASSWORD_RESET_SUBMITED = "resetPasswordInfo";
private boolean submited;
public PageEmailNonse() {
}
protected void initCustomLayer() {
Form form = new Form(ID_MAIN_FORM);
form.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return !submited;
}
});
add(form);
initStaticLayout(form);
initDynamicLayout(form, PageEmailNonse.this);
initButtons(form);
MultiLineLabel label = new MultiLineLabel(ID_PASSWORD_RESET_SUBMITED,
createStringResource("PageForgotPassword.form.submited.message"));
add(label);
label.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return submited;
}
@Override
public boolean isEnabled() {
return submited;
}
});
}
private void initButtons(Form form) {
AjaxSubmitButton submit = new AjaxSubmitButton(ID_SUBMIT, createStringResource("PageForgetPassword.resetPassword")) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target) {
processResetPassword(target);
}
@Override
protected void onError(AjaxRequestTarget target) {
target.add(getFeedbackPanel());
}
};
form.add(submit);
form.add(createBackButton(ID_BACK_BUTTON));
}
private void processResetPassword(AjaxRequestTarget target) {
UserType user = searchUser();
if (user == null) {
getSession().error(getString("pageForgetPassword.message.user.not.found"));
throw new RestartResponseException(PageEmailNonse.class);
}
LOGGER.trace("Reset Password user: {}", user);
if (getResetPasswordPolicy() == null) {
LOGGER.debug("No policies for reset password defined");
getSession().error(getString("pageForgetPassword.message.policy.not.found"));
throw new RestartResponseException(PageEmailNonse.class);
}
OperationResult result = saveUserNonce(user, getMailNoncePolicy(user.asPrismObject()));
if (result.getStatus() == OperationResultStatus.SUCCESS) {
submited = true;
target.add(PageEmailNonse.this);
} else {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("Failed to send nonce to user: {} ", result.getMessage());
throw new RestartResponseException(PageEmailNonse.this);
}
}
private NonceCredentialsPolicyType getMailNoncePolicy(PrismObject<UserType> user) {
SecurityPolicyType securityPolicy = resolveSecurityPolicy(user);
LOGGER.trace("Found security policy: {}", securityPolicy);
if (securityPolicy == null) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("No security policy, cannot process nonce credential");
// Just log the error, but do not display it. We are still in unprivileged part of the web
// we do not want to provide any information to the attacker.
throw new RestartResponseException(PageEmailNonse.class);
}
if (securityPolicy.getCredentials() == null) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("No credential for security policy, cannot process nonce credential");
// Just log the error, but do not display it. We are still in unprivileged part of the web
// we do not want to provide any information to the attacker.
throw new RestartResponseException(PageEmailNonse.class);
}
if (securityPolicy.getCredentials().getNonce() == null) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("No nonce credential for security policy, cannot process nonce credential");
// Just log the error, but do not display it. We are still in unprivileged part of the web
// we do not want to provide any information to the attacker.
throw new RestartResponseException(PageEmailNonse.class);
}
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof MidpointAuthentication)) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("Bad type of authentication, support only MidpointAuthentication, but is "
+ authentication != null ? authentication.getClass().getName() : null);
throw new RestartResponseException(PageEmailNonse.class);
}
ModuleAuthentication moduleAuthentication = ((MidpointAuthentication) authentication).getProcessingModuleAuthentication();
if (!(moduleAuthentication instanceof MailNonceModuleAuthentication)) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("Bad type of module authentication, support only EmailNonceModuleAuthentication, but is "
+ moduleAuthentication != null ? moduleAuthentication.getClass().getName() : null);
throw new RestartResponseException(PageEmailNonse.class);
}
MailNonceModuleAuthentication nonseAutht = (MailNonceModuleAuthentication) moduleAuthentication;
String credentialName = nonseAutht.getCredentialName();
if (credentialName == null) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("EmailNonceModuleAuthentication " + nonseAutht.getNameOfModule() + " haven't define name of credential");
throw new RestartResponseException(PageEmailNonse.class);
}
NonceCredentialsPolicyType credentialByName = null;
for (NonceCredentialsPolicyType credential : securityPolicy.getCredentials().getNonce()) {
if (credentialName != null && credentialName.equals(credential.getName())) {
credentialByName = credential;
}
}
if (credentialByName == null) {
getSession().error(getString("PageForgotPassword.send.nonce.failed"));
LOGGER.error("Couldn't find nonce credentials by name " + credentialName);
throw new RestartResponseException(PageEmailNonse.class);
}
return credentialByName;
}
private void initStaticLayout(Form form) {
WebMarkupContainer staticLayout = new WebMarkupContainer(ID_STATIC_LAYOUT);
staticLayout.setOutputMarkupId(true);
staticLayout.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return !isDynamicForm();
}
});
form.add(staticLayout);
RequiredTextField<String> visibleUsername = new RequiredTextField<>(ID_EMAIL, new Model<>());
visibleUsername.setOutputMarkupId(true);
staticLayout.add(visibleUsername);
}
public PageBase getPageBase() {
return (PageBase) getPage();
}
protected ObjectQuery createStaticFormQuery() {
RequiredTextField<String> emailTextFiled = getEmail();
String email = emailTextFiled != null ? emailTextFiled.getModelObject() : null;
LOGGER.debug("Reset Password user info form submitted. email={}", email);
return getPrismContext().queryFor(UserType.class).item(UserType.F_EMAIL_ADDRESS)
.eq(email).matchingCaseIgnore().build();
}
private Form getMainForm() {
return (Form) get(ID_MAIN_FORM);
}
protected DynamicFormPanel getDynamicForm(){
return (DynamicFormPanel) getMainForm().get(createComponentPath(ID_DYNAMIC_LAYOUT, ID_DYNAMIC_FORM));
}
private RequiredTextField getEmail(){
return (RequiredTextField) getMainForm().get(createComponentPath(ID_STATIC_LAYOUT, ID_EMAIL));
}
@Override
protected void onConfigure() {
super.onConfigure();
ServletWebRequest req = (ServletWebRequest) RequestCycle.get().getRequest();
HttpServletRequest httpReq = req.getContainerRequest();
HttpSession httpSession = httpReq.getSession();
Exception ex = (Exception) httpSession.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
if (ex == null) {
return;
}
String msg = ex.getMessage();
if (StringUtils.isEmpty(msg)) {
msg = "web.security.provider.unavailable";
}
String[] msgs = msg.split(";");
for (String message : msgs) {
message = getLocalizationService().translate(message, null, getLocale(), message);
error(message);
}
httpSession.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
clearBreadcrumbs();
}
@Override
protected void createBreadcrumb() {
//don't create breadcrumb for login page
}
@Override
protected void onBeforeRender() {
super.onBeforeRender();
if (SecurityUtils.getPrincipalUser() != null) {
MidPointApplication app = getMidpointApplication();
throw new RestartResponseException(app.getHomePage());
}
}
private OperationResult saveUserNonce(final UserType user, final NonceCredentialsPolicyType noncePolicy) {
return runPrivileged(new Producer<OperationResult>() {
private static final long serialVersionUID = 1L;
@Override
public OperationResult run() {
Task task = createAnonymousTask("generateUserNonce");
task.setChannel(SchemaConstants.CHANNEL_GUI_RESET_PASSWORD_URI);
task.setOwner(user.asPrismObject());
OperationResult result = new OperationResult("generateUserNonce");
ProtectedStringType nonceCredentials = new ProtectedStringType();
try {
nonceCredentials
.setClearValue(generateNonce(noncePolicy, task, user.asPrismObject(), result));
// NonceType nonceType = new NonceType();
// nonceType.setValue(nonceCredentials);
ObjectDelta<UserType> nonceDelta = getPrismContext().deltaFactory().object()
.createModificationReplaceProperty(UserType.class, user.getOid(),
SchemaConstants.PATH_NONCE_VALUE, nonceCredentials);
WebModelServiceUtils.save(nonceDelta, result, task, PageEmailNonse.this);
} catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
result.recordFatalError(getString("PageForgotPassword.message.saveUserNonce.fatalError"));
LoggingUtils.logException(LOGGER, "Failed to generate nonce for user: " + e.getMessage(),
e);
}
result.computeStatusIfUnknown();
return result;
}
});
}
private <O extends ObjectType> String generateNonce(NonceCredentialsPolicyType noncePolicy, Task task,
PrismObject<O> user, OperationResult result)
throws ExpressionEvaluationException, SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
ValuePolicyType policy = null;
if (noncePolicy != null && noncePolicy.getValuePolicyRef() != null) {
PrismObject<ValuePolicyType> valuePolicy = WebModelServiceUtils.loadObject(ValuePolicyType.class,
noncePolicy.getValuePolicyRef().getOid(), PageEmailNonse.this, task, result);
policy = valuePolicy.asObjectable();
}
return getModelInteractionService().generateValue(policy, 24, false, user, "nonce generation", task, result);
}
}