Search in sources :

Example 1 with GoogleAuthenticatorTokenCredential

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());
}
Also used : lombok.val(lombok.val) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoogleAuthenticatorTokenCredential(org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential) Test(org.junit.jupiter.api.Test)

Example 2 with GoogleAuthenticatorTokenCredential

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);
}
Also used : lombok.val(lombok.val) FailedLoginException(javax.security.auth.login.FailedLoginException) GoogleAuthenticatorTokenCredential(org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential)

Example 3 with GoogleAuthenticatorTokenCredential

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);
}
Also used : lombok.val(lombok.val) ArrayList(java.util.ArrayList) GoogleAuthenticatorTokenCredential(org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential)

Example 4 with GoogleAuthenticatorTokenCredential

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));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DefaultMessageContext(org.springframework.binding.message.DefaultMessageContext) MessageSource(org.springframework.context.MessageSource) GoogleAuthenticatorTokenCredential(org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 5 with GoogleAuthenticatorTokenCredential

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));
}
Also used : lombok.val(lombok.val) GoogleAuthenticatorTokenCredential(org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)6 GoogleAuthenticatorTokenCredential (org.apereo.cas.gauth.credential.GoogleAuthenticatorTokenCredential)6 Test (org.junit.jupiter.api.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ArrayList (java.util.ArrayList)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 DefaultMessageContext (org.springframework.binding.message.DefaultMessageContext)1 MessageSource (org.springframework.context.MessageSource)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)1 MockRequestContext (org.springframework.webflow.test.MockRequestContext)1