Skip to content

Commit

Permalink
adding mail nonce module for reset password and selfRegistration with…
Browse files Browse the repository at this point in the history
… schrodinger tests
  • Loading branch information
skublik committed Jan 20, 2020
1 parent 277c8a5 commit 974282a
Show file tree
Hide file tree
Showing 79 changed files with 3,487 additions and 498 deletions.
Expand Up @@ -87,7 +87,7 @@ protected void finishChangePassword(final OperationResult result, AjaxRequestTar
target.add(getFeedbackPanel());
// get(ID_MAIN_FORM).setVisible(false);


// success(getString("PageShowPassword.success")); //TODO uncomment when remove old mechanism
}

@Override
Expand Down
Expand Up @@ -162,6 +162,15 @@ private void doRegistration(AjaxRequestTarget target) {

private boolean validateCaptcha(AjaxRequestTarget target) {
CaptchaPanel captcha = getCaptcha();
String value = System.getProperty("midpoint.schrodinger");
if (value != null){
Boolean isSchrodingerTesting = Boolean.valueOf(value);
if (Boolean.TRUE.equals(isSchrodingerTesting)){
LOGGER.trace("Skipping CAPTCHA Validation, because system variable (midpoint.schrodinget) for schrodinger testing is TRUE");
return true;
}
}


if (captcha.getRandomText() == null) {
String message = createStringResource("PageSelfRegistration.captcha.validation.failed")
Expand Down
Expand Up @@ -10,21 +10,37 @@
import com.evolveum.midpoint.model.api.AuthenticationEvaluator;
import com.evolveum.midpoint.model.api.context.NonceAuthenticationContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.EqualFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.QueryFactory;
import com.evolveum.midpoint.schema.SearchResultList;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.Producer;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.SchemaException;
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.component.AjaxButton;
import com.evolveum.midpoint.web.component.prism.DynamicFormPanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.forgetpassword.PageForgotPassword;
import com.evolveum.midpoint.web.page.forgetpassword.ResetPolicyDto;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityPolicyType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class PageAuthenticationBase extends PageBase {
import java.util.ArrayList;
import java.util.List;

public abstract class PageAuthenticationBase extends PageBase {

private static final long serialVersionUID = 1L;
private static final String DOT_CLASS = PageAuthenticationBase.class.getName() + ".";
Expand All @@ -34,6 +50,9 @@ public class PageAuthenticationBase extends PageBase {

private static final Trace LOGGER = TraceManager.getTrace(PageAuthenticationBase.class);

protected static final String ID_DYNAMIC_LAYOUT = "dynamicLayout";
protected static final String ID_DYNAMIC_FORM = "dynamicForm";

@SpringBean(name = "nonceAuthenticationEvaluator")
private AuthenticationEvaluator<NonceAuthenticationContext> authenticationEvaluator;

Expand Down Expand Up @@ -134,7 +153,6 @@ public SecurityPolicyType run() {
});

return securityPolicy;

}

public SelfRegistrationDto getSelfRegistrationConfiguration() {
Expand Down Expand Up @@ -168,4 +186,136 @@ public AuthenticationEvaluator<NonceAuthenticationContext> getAuthenticationEval
return authenticationEvaluator;
}

protected void initDynamicLayout(final org.apache.wicket.markup.html.form.Form<?> mainForm, PageBase parentPage) {
WebMarkupContainer dynamicLayout = new WebMarkupContainer(ID_DYNAMIC_LAYOUT);
dynamicLayout.setOutputMarkupId(true);
mainForm.add(dynamicLayout);

dynamicLayout.add(new VisibleEnableBehaviour() {

private static final long serialVersionUID = 1L;

@Override
public boolean isVisible() {
return isDynamicForm();
}
});

DynamicFormPanel<UserType> searchAttributesForm = runPrivileged(
() -> {
ObjectReferenceType formRef = getResetPasswordPolicy().getFormRef();
if (formRef == null) {
return null;
}
Task task = createAnonymousTask(OPERATION_LOAD_DYNAMIC_FORM);
return new DynamicFormPanel<UserType>(ID_DYNAMIC_FORM, UserType.COMPLEX_TYPE,
formRef.getOid(), mainForm, task, parentPage, true);
});

if (searchAttributesForm != null) {
dynamicLayout.add(searchAttributesForm);
}
}

protected boolean isDynamicForm() {
return getResetPasswordPolicy().getFormRef() != null;
}

protected void cancelPerformed() {
setResponsePage(getMidpointApplication().getHomePage());
}

protected AjaxButton createBackButton(String id){
AjaxButton back = new AjaxButton(id) {

private static final long serialVersionUID = 1L;

@Override
public void onClick(AjaxRequestTarget target) {
cancelPerformed();
}
};
return back;
}

protected UserType searchUser() {
ObjectQuery query = null;

if (isDynamicForm()) {
query = createDynamicFormQuery();
} else {
query = createStaticFormQuery();
}

if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Searching for user with query:\n{}", query.debugDump(1));
}

return searchUserPrivileged(query);

}

protected abstract ObjectQuery createStaticFormQuery();

protected UserType searchUserPrivileged(ObjectQuery query) {
UserType userType = runPrivileged(new Producer<UserType>() {

@Override
public UserType run() {

Task task = createAnonymousTask("load user");
OperationResult result = new OperationResult("search user");

SearchResultList<PrismObject<UserType>> users;
try {
users = getModelService().searchObjects(UserType.class, query, null, task, result);
} catch (SchemaException | ObjectNotFoundException | SecurityViolationException
| CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
LoggingUtils.logException(LOGGER, "failed to search user", e);
return null;
}

if ((users == null) || (users.isEmpty())) {
LOGGER.trace("Empty user list in ForgetPassword");
return null;
}

if (users.size() > 1) {
LOGGER.trace("Problem while seeking for user");
return null;
}

UserType user = users.iterator().next().asObjectable();
LOGGER.trace("User found for ForgetPassword: {}", user);

return user;
}

});
return userType;
}

protected ObjectQuery createDynamicFormQuery() {
DynamicFormPanel<UserType> userDynamicPanel = getDynamicForm();
List<ItemPath> filledItems = userDynamicPanel.getChangedItems();
PrismObject<UserType> user;
try {
user = userDynamicPanel.getObject();
} catch (SchemaException e1) {
getSession().error(getString("pageForgetPassword.message.usernotfound"));
throw new RestartResponseException(PageForgotPassword.class);
}

List<EqualFilter> filters = new ArrayList<>();
QueryFactory queryFactory = getPrismContext().queryFactory();
for (ItemPath path : filledItems) {
PrismProperty property = user.findProperty(path);
EqualFilter filter = queryFactory.createEqual(path, property.getDefinition(), null);
filter.setValue(property.getAnyValue().clone());
filters.add(filter);
}
return queryFactory.createQuery(queryFactory.createAnd((List) filters));
}

protected abstract DynamicFormPanel<UserType> getDynamicForm();
}
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2013 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:wicket="http://wicket.apache.org">
<body>
<wicket:extend>

<div class="row">
<div class="col-md-offset-2 col-md-8 col-lg-offset-4 col-lg-4">
<div class="panel panel-default" style="margin-top: 120px;">
<div class="panel-body">

<form class="form-horizontal" action="ignore" wicket:id="mainForm">
<div class="form-group" wicket:id="staticLayout">
<label class="col-md-4 col-lg-4 control-label">
<wicket:message key="PageForgetPassword.email"/>
</label>

<div class="col-md-8 col-lg-8">
<input name="email" type="text" class="form-control input-sm focus-username"
wicket:message="placeholder:PageForgetPassword.email" wicket:id="email">
</div>
</div>

<div wicket:id="dynamicLayout">
<div wicket:id="dynamicForm"/>
</div>

<div class="pull-right">
<a class="btn btn-default" wicket:id="back"><wicket:message key="PageBase.button.back"/></a>
<a class="btn btn-primary" wicket:id="submit"/>
</div>
</form>
<div style="text-align: center">
<h2 wicket:id="resetPasswordInfo"></h2>
</div>
</div>
</div>
</div>
</div>


<script type="text/javascript">
$(".focus-username").focus();
</script>

<wicket:remove>This css is used to hide sidebar menu</wicket:remove>
<style type="text/css">
.content-wrapper, .right-side, .main-footer {
margin-left: 0px;

-moz-transition: none;
-webkit-transition: none;
-o-transition: color 0 ease-in;
transition: none;
}
</style>
</wicket:extend>

</body>
</html>

0 comments on commit 974282a

Please sign in to comment.