/
PageLogin.java
179 lines (147 loc) · 7.25 KB
/
PageLogin.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
/*
* Copyright (c) 2010-2017 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.
*/
package com.evolveum.midpoint.web.page.login;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
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.component.form.Form;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.forgetpassword.PageForgotPassword;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.web.security.SecurityUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CredentialsPolicyType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RegistrationsPolicyType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityPolicyType;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.request.cycle.RequestCycle;
import org.springframework.security.web.WebAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* @author mserbak
*/
@PageDescriptor(url = "/login", permitAll = true)
public class PageLogin extends PageBase {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(PageLogin.class);
private static final String ID_FORGET_PASSWORD = "forgetpassword";
private static final String ID_SELF_REGISTRATION = "selfRegistration";
private static final String ID_CSRF_FIELD = "csrfField";
private static final String DOT_CLASS = PageLogin.class.getName() + ".";
protected static final String OPERATION_LOAD_RESET_PASSWORD_POLICY = DOT_CLASS + "loadPasswordResetPolicy";
private static final String OPERATION_LOAD_REGISTRATION_POLICY = DOT_CLASS + "loadRegistrationPolicy";
public PageLogin() {
BookmarkablePageLink<String> link = new BookmarkablePageLink<>(ID_FORGET_PASSWORD, PageForgotPassword.class);
link.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
OperationResult parentResult = new OperationResult(OPERATION_LOAD_RESET_PASSWORD_POLICY);
SecurityPolicyType securityPolicy = null;
try {
securityPolicy = getModelInteractionService().getSecurityPolicy(null, null, parentResult);
} catch (ObjectNotFoundException | SchemaException e) {
LOGGER.warn("Cannot read credentials policy: " + e.getMessage(), e);
}
if (securityPolicy == null) {
return false;
}
CredentialsPolicyType creds = securityPolicy.getCredentials();
// TODO: Not entirely correct. This means we have reset somehow configured, but not necessarily enabled.
if (creds != null
&& ((creds.getSecurityQuestions() != null
&& creds.getSecurityQuestions().getQuestionNumber() != null) || (securityPolicy.getCredentialsReset() != null))) {
return true;
}
return false;
}
});
add(link);
AjaxLink<String> registration = new AjaxLink<String>(ID_SELF_REGISTRATION) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
setResponsePage(PageSelfRegistration.class);
}
};
registration.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
OperationResult parentResult = new OperationResult(OPERATION_LOAD_REGISTRATION_POLICY);
RegistrationsPolicyType registrationPolicies = null;
try {
Task task = createAnonymousTask(OPERATION_LOAD_REGISTRATION_POLICY);
registrationPolicies = getModelInteractionService().getRegistrationPolicy(null, task, parentResult);
} catch (ObjectNotFoundException | SchemaException e) {
LOGGER.warn("Cannot read credentials policy: " + e.getMessage(), e);
}
boolean linkIsVisible = false;
if (registrationPolicies != null
&& registrationPolicies.getSelfRegistration() != null) {
linkIsVisible = true;
}
return linkIsVisible;
}
});
add(registration);
WebMarkupContainer csrfField = SecurityUtils.createHiddenInputForCsrf(ID_CSRF_FIELD);
add(csrfField);
}
@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 = getString("web.security.provider.unavailable");
}
error(msg);
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());
}
}
}