Search in sources :

Example 1 with GoogleAuthenticatorToken

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));
}
Also used : GoogleAuthenticatorToken(org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with GoogleAuthenticatorToken

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;
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) GoogleAuthenticatorToken(org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken) NoResultException(javax.persistence.NoResultException)

Example 3 with GoogleAuthenticatorToken

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);
}
Also used : OneTimeToken(org.apereo.cas.otp.repository.token.OneTimeToken) GoogleAuthenticatorToken(org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with GoogleAuthenticatorToken

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);
}
Also used : OneTimeToken(org.apereo.cas.otp.repository.token.OneTimeToken) GoogleAuthenticatorToken(org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with GoogleAuthenticatorToken

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);
}
Also used : OneTimeTokenAccount(org.apereo.cas.otp.repository.credentials.OneTimeTokenAccount) FailedLoginException(javax.security.auth.login.FailedLoginException) Authentication(org.apereo.cas.authentication.Authentication) AccountExpiredException(javax.security.auth.login.AccountExpiredException) NonNull(lombok.NonNull) GoogleAuthenticatorToken(org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken) PreventedException(org.apereo.cas.authentication.PreventedException) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException)

Aggregations

GoogleAuthenticatorToken (org.apereo.cas.adaptors.gauth.token.GoogleAuthenticatorToken)5 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 OneTimeToken (org.apereo.cas.otp.repository.token.OneTimeToken)2 NoResultException (javax.persistence.NoResultException)1 AccountExpiredException (javax.security.auth.login.AccountExpiredException)1 AccountNotFoundException (javax.security.auth.login.AccountNotFoundException)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 NonNull (lombok.NonNull)1 Authentication (org.apereo.cas.authentication.Authentication)1 PreventedException (org.apereo.cas.authentication.PreventedException)1 OneTimeTokenAccount (org.apereo.cas.otp.repository.credentials.OneTimeTokenAccount)1 Query (org.springframework.data.mongodb.core.query.Query)1