Search in sources :

Example 11 with AccountExpiredException

use of javax.security.auth.login.AccountExpiredException in project cas by apereo.

the class GoogleAuthenticatorOneTimeTokenCredentialValidator method validate.

@Override
public GoogleAuthenticatorToken validate(final Authentication authentication, final GoogleAuthenticatorTokenCredential tokenCredential) throws GeneralSecurityException, PreventedException {
    if (!StringUtils.isNumeric(tokenCredential.getToken())) {
        throw new PreventedException("Invalid non-numeric OTP format specified.");
    }
    val uid = authentication.getPrincipal().getId();
    val otp = Integer.parseInt(tokenCredential.getToken());
    LOGGER.trace("Received OTP [{}] assigned to account [{}]", otp, tokenCredential.getAccountId());
    LOGGER.trace("Received principal id [{}]. Attempting to locate account in credential repository...", uid);
    val accounts = this.credentialRepository.get(uid);
    if (accounts == null || accounts.isEmpty()) {
        throw new AccountNotFoundException(uid + " cannot be found in the registry");
    }
    if (accounts.size() > 1 && tokenCredential.getAccountId() == null) {
        throw new PreventedException("Account identifier must be specified if multiple accounts are registered for " + uid);
    }
    LOGGER.trace("Attempting to locate OTP token [{}] in token repository for [{}]...", otp, uid);
    if (this.tokenRepository.exists(uid, otp)) {
        throw new AccountExpiredException(uid + " cannot reuse OTP " + otp + " as it may be expired/invalid");
    }
    LOGGER.debug("Attempting to authorize OTP token [{}]...", otp);
    val result = getAuthorizedAccountForToken(tokenCredential, accounts).or(() -> getAuthorizedScratchCodeForToken(tokenCredential, authentication, accounts));
    return result.map(acct -> new GoogleAuthenticatorToken(otp, uid)).orElse(null);
}
Also used : lombok.val(lombok.val) OneTimeTokenCredentialRepository(org.apereo.cas.otp.repository.credentials.OneTimeTokenCredentialRepository) Getter(lombok.Getter) Collection(java.util.Collection) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) IGoogleAuthenticator(com.warrenstrange.googleauth.IGoogleAuthenticator) OneTimeTokenRepository(org.apereo.cas.otp.repository.token.OneTimeTokenRepository) OneTimeTokenCredentialValidator(org.apereo.cas.otp.repository.credentials.OneTimeTokenCredentialValidator) StringUtils(org.apache.commons.lang3.StringUtils) AccountExpiredException(javax.security.auth.login.AccountExpiredException) Slf4j(lombok.extern.slf4j.Slf4j) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException) GeneralSecurityException(java.security.GeneralSecurityException) OneTimeTokenAccount(org.apereo.cas.authentication.OneTimeTokenAccount) Authentication(org.apereo.cas.authentication.Authentication) GoogleAuthenticatorToken(org.apereo.cas.gauth.token.GoogleAuthenticatorToken) Optional(java.util.Optional) PreventedException(org.apereo.cas.authentication.PreventedException) AccountExpiredException(javax.security.auth.login.AccountExpiredException) GoogleAuthenticatorToken(org.apereo.cas.gauth.token.GoogleAuthenticatorToken) PreventedException(org.apereo.cas.authentication.PreventedException) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException)

Aggregations

AccountExpiredException (javax.security.auth.login.AccountExpiredException)11 FailedLoginException (javax.security.auth.login.FailedLoginException)9 AccountNotFoundException (javax.security.auth.login.AccountNotFoundException)8 lombok.val (lombok.val)8 AccountLockedException (javax.security.auth.login.AccountLockedException)5 AccountDisabledException (org.apereo.cas.authentication.exceptions.AccountDisabledException)5 AccountPasswordMustChangeException (org.apereo.cas.authentication.exceptions.AccountPasswordMustChangeException)5 CredentialExpiredException (javax.security.auth.login.CredentialExpiredException)4 GeneralSecurityException (java.security.GeneralSecurityException)3 Principal (java.security.Principal)2 LinkedHashMap (java.util.LinkedHashMap)2 Subject (javax.security.auth.Subject)2 Configuration (javax.security.auth.login.Configuration)2 LoginContext (javax.security.auth.login.LoginContext)2 LoginException (javax.security.auth.login.LoginException)2 HttpResponse (org.apache.http.HttpResponse)2 Authentication (org.apereo.cas.authentication.Authentication)2 PreventedException (org.apereo.cas.authentication.PreventedException)2 SimpleSecurityContext (com.nimbusds.jose.proc.SimpleSecurityContext)1 IGoogleAuthenticator (com.warrenstrange.googleauth.IGoogleAuthenticator)1