Search in sources :

Example 1 with UsernameAuthenticationToken

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));
}
Also used : UsernameAuthenticationToken(io.vertigo.account.impl.authentication.UsernameAuthenticationToken) UsernamePasswordAuthenticationToken(io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken) AuthenticationToken(io.vertigo.account.authentication.AuthenticationToken) Matcher(java.util.regex.Matcher) UsernameAuthenticationToken(io.vertigo.account.impl.authentication.UsernameAuthenticationToken) UsernamePasswordAuthenticationToken(io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken)

Example 2 with UsernameAuthenticationToken

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();
}
Also used : UsernameAuthenticationToken(io.vertigo.account.impl.authentication.UsernameAuthenticationToken) UsernamePasswordAuthenticationToken(io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken) AuthenticationToken(io.vertigo.account.authentication.AuthenticationToken) DtObject(io.vertigo.dynamo.domain.model.DtObject) UsernameAuthenticationToken(io.vertigo.account.impl.authentication.UsernameAuthenticationToken) UsernamePasswordAuthenticationToken(io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken) Criteria(io.vertigo.dynamo.criteria.Criteria)

Aggregations

AuthenticationToken (io.vertigo.account.authentication.AuthenticationToken)2 UsernameAuthenticationToken (io.vertigo.account.impl.authentication.UsernameAuthenticationToken)2 UsernamePasswordAuthenticationToken (io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken)2 Criteria (io.vertigo.dynamo.criteria.Criteria)1 DtObject (io.vertigo.dynamo.domain.model.DtObject)1 Matcher (java.util.regex.Matcher)1