use of org.springframework.security.authentication.BadCredentialsException in project webofneeds by researchstudio-sat.
the class WebIdUserDetailsService method loadUserDetails.
@Override
public UserDetails loadUserDetails(final PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
String principal = (String) token.getPrincipal();
Certificate certificate = (Certificate) token.getCredentials();
logger.debug("Adding userDetails for '" + principal + "'");
URI webID = null;
try {
webID = new URI(principal);
} catch (URISyntaxException e) {
throw new BadCredentialsException("Principal of X.509 Certificate must be a WebId URI. Actual value: '" + principal + "'");
}
// at this point, we know that a client certificate was presented. Grant this role:
List<GrantedAuthority> authorities = new ArrayList<>(3);
authorities.add(new SimpleGrantedAuthority("ROLE_CLIENT_CERTIFICATE_PRESENTED"));
logger.debug("verifying webId '" + principal + "'");
try {
if (webIDVerificationAgent.verify(certificate.getPublicKey(), webID)) {
authorities.add(new SimpleGrantedAuthority("ROLE_WEBID"));
logger.debug("webId '" + principal + "' successfully verified - ROLE_WEBID granted");
} else {
logger.debug("could not verify webId '" + principal + "'. ROLE_WEBID not granted");
}
} catch (Exception e) {
logger.debug("could not verify webId '" + principal + "' because of an error during verification. ROLE_WEBID " + "not granted. Cause is logged", e);
}
stopWatch.stop();
logger.debug("webID check took " + stopWatch.getLastTaskTimeMillis() + " millis");
return new WebIdUserDetails(webID, authorities);
}
use of org.springframework.security.authentication.BadCredentialsException in project midpoint by Evolveum.
the class TestAbstractAuthenticationEvaluator method test101PasswordLoginBadPasswordJack.
@Test
public void test101PasswordLoginBadPasswordJack() throws Exception {
final String TEST_NAME = "test101PasswordLoginBadPasswordJack";
TestUtil.displayTestTile(TEST_NAME);
// GIVEN
ConnectionEnvironment connEnv = createConnectionEnvironment();
XMLGregorianCalendar startTs = clock.currentTimeXMLGregorianCalendar();
try {
// WHEN
TestUtil.displayWhen(TEST_NAME);
getAuthenticationEvaluator().authenticate(connEnv, getAuthenticationContext(USER_JACK_USERNAME, getBadPasswordJack()));
AssertJUnit.fail("Unexpected success");
} catch (BadCredentialsException e) {
// This is expected
// THEN
TestUtil.displayThen(TEST_NAME);
display("expected exception", e);
assertBadPasswordException(e, USER_JACK_USERNAME);
}
XMLGregorianCalendar endTs = clock.currentTimeXMLGregorianCalendar();
PrismObject<UserType> userAfter = getUser(USER_JACK_OID);
display("user after", userAfter);
assertFailedLogins(userAfter, 1);
assertUserLockout(userAfter, LockoutStatusType.NORMAL);
assertLastFailedLogin(userAfter, startTs, endTs);
}
use of org.springframework.security.authentication.BadCredentialsException in project midpoint by Evolveum.
the class TestAbstractAuthenticationEvaluator method test202UserGuybrushPasswordLoginBadPassword.
@Test
public void test202UserGuybrushPasswordLoginBadPassword() throws Exception {
final String TEST_NAME = "test202UserGuybrushPasswordLoginBadPassword";
TestUtil.displayTestTile(TEST_NAME);
// GIVEN
ConnectionEnvironment connEnv = createConnectionEnvironment();
XMLGregorianCalendar startTs = clock.currentTimeXMLGregorianCalendar();
try {
// WHEN
TestUtil.displayWhen(TEST_NAME);
getAuthenticationEvaluator().authenticate(connEnv, getAuthenticationContext(USER_GUYBRUSH_USERNAME, getBadPasswordGuybrush()));
AssertJUnit.fail("Unexpected success");
} catch (BadCredentialsException e) {
// This is expected
// THEN
TestUtil.displayThen(TEST_NAME);
display("expected exception", e);
assertBadPasswordException(e, USER_GUYBRUSH_USERNAME);
}
XMLGregorianCalendar endTs = clock.currentTimeXMLGregorianCalendar();
PrismObject<UserType> userAfter = getUser(USER_GUYBRUSH_OID);
display("user after", userAfter);
assertFailedLogins(userAfter, 1);
assertLastFailedLogin(userAfter, startTs, endTs);
}
use of org.springframework.security.authentication.BadCredentialsException in project midpoint by Evolveum.
the class TestAbstractAuthenticationEvaluator method test125PasswordLoginBadPasswordJackAfterLockoutFailedAttemptsDuration.
/**
* Wait for 5 minutes. The failed login count should reset after 3 minutes. Therefore bad login
* count should be one after we try to make a bad login.
*/
@Test
public void test125PasswordLoginBadPasswordJackAfterLockoutFailedAttemptsDuration() throws Exception {
final String TEST_NAME = "test125PasswordLoginBadPasswordJackAfterLockoutFailedAttemptsDuration";
TestUtil.displayTestTile(TEST_NAME);
// GIVEN
clock.overrideDuration("PT5M");
ConnectionEnvironment connEnv = createConnectionEnvironment();
XMLGregorianCalendar startTs = clock.currentTimeXMLGregorianCalendar();
try {
// WHEN
TestUtil.displayWhen(TEST_NAME);
getAuthenticationEvaluator().authenticate(connEnv, getAuthenticationContext(USER_JACK_USERNAME, getBadPasswordJack()));
AssertJUnit.fail("Unexpected success");
} catch (BadCredentialsException e) {
// This is expected
// THEN
TestUtil.displayThen(TEST_NAME);
display("expected exception", e);
assertBadPasswordException(e, USER_JACK_USERNAME);
}
XMLGregorianCalendar endTs = clock.currentTimeXMLGregorianCalendar();
PrismObject<UserType> userAfter = getUser(USER_JACK_OID);
display("user after", userAfter);
assertFailedLogins(userAfter, 1);
assertLastFailedLogin(userAfter, startTs, endTs);
assertUserLockout(userAfter, LockoutStatusType.NORMAL);
}
use of org.springframework.security.authentication.BadCredentialsException in project dhis2-core by dhis2.
the class DhisBindAuthenticator method authenticate.
@Override
public DirContextOperations authenticate(Authentication authentication) {
boolean ldapConf = configurationProvider.isLdapConfigured();
if (!ldapConf) {
throw new BadCredentialsException("LDAP authentication is not configured");
}
UserCredentials userCredentials = userService.getUserCredentialsByUsername(authentication.getName());
if (userCredentials == null) {
throw new BadCredentialsException("Incorrect user credentials");
}
if (userCredentials.hasLdapId()) {
authentication = new UsernamePasswordAuthenticationToken(userCredentials.getLdapId(), authentication.getCredentials());
}
return super.authenticate(authentication);
}
Aggregations