use of org.apereo.cas.authentication.OneTimeTokenAccount in project cas by apereo.
the class LdapGoogleAuthenticatorTokenCredentialRepository method update.
@Override
public OneTimeTokenAccount update(final OneTimeTokenAccount account) {
if (account.getId() < 0) {
account.setId(RandomUtils.nextLong());
}
LOGGER.debug("Storing account [{}]", account);
val entry = locateLdapEntryFor(account.getUsername());
val ldapAttribute = Objects.requireNonNull(entry, () -> String.format("Unable to locate LDAP entry for %s", account.getUsername())).getAttribute(ldapProperties.getAccountAttributeName());
if (ldapAttribute == null || ldapAttribute.getStringValues().isEmpty()) {
LOGGER.debug("Adding new account for LDAP entry [{}]", entry);
updateAccount(account, entry);
} else {
val existingAccounts = ldapAttribute.getStringValues().stream().map(LdapGoogleAuthenticatorTokenCredentialRepository::mapFromJson).filter(Objects::nonNull).flatMap(List::stream).map(this::decode).collect(Collectors.toSet());
val matchingAccount = existingAccounts.stream().filter(acct -> acct.getId() == account.getId()).findFirst();
matchingAccount.ifPresentOrElse(ac -> {
ac.setValidationCode(account.getValidationCode());
ac.setScratchCodes(account.getScratchCodes());
ac.setSecretKey(account.getSecretKey());
}, () -> existingAccounts.add(account));
val accountsToSave = existingAccounts.stream().map(acct -> encode(account)).filter(Objects::nonNull).map(acct -> mapToJson(CollectionUtils.wrapArrayList(acct))).collect(Collectors.toSet());
executeModifyOperation(accountsToSave, entry);
}
return account;
}
use of org.apereo.cas.authentication.OneTimeTokenAccount in project cas by apereo.
the class CouchDbGoogleAuthenticatorTokenCredentialRepository method update.
@Override
public OneTimeTokenAccount update(final OneTimeTokenAccount account) {
val records = couchDbRepository.findByUsername(account.getUsername());
if (records == null || records.isEmpty()) {
val newAccount = CouchDbGoogleAuthenticatorAccount.from(encode(account));
couchDbRepository.add(newAccount);
return newAccount;
}
records.stream().filter(rec -> rec.getId() == account.getId()).map(CouchDbGoogleAuthenticatorAccount.class::cast).findFirst().ifPresent(act -> couchDbRepository.update(CouchDbGoogleAuthenticatorAccount.from(account)));
return account;
}
use of org.apereo.cas.authentication.OneTimeTokenAccount in project cas by apereo.
the class GoogleAuthenticatorOneTimeTokenCredentialValidator method getAuthorizedScratchCodeForToken.
/**
* Gets authorized scratch code for token.
*
* @param tokenCredential the token credential
* @param authentication the authentication
* @param accounts the accounts
* @return the authorized scratch code for token
*/
protected Optional<GoogleAuthenticatorAccount> getAuthorizedScratchCodeForToken(final GoogleAuthenticatorTokenCredential tokenCredential, final Authentication authentication, final Collection<? extends OneTimeTokenAccount> accounts) {
val uid = authentication.getPrincipal().getId();
val otp = Integer.parseInt(tokenCredential.getToken());
return accounts.stream().filter(ac -> isCredentialAssignedToAccount(tokenCredential, ac) && ac.getScratchCodes().contains(otp)).map(GoogleAuthenticatorAccount.class::cast).peek(acct -> {
LOGGER.info("Using scratch code [{}] to authenticate user [{}]. Scratch code will be removed", otp, uid);
acct.getScratchCodes().removeIf(token -> token == otp);
credentialRepository.update(acct);
}).findFirst();
}
Aggregations