use of org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken in project cas by apereo.
the class GoogleAuthenticatorJpaTokenRepositoryTests method verifyTokenSave.
@Test
public void verifyTokenSave() {
final GoogleAuthenticatorToken token = new GoogleAuthenticatorToken(1234, "casuser");
repository.store(token);
assertTrue(repository.exists("casuser", 1234));
}
use of org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken in project cas by apereo.
the class GoogleAuthenticatorMongoDbTokenRepository method get.
@Override
public GoogleAuthenticatorToken get(final String uid, final Integer otp) {
try {
final Query query = new Query();
query.addCriteria(Criteria.where("userId").is(uid).and("token").is(otp));
final GoogleAuthenticatorToken r = this.mongoTemplate.findOne(query, GoogleAuthenticatorToken.class, this.collectionName);
return r;
} catch (final NoResultException e) {
LOGGER.debug("No record could be found for google authenticator id [{}]", uid);
}
return null;
}
use of org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken in project cas by apereo.
the class GoogleAuthenticatorMongoDbTokenRepositoryTests method verifyTokenSave.
@Test
public void verifyTokenSave() {
OneTimeToken token = new GoogleAuthenticatorToken(1234, "casuser");
repository.store(token);
assertTrue(repository.exists("casuser", 1234));
token = repository.get("casuser", 1234);
assertTrue(token.getId() > 0);
}
use of org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken in project cas by apereo.
the class GoogleAuthenticatorMongoDbTokenRepositoryTests method verifyTokensWithUniqueIdsSave.
@Test
public void verifyTokensWithUniqueIdsSave() {
final OneTimeToken token = new GoogleAuthenticatorToken(1111, "casuser");
repository.store(token);
final OneTimeToken token2 = new GoogleAuthenticatorToken(5678, "casuser");
repository.store(token2);
final OneTimeToken t1 = repository.get("casuser", 1111);
final OneTimeToken t2 = repository.get("casuser", 5678);
assertTrue(t1.getId() > 0);
assertTrue(t2.getId() > 0);
assertNotEquals(token.getId(), token2.getId());
assertTrue(t1.getToken() == 1111);
}
use of org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken in project cas by apereo.
the class GoogleAuthenticatorAuthenticationHandler method doAuthentication.
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
final GoogleAuthenticatorTokenCredential tokenCredential = (GoogleAuthenticatorTokenCredential) credential;
if (!StringUtils.isNumeric(tokenCredential.getToken())) {
throw new PreventedException("Invalid non-numeric OTP format specified.", new IllegalArgumentException("Invalid token " + tokenCredential.getToken()));
}
final int otp = Integer.parseInt(tokenCredential.getToken());
LOGGER.debug("Received OTP [{}]", otp);
@NonNull final Authentication authentication = WebUtils.getInProgressAuthentication();
final String uid = authentication.getPrincipal().getId();
LOGGER.debug("Received principal id [{}]", uid);
final OneTimeTokenAccount acct = this.credentialRepository.get(uid);
if (acct == null || StringUtils.isBlank(acct.getSecretKey())) {
throw new AccountNotFoundException(uid + " cannot be found in the registry");
}
if (this.tokenRepository.exists(uid, otp)) {
throw new AccountExpiredException(uid + " cannot reuse OTP " + otp + " as it may be expired/invalid");
}
boolean isCodeValid = this.googleAuthenticatorInstance.authorize(acct.getSecretKey(), otp);
if (!isCodeValid && acct.getScratchCodes().contains(otp)) {
LOGGER.warn("Using scratch code [{}] to authenticate user [{}]. Scratch code will be removed", otp, uid);
acct.getScratchCodes().removeIf(token -> token == otp);
this.credentialRepository.update(acct);
isCodeValid = true;
}
if (isCodeValid) {
this.tokenRepository.store(new GoogleAuthenticatorToken(otp, uid));
return createHandlerResult(tokenCredential, this.principalFactory.createPrincipal(uid));
}
throw new FailedLoginException("Failed to authenticate code " + otp);
}
Aggregations