/
LoginEvents.java
358 lines (318 loc) · 17.8 KB
/
LoginEvents.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
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ofbiz.securityext.login;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.UtilHttp;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.string.FlexibleStringExpander;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.entity.util.EntityUtilProperties;
import org.apache.ofbiz.party.contact.ContactHelper;
import org.apache.ofbiz.product.product.ProductEvents;
import org.apache.ofbiz.product.store.ProductStoreWorker;
import org.apache.ofbiz.security.SecurityUtil;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ModelService;
import org.apache.ofbiz.service.ServiceUtil;
import org.apache.ofbiz.webapp.control.LoginWorker;
/**
* LoginEvents - Events for UserLogin and Security handling.
*/
public class LoginEvents {
public static final String module = LoginEvents.class.getName();
public static final String resource = "SecurityextUiLabels";
public static final String usernameCookieName = "OFBiz.Username";
/**
* Save USERNAME and PASSWORD for use by auth pages even if we start in non-auth pages.
*
* @param request The HTTP request object for the current JSP or Servlet request.
* @param response The HTTP response object for the current JSP or Servlet request.
* @return String
*/
public static String saveEntryParams(HttpServletRequest request, HttpServletResponse response) {
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
HttpSession session = request.getSession();
Delegator delegator = (Delegator) request.getAttribute("delegator");
// save entry login parameters if we don't have a valid login object
if (userLogin == null) {
String username = request.getParameter("USERNAME");
String password = request.getParameter("PASSWORD");
if ((username != null) && ("true".equalsIgnoreCase(EntityUtilProperties.getPropertyValue("security", "username.lowercase", delegator)))) {
username = username.toLowerCase(Locale.getDefault());
}
if ((password != null) && ("true".equalsIgnoreCase(EntityUtilProperties.getPropertyValue("security", "password.lowercase", delegator)))) {
password = password.toLowerCase(Locale.getDefault());
}
// save parameters into the session - so they can be used later, if needed
if (username != null) {
session.setAttribute("USERNAME", username);
}
if (password != null) {
session.setAttribute("PASSWORD", password);
}
} else {
// if the login object is valid, remove attributes
session.removeAttribute("USERNAME");
session.removeAttribute("PASSWORD");
}
return "success";
}
/**
* The user forgot his/her password. This will call showPasswordHint, emailPassword or simply returns "success" in case
* no operation has been specified.
*
* @param request The HTTPRequest object for the current request
* @param response The HTTPResponse object for the current request
* @return String specifying the exit status of this event
*/
public static String forgotPassword(HttpServletRequest request, HttpServletResponse response) {
if (UtilValidate.isNotEmpty(request.getParameter("GET_PASSWORD_HINT")) || UtilValidate.isNotEmpty(request.getParameter("GET_PASSWORD_HINT.x"))) {
return showPasswordHint(request, response);
} else if ((UtilValidate.isNotEmpty(request.getParameter("EMAIL_PASSWORD"))) || (UtilValidate.isNotEmpty(request.getParameter("EMAIL_PASSWORD.x")))) {
return emailPasswordRequest(request, response);
}
return "success";
}
/** Show the password hint for the userLoginId specified in the request object.
*@param request The HTTPRequest object for the current request
*@param response The HTTPResponse object for the current request
*@return String specifying the exit status of this event
*/
public static String showPasswordHint(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
String userLoginId = request.getParameter("USERNAME");
String errMsg = null;
if ((userLoginId != null) && ("true".equals(EntityUtilProperties.getPropertyValue("security", "username.lowercase", delegator)))) {
userLoginId = userLoginId.toLowerCase(Locale.getDefault());
}
if (UtilValidate.isEmpty(userLoginId)) {
// the password was incomplete
errMsg = UtilProperties.getMessage(resource, "loginevents.username_was_empty_reenter", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String questionEnumId = request.getParameter("securityQuestion");
String securityAnswer = request.getParameter("securityAnswer");
if (UtilValidate.isEmpty(questionEnumId) && UtilValidate.isEmpty(securityAnswer)) {
return "success";
}
try {
GenericValue userLoginSecurityQuestion = EntityQuery.use(delegator).from("UserLoginSecurityQuestion").where("questionEnumId", questionEnumId, "userLoginId", userLoginId).cache().queryOne();
if (userLoginSecurityQuestion != null) {
if (UtilValidate.isEmpty(securityAnswer)) {
errMsg = UtilProperties.getMessage(resource, "loginservices.security_answer_empty", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String ulSecurityAnswer = userLoginSecurityQuestion.getString("securityAnswer");
if (UtilValidate.isNotEmpty(ulSecurityAnswer) && ! securityAnswer.equalsIgnoreCase(ulSecurityAnswer)) {
errMsg = UtilProperties.getMessage(resource, "loginservices.security_answer_not_match", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
}
} catch (GenericEntityException e) {
errMsg = UtilProperties.getMessage(resource, "loginevents.problem_getting_security_question_record", UtilHttp.getLocale(request));
Debug.logError(e, errMsg, module);
}
GenericValue supposedUserLogin = null;
String passwordHint = null;
try {
supposedUserLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne();
} catch (GenericEntityException gee) {
Debug.logWarning(gee, "", module);
}
if (supposedUserLogin != null) {
passwordHint = supposedUserLogin.getString("passwordHint");
}
if (supposedUserLogin == null || UtilValidate.isEmpty(passwordHint)) {
// the Username was not found or there was no hint for the Username
errMsg = UtilProperties.getMessage(resource, "loginevents.no_password_hint_specified_try_password_emailed", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
Map<String, String> messageMap = UtilMisc.toMap("passwordHint", passwordHint);
errMsg = UtilProperties.getMessage(resource, "loginevents.password_hint_is", messageMap, UtilHttp.getLocale(request));
request.setAttribute("_EVENT_MESSAGE_", errMsg);
return "auth";
}
/**
* event to send an email with a link to change password
*
* @param request The HTTPRequest object for the current request
* @param response The HTTPResponse object for the current request
* @return String specifying the exit status of this event
*/
public static String emailPasswordRequest(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
String productStoreId = ProductStoreWorker.getProductStoreId(request);
String defaultScreenLocation = "component://securityext/widget/EmailSecurityScreens.xml#PasswordEmail";
// get userloginId
String userLoginId = request.getParameter("USERNAME");
if (UtilValidate.isEmpty(userLoginId)) {
String errMsg = UtilProperties.getMessage(resource, "loginevents.username_was_empty_reenter",
UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
if (UtilValidate.isNotEmpty(request.getParameter("token"))) {
return "success";
}
GenericValue userLogin;
try {
// test if user exist and is active
userLogin = EntityQuery.use(delegator)
.from("UserLogin")
.where("userLoginId", userLoginId)
.queryOne();
if (userLogin == null || "N".equals(userLogin.getString("enabled"))) {
Debug.logError("userlogin uknown or disabled " + userLogin, module);
//giving a "sent email to associated email-address" response, to suppress feedback on in-/valid usernames
String errMsg = UtilProperties.getMessage(resource, "loginevents.new_password_sent_check_email",
UtilHttp.getLocale(request));
request.setAttribute("_EVENT_MESSAGE_", errMsg);
return "success";
}
// check login is associated to a party
GenericValue userParty = userLogin.getRelatedOne("Party", false);
if (userParty == null) {
String errMsg = UtilProperties.getMessage(resource, "loginevents.username_not_found_reenter",
UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
// check there is an email to send to
List<GenericValue> contactMechs = (List<GenericValue>) ContactHelper.getContactMechByPurpose(userParty, "PRIMARY_EMAIL", false);
if (UtilValidate.isEmpty(contactMechs)) {
// the email was not found
String errMsg = UtilProperties.getMessage(resource,
"loginevents.no_primary_email_address_set_contact_customer_service",
UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String emails = contactMechs.stream()
.map(email -> email.getString("infoString"))
.collect(Collectors.joining(","));
//Generate a JWT with default retention time
String jwtToken = SecurityUtil.generateJwtToAuthenticateUserLogin(delegator, userLoginId);
// get the ProductStore email settings
GenericValue productStoreEmail = null;
try {
productStoreEmail = EntityQuery.use(delegator).from("ProductStoreEmailSetting").where("productStoreId", productStoreId, "emailType", "PRDS_PWD_RETRIEVE").queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting ProductStoreEmailSetting", module);
}
String bodyScreenLocation = null;
if (productStoreEmail != null) {
bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation");
}
if (UtilValidate.isEmpty(bodyScreenLocation)) {
bodyScreenLocation = defaultScreenLocation;
}
// set the needed variables in new context
Map<String, Object> bodyParameters = new HashMap<>();
bodyParameters.put("token", jwtToken);
bodyParameters.put("locale", UtilHttp.getLocale(request));
bodyParameters.put("userLogin", userLogin);
bodyParameters.put("productStoreId", productStoreId);
Map<String, Object> serviceContext = new HashMap<>();
serviceContext.put("bodyScreenUri", bodyScreenLocation);
serviceContext.put("bodyParameters", bodyParameters);
if (productStoreEmail != null) {
serviceContext.put("subject", productStoreEmail.getString("subject"));
serviceContext.put("sendFrom", productStoreEmail.get("fromAddress"));
serviceContext.put("sendCc", productStoreEmail.get("ccAddress"));
serviceContext.put("sendBcc", productStoreEmail.get("bccAddress"));
serviceContext.put("contentType", productStoreEmail.get("contentType"));
} else {
GenericValue emailTemplateSetting = null;
try {
emailTemplateSetting = EntityQuery.use(delegator).from("EmailTemplateSetting").where("emailTemplateSettingId", "EMAIL_PASSWORD").cache().queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (emailTemplateSetting != null) {
String subject = emailTemplateSetting.getString("subject");
subject = FlexibleStringExpander.expandString(subject, UtilMisc.toMap("userLoginId", userLoginId));
serviceContext.put("subject", subject);
serviceContext.put("sendFrom", emailTemplateSetting.get("fromAddress"));
} else {
serviceContext.put("subject", UtilProperties.getMessage(resource, "loginservices.password_reminder_subject", UtilMisc.toMap("userLoginId", userLoginId), UtilHttp.getLocale(request)));
serviceContext.put("sendFrom", EntityUtilProperties.getPropertyValue("general", "defaultFromEmailAddress", delegator));
}
}
serviceContext.put("sendTo", emails);
serviceContext.put("partyId", userParty.getString("partyId"));
Map<String, Object> result = dispatcher.runSync("sendMailHiddenInLogFromScreen", serviceContext);
if (ServiceUtil.isError(result)) {
String errorMessage = ServiceUtil.getErrorMessage(result);
request.setAttribute("_ERROR_MESSAGE_", errorMessage);
Debug.logError(errorMessage, module);
return "error";
}
if (ServiceUtil.isError(result)) {
Map<String, Object> messageMap = UtilMisc.toMap("errorMessage", result.get(ModelService.ERROR_MESSAGE));
String errMsg = UtilProperties.getMessage(resource, "loginevents.error_unable_email_password_contact_customer_service_errorwas", messageMap, UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
} catch (GeneralException e) {
Debug.logWarning(e, "", module);
String errMsg = UtilProperties.getMessage(resource, "loginevents.error_unable_email_password_contact_customer_service", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String msg = UtilProperties.getMessage(resource, "loginevents.new_password_sent_check_email", UtilHttp.getLocale(request));
request.setAttribute("_EVENT_MESSAGE_", msg);
return "success";
}
public static String storeCheckLogin(HttpServletRequest request, HttpServletResponse response) {
String responseString = LoginWorker.checkLogin(request, response);
if ("error".equals(responseString)) {
return responseString;
}
// if we are logged in okay, do the check store customer role
return ProductEvents.checkStoreCustomerRole(request, response);
}
public static String storeLogin(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
String responseString = LoginWorker.login(request, response);
if (!"success".equals(responseString)) {
return responseString;
}
// if we logged in okay, do the check store customer role
return ProductEvents.checkStoreCustomerRole(request, response);
}
}