use of io.vertigo.account.authentication.AuthenticationToken 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.authentication.AuthenticationToken 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.authentication.AuthenticationToken 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();
}
use of io.vertigo.account.authentication.AuthenticationToken in project vertigo by KleeGroup.
the class AuthenticationManagerTest method loginSuccess.
private Optional<Account> loginSuccess() {
final AuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", "v3rt1g0");
final Optional<Account> account = authenticationManager.login(token);
Assert.assertTrue("Authent fail", account.isPresent());
final Optional<UserSession> userSession = securityManager.getCurrentUserSession();
Assert.assertTrue("No UserSession", userSession.isPresent());
Assert.assertTrue("Not authenticated", userSession.get().isAuthenticated());
return account;
}
Aggregations