use of org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential in project cas by apereo.
the class GoogleAuthenticatorRestHttpRequestCredentialFactoryTests method verifyCredentials.
@Test
public void verifyCredentials() {
val f = new GoogleAuthenticatorRestHttpRequestCredentialFactory();
val body = new LinkedMultiValueMap<String, String>();
body.add(GoogleAuthenticatorRestHttpRequestCredentialFactory.PARAMETER_NAME_GAUTH_OTP, "132456");
body.add(GoogleAuthenticatorRestHttpRequestCredentialFactory.PARAMETER_NAME_GAUTH_ACCT, "132456");
val results = f.fromRequest(new MockHttpServletRequest(), body);
assertFalse(results.isEmpty());
val credential = (GoogleAuthenticatorTokenCredential) results.get(0);
assertEquals("132456", credential.getId());
assertEquals(132456, credential.getAccountId());
}
use of org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential in project cas by apereo.
the class GoogleAuthenticatorAuthenticationHandler method doAuthentication.
@Override
protected AuthenticationHandlerExecutionResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
val tokenCredential = (GoogleAuthenticatorTokenCredential) credential;
val authentication = WebUtils.getInProgressAuthentication();
val validatedToken = validator.validate(authentication, tokenCredential);
if (validatedToken != null) {
val principal = authentication.getPrincipal().getId();
LOGGER.debug("Validated OTP token [{}] successfully for [{}]", validatedToken, principal);
validator.store(validatedToken);
LOGGER.debug("Creating authentication result and building principal for [{}]", principal);
return createHandlerResult(tokenCredential, this.principalFactory.createPrincipal(principal));
}
LOGGER.warn("Authorization of OTP token [{}] has failed", credential);
throw new FailedLoginException("Failed to authenticate code " + credential);
}
use of org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential in project cas by apereo.
the class GoogleAuthenticatorRestHttpRequestCredentialFactory method fromRequest.
@Override
public List<Credential> fromRequest(final HttpServletRequest request, final MultiValueMap<String, String> requestBody) {
if (requestBody == null || requestBody.isEmpty()) {
LOGGER.debug("Skipping [{}] because the requestBody is null or empty", getClass().getSimpleName());
return new ArrayList<>(0);
}
val token = requestBody.getFirst(PARAMETER_NAME_GAUTH_OTP);
val id = requestBody.getFirst(PARAMETER_NAME_GAUTH_ACCT);
LOGGER.debug("Google authenticator token [{}] in the request body via account [{}]", token, id);
if (StringUtils.isBlank(token)) {
return new ArrayList<>(0);
}
val creds = new GoogleAuthenticatorTokenCredential(token, StringUtils.isNotBlank(id) ? Long.valueOf(id) : null);
return CollectionUtils.wrap(creds);
}
use of org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential in project cas by apereo.
the class GoogleAuthenticatorValidateSelectedRegistrationActionTests method verifyOperation.
@Test
public void verifyOperation() throws Exception {
val context = new MockRequestContext();
val messageContext = (DefaultMessageContext) context.getMessageContext();
messageContext.setMessageSource(mock(MessageSource.class));
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
RequestContextHolder.setRequestContext(context);
ExternalContextHolder.setExternalContext(context.getExternalContext());
val action = new GoogleAuthenticatorValidateSelectedRegistrationAction();
assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, action.execute(context).getId());
val acct = OneTimeTokenAccount.builder().username("casuser").name(UUID.randomUUID().toString()).secretKey("secret").validationCode(123456).scratchCodes(List.of()).build();
WebUtils.putOneTimeTokenAccount(context, acct);
assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, action.execute(context).getId());
WebUtils.putCredential(context, new GoogleAuthenticatorTokenCredential("token", 987655L));
assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, action.execute(context).getId());
WebUtils.putCredential(context, new GoogleAuthenticatorTokenCredential("token", acct.getId()));
assertNull(action.execute(context));
}
use of org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential in project cas by apereo.
the class GoogleAuthenticatorAuthenticationHandlerTests method verifySupports.
@Test
public void verifySupports() {
val credential = new GoogleAuthenticatorTokenCredential();
assertTrue(handler.supports(credential));
assertTrue(handler.supports(GoogleAuthenticatorTokenCredential.class));
}
Aggregations