Skip to content

Commit c8db225

Browse files
authored
fix: username or password error mesage shown with failed attempt (#265)
1 parent 46195c5 commit c8db225

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/main/java/com/iemr/common/controller/users/IEMRAdminController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,10 @@ public String forgetPassword(
620620

621621
if (mUsers == null || mUsers.size() <= 0) {
622622
logger.error("User not found");
623-
throw new IEMRException("If the username is valid, you will be asked a security question");
623+
throw new IEMRException("If the username is registered, you will be asked a security question");
624624
} else if (mUsers.size() > 1) {
625625
logger.error("More than 1 user found");
626-
throw new IEMRException("If the username is valid, you will be asked a security question");
626+
throw new IEMRException("If the username is registered, you will be asked a security question");
627627

628628
} else if (mUsers.size() == 1) {
629629
List<Map<String, String>> quesAnsList = new ArrayList<>();

src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,19 @@ public void setValidator(Validator validator) {
220220
this.validator = validator;
221221
}
222222

223+
private void checkUserAccountStatus(User user) throws IEMRException {
224+
if (user.getDeleted()) {
225+
throw new IEMRException("Your account is locked or de-activated. Please contact administrator");
226+
} else if (user.getStatusID() > 2) {
227+
throw new IEMRException("Your account is not active. Please contact administrator");
228+
}
229+
}
230+
223231
@Override
224232
public List<User> userAuthenticate(String userName, String password) throws Exception {
225233
List<User> users = iEMRUserRepositoryCustom.findByUserNameNew(userName);
226234
if (users.size() != 1) {
227235
throw new IEMRException("Invalid username or password");
228-
} else {
229-
if (users.get(0).getDeleted())
230-
throw new IEMRException("Your account is locked or de-activated. Please contact administrator");
231-
else if (users.get(0).getStatusID() > 2)
232-
throw new IEMRException("Your account is not active. Please contact administrator");
233236
}
234237
int failedAttempt = 0;
235238
if (failedLoginAttempt != null)
@@ -241,6 +244,7 @@ else if (users.get(0).getStatusID() > 2)
241244
int validatePassword;
242245
validatePassword = securePassword.validatePassword(password, user.getPassword());
243246
if (validatePassword == 1) {
247+
checkUserAccountStatus(user);
244248
int iterations = 1001;
245249
char[] chars = password.toCharArray();
246250
byte[] salt = getSalt();
@@ -254,29 +258,37 @@ else if (users.get(0).getStatusID() > 2)
254258
iEMRUserRepositoryCustom.save(user);
255259

256260
} else if (validatePassword == 2) {
261+
checkUserAccountStatus(user);
257262
iEMRUserRepositoryCustom.save(user);
258263

259264
} else if (validatePassword == 3) {
265+
checkUserAccountStatus(user);
260266
iEMRUserRepositoryCustom.save(user);
261267
} else if (validatePassword == 0) {
262-
if (user.getFailedAttempt() + 1 >= failedAttempt) {
268+
if (user.getFailedAttempt() + 1 < failedAttempt) {
269+
user.setFailedAttempt(user.getFailedAttempt() + 1);
270+
user = iEMRUserRepositoryCustom.save(user);
271+
logger.warn("User Password Wrong");
272+
throw new IEMRException("Invalid username or password");
273+
} else if (user.getFailedAttempt() + 1 >= failedAttempt) {
263274
user.setFailedAttempt(user.getFailedAttempt() + 1);
264275
user.setDeleted(true);
265276
user = iEMRUserRepositoryCustom.save(user);
266277
logger.warn("User Account has been locked after reaching the limit of {} failed login attempts.",
267278
ConfigProperties.getInteger("failedLoginAttempt"));
268279

269280
throw new IEMRException(
270-
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
281+
"Invalid username or password. Please contact administrator.");
271282
} else {
272283
user.setFailedAttempt(user.getFailedAttempt() + 1);
273284
user = iEMRUserRepositoryCustom.save(user);
274285
logger.warn("Failed login attempt {} of {} for a user account.",
275286
user.getFailedAttempt(), ConfigProperties.getInteger("failedLoginAttempt"));
276287
throw new IEMRException(
277-
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
288+
"Invalid username or password. Please contact administrator.");
278289
}
279290
} else {
291+
checkUserAccountStatus(user);
280292
if (user.getFailedAttempt() != 0) {
281293
user.setFailedAttempt(0);
282294
user = iEMRUserRepositoryCustom.save(user);
@@ -310,11 +322,6 @@ public User superUserAuthenticate(String userName, String password) throws Excep
310322

311323
if (users.size() != 1) {
312324
throw new IEMRException("Invalid username or password");
313-
} else {
314-
if (users.get(0).getDeleted())
315-
throw new IEMRException("Your account is locked or de-activated. Please contact administrator");
316-
else if (users.get(0).getStatusID() > 2)
317-
throw new IEMRException("Your account is not active. Please contact administrator");
318325
}
319326
int failedAttempt = 0;
320327
if (failedLoginAttempt != null)
@@ -326,6 +333,7 @@ else if (users.get(0).getStatusID() > 2)
326333
int validatePassword;
327334
validatePassword = securePassword.validatePassword(password, user.getPassword());
328335
if (validatePassword == 1) {
336+
checkUserAccountStatus(user);
329337
int iterations = 1001;
330338
char[] chars = password.toCharArray();
331339
byte[] salt = getSalt();
@@ -339,27 +347,34 @@ else if (users.get(0).getStatusID() > 2)
339347
iEMRUserRepositoryCustom.save(user);
340348

341349
} else if (validatePassword == 2) {
350+
checkUserAccountStatus(user);
342351
iEMRUserRepositoryCustom.save(user);
343352

344353
} else if (validatePassword == 0) {
345-
if (user.getFailedAttempt() + 1 >= failedAttempt) {
354+
if (user.getFailedAttempt() + 1 < failedAttempt) {
355+
user.setFailedAttempt(user.getFailedAttempt() + 1);
356+
user = iEMRUserRepositoryCustom.save(user);
357+
logger.warn("User Password Wrong");
358+
throw new IEMRException("Invalid username or password");
359+
} else if (user.getFailedAttempt() + 1 >= failedAttempt) {
346360
user.setFailedAttempt(user.getFailedAttempt() + 1);
347361
user.setDeleted(true);
348362
user = iEMRUserRepositoryCustom.save(user);
349363
logger.warn("User Account has been locked after reaching the limit of {} failed login attempts.",
350364
ConfigProperties.getInteger("failedLoginAttempt"));
351365

352366
throw new IEMRException(
353-
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
367+
"Invalid username or password. Please contact administrator.");
354368
} else {
355369
user.setFailedAttempt(user.getFailedAttempt() + 1);
356370
user = iEMRUserRepositoryCustom.save(user);
357371
logger.warn("Failed login attempt {} of {} for a user account.",
358372
user.getFailedAttempt(), ConfigProperties.getInteger("failedLoginAttempt"));
359373
throw new IEMRException(
360-
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
374+
"Invalid username or password. Please contact administrator.");
361375
}
362376
} else {
377+
checkUserAccountStatus(user);
363378
if (user.getFailedAttempt() != 0) {
364379
user.setFailedAttempt(0);
365380
user = iEMRUserRepositoryCustom.save(user);

0 commit comments

Comments
 (0)