use of io.vertigo.account.impl.authentication.UsernameAuthenticationToken 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.UsernameAuthenticationToken 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