use of io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken in project vertigo by KleeGroup.
the class TextAuthenticationPlugin method parseUserInfo.
private void parseUserInfo(final String line) {
final Matcher matcher = FILE_PATTERN.matcher(line);
final boolean matches = matcher.matches();
Assertion.checkState(matches, "No match found for entry '{0}' and pattern '{1}'", line, FILE_PATTERN_STR);
// ---
final String accountKey = matcher.group(1);
final String username = matcher.group(2);
final String password = matcher.group(3);
final AuthenticationToken authenticationToken;
if (password.isEmpty()) {
authenticationToken = new UsernameAuthenticationToken(username);
} else {
authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
}
users.put(username, new AuthenticationAccountInfo(accountKey, authenticationToken));
}
use of io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken in project vertigo by KleeGroup.
the class AuthenticationManagerTest method testLoginFail.
@Test
public void testLoginFail() {
final AuthenticationToken token = new UsernamePasswordAuthenticationToken("badUserName", "badPassword");
final Optional<Account> account = authenticationManager.login(token);
Assert.assertFalse("Shouldn't found any account with a bad login", account.isPresent());
final Optional<UserSession> userSession = securityManager.getCurrentUserSession();
Assert.assertTrue("No UserSession", userSession.isPresent());
Assert.assertFalse("Badly authenticated", userSession.get().isAuthenticated());
}
use of io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken in project vertigo by KleeGroup.
the class LdapAuthenticationPlugin method authenticateAccount.
/**
* {@inheritDoc}
*/
@Override
public Optional<String> authenticateAccount(final AuthenticationToken token) {
Assertion.checkNotNull(token);
// ---
final UsernamePasswordAuthenticationToken usernamePasswordToken = (UsernamePasswordAuthenticationToken) token;
LdapContext ldapContext = null;
try {
final String userProtectedDn = userLoginPrefix + protectLdap(usernamePasswordToken.getPrincipal()) + userLoginSuffix;
ldapContext = createLdapContext(userProtectedDn, usernamePasswordToken.getPassword());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Ouverture de connexion LDAP '" + ldapContext + "'");
}
return Optional.of(token.getPrincipal());
} catch (final NamingException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.info("Can't authenticate user '" + token.getPrincipal() + "'", e);
} else {
LOGGER.info("Can't authenticate user '" + token.getPrincipal() + "'");
}
// can't connect user
return Optional.empty();
} finally {
if (ldapContext != null) {
closeLdapContext(ldapContext);
}
}
}
use of io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken in project vertigo by KleeGroup.
the class StoreAuthenticationPlugin method start.
/**
* {@inheritDoc}
*/
@Override
public void start() {
userCredentialDefinition = Home.getApp().getDefinitionSpace().resolve(userCredentialEntity, DtDefinition.class);
defaultUserTrustedCredential = new UsernamePasswordAuthenticationToken("defaultLogin", "defaultPassword");
}
use of io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken in project vertigo by KleeGroup.
the class StoreAuthenticationPlugin method authenticateAccount.
/**
* {@inheritDoc}
*/
@Override
public Optional<String> authenticateAccount(final AuthenticationToken token) {
final Criteria criteriaByLogin = Criterions.isEqualTo(() -> userLoginField, token.getPrincipal());
final DtList<DtObject> results = storeManager.getDataStore().find(userCredentialDefinition, criteriaByLogin);
// may ensure, that valid or invalid login took the same time, so we don't assert no result here
Assertion.checkState(results.size() <= 1, "Too many matching credentials for {0}", token.getPrincipal());
final AuthenticationToken trustedAuthenticationToken;
if (token instanceof UsernamePasswordAuthenticationToken) {
if (results.isEmpty()) {
trustedAuthenticationToken = defaultUserTrustedCredential;
} else {
final String trustedEncodedPassword = (String) userCredentialDefinition.getField(userPasswordField).getDataAccessor().getValue(results.get(0));
trustedAuthenticationToken = new UsernamePasswordAuthenticationToken(token.getPrincipal(), trustedEncodedPassword);
}
} else {
if (results.isEmpty()) {
trustedAuthenticationToken = defaultUserTrustedCredential;
} else {
trustedAuthenticationToken = new UsernameAuthenticationToken(token.getPrincipal());
}
}
// may ensure, that valid or invalid login took the same time, so we don't assert no result here
if (// tokens match
token.match(trustedAuthenticationToken) && !results.isEmpty()) {
// and Username exists (after)
final String userTokenId = (String) userCredentialDefinition.getField(userTokenIdField).getDataAccessor().getValue(results.get(0));
return Optional.of(userTokenId);
}
return Optional.empty();
}
Aggregations