use of org.springframework.security.authentication.LockedException in project dhis2-core by dhis2.
the class TwoFactorAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
log.debug(String.format("Login attempt: %s", auth.getName()));
String username = auth.getName();
User user = userService.getUserWithEagerFetchAuthorities(username);
if (user == null) {
throw new BadCredentialsException("Invalid username or password");
}
// Initialize all required properties of user credentials since these
// will become detached
user.getAllAuthorities();
if (user.isTwoFA() && auth.getDetails() instanceof TwoFactorWebAuthenticationDetails) {
TwoFactorWebAuthenticationDetails authDetails = (TwoFactorWebAuthenticationDetails) auth.getDetails();
if (authDetails == null) {
log.info("Missing authentication details in authentication request.");
throw new PreAuthenticatedCredentialsNotFoundException("Missing authentication details in authentication request.");
}
String ip = authDetails.getIp();
String code = StringUtils.deleteWhitespace(authDetails.getCode());
if (securityService.isLocked(username)) {
log.debug(String.format("Temporary lockout for user: %s and IP: %s", username, ip));
throw new LockedException(String.format("IP is temporarily locked: %s", ip));
}
if (!LongValidator.getInstance().isValid(code) || !SecurityUtils.verify(user, code)) {
log.debug(String.format("Two-factor authentication failure for user: %s", user.getUsername()));
throw new BadCredentialsException("Invalid verification code");
}
} else if (user.isTwoFA() && !(auth.getDetails() instanceof TwoFactorWebAuthenticationDetails)) {
throw new BadCredentialsException("Can't authenticate non form based login with 2FA enabled");
}
// -------------------------------------------------------------------------
// Delegate authentication downstream, using User as
// principal
// -------------------------------------------------------------------------
Authentication result = super.authenticate(auth);
// Put detached state of the user credentials into the session as user
// must not be updated during session execution
user = SerializationUtils.clone(user);
// Initialize cached authorities
user.isSuper();
user.getAllAuthorities();
return new UsernamePasswordAuthenticationToken(user, result.getCredentials(), result.getAuthorities());
}
use of org.springframework.security.authentication.LockedException in project OpenClinica by OpenClinica.
the class OpenClinicaUsernamePasswordAuthenticationFilter method attemptAuthentication.
// ~ Methods ========================================================================================================
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if (postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
String username = obtainUsername(request);
String password = obtainPassword(request);
if (username == null) {
username = "";
}
if (password == null) {
password = "";
}
username = username.trim();
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
// Place the last username attempted into HttpSession for views
HttpSession session = request.getSession(false);
if (session != null || getAllowSessionCreation()) {
request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, TextEscapeUtils.escapeEntities(username));
}
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
Authentication authentication = null;
UserAccountBean userAccountBean = null;
ResourceBundleProvider.updateLocale(new Locale("en_US"));
try {
EntityBean eb = getUserAccountDao().findByUserName(username);
userAccountBean = eb.getId() != 0 ? (UserAccountBean) eb : null;
authentication = this.getAuthenticationManager().authenticate(authRequest);
auditUserLogin(username, LoginStatus.SUCCESSFUL_LOGIN, userAccountBean);
resetLockCounter(username, LoginStatus.SUCCESSFUL_LOGIN, userAccountBean);
} catch (LockedException le) {
auditUserLogin(username, LoginStatus.FAILED_LOGIN_LOCKED, userAccountBean);
throw le;
} catch (BadCredentialsException au) {
auditUserLogin(username, LoginStatus.FAILED_LOGIN, userAccountBean);
lockAccount(username, LoginStatus.FAILED_LOGIN, userAccountBean);
throw au;
} catch (AuthenticationException ae) {
throw ae;
}
return authentication;
}
Aggregations