Search in sources :

Example 46 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestConsentRepository method findConsentDecisions.

@Override
public Collection<ConsentDecision> findConsentDecisions(final String principal) {
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
        headers.put("principal", CollectionUtils.wrap(principal));
        final HttpEntity<String> entity = new HttpEntity<>(headers);
        final ResponseEntity<List> result = restTemplate.exchange(this.endpoint, HttpMethod.GET, entity, List.class);
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return result.getBody();
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new ArrayList<>(0);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ToString(lombok.ToString)

Example 47 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestConsentRepository method storeConsentDecision.

@Override
public boolean storeConsentDecision(final ConsentDecision decision) {
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
        final HttpEntity<ConsentDecision> entity = new HttpEntity<>(decision, headers);
        final ResponseEntity<ConsentDecision> result = restTemplate.exchange(this.endpoint, HttpMethod.POST, entity, ConsentDecision.class);
        return result.getStatusCodeValue() == HttpStatus.OK.value();
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity)

Example 48 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestGoogleAuthenticatorTokenCredentialRepository method get.

@Override
public OneTimeTokenAccount get(final String username) {
    final GAuthMultifactorProperties.Rest rest = gauth.getRest();
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
    headers.put("username", CollectionUtils.wrap(username));
    final HttpEntity<String> entity = new HttpEntity<>(headers);
    final ResponseEntity<OneTimeTokenAccount> result = restTemplate.exchange(rest.getEndpointUrl(), HttpMethod.GET, entity, OneTimeTokenAccount.class);
    if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
        return decode(result.getBody());
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) OneTimeTokenAccount(org.apereo.cas.otp.repository.credentials.OneTimeTokenAccount) HttpEntity(org.springframework.http.HttpEntity) GAuthMultifactorProperties(org.apereo.cas.configuration.model.support.mfa.GAuthMultifactorProperties)

Example 49 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestGoogleAuthenticatorTokenCredentialRepository method update.

@Override
public OneTimeTokenAccount update(final OneTimeTokenAccount accountToUpdate) {
    final GAuthMultifactorProperties.Rest rest = gauth.getRest();
    final OneTimeTokenAccount account = encode(accountToUpdate);
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
    headers.put("username", CollectionUtils.wrap(account.getUsername()));
    headers.put("validationCode", CollectionUtils.wrap(String.valueOf(account.getValidationCode())));
    headers.put("secretKey", CollectionUtils.wrap(account.getSecretKey()));
    headers.put("scratchCodes", account.getScratchCodes().stream().map(String::valueOf).collect(Collectors.toList()));
    final HttpEntity<String> entity = new HttpEntity<>(headers);
    final ResponseEntity<Boolean> result = restTemplate.exchange(rest.getEndpointUrl(), HttpMethod.POST, entity, Boolean.class);
    if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
        LOGGER.debug("Posted google authenticator account successfully");
        return account;
    }
    LOGGER.warn("Failed to save google authenticator account successfully");
    return null;
}
Also used : OneTimeTokenAccount(org.apereo.cas.otp.repository.credentials.OneTimeTokenAccount) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) GAuthMultifactorProperties(org.apereo.cas.configuration.model.support.mfa.GAuthMultifactorProperties)

Example 50 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestfulAuthenticationPolicy method isSatisfiedBy.

@Override
public boolean isSatisfiedBy(final Authentication authentication) throws Exception {
    try {
        final HttpHeaders acceptHeaders = new HttpHeaders();
        acceptHeaders.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
        final HttpEntity<Principal> entity = new HttpEntity<>(authentication.getPrincipal(), acceptHeaders);
        LOGGER.warn("Checking authentication policy for [{}] via POST at [{}]", authentication.getPrincipal(), this.endpoint);
        final ResponseEntity<String> resp = restTemplate.exchange(this.endpoint, HttpMethod.POST, entity, String.class);
        if (resp == null) {
            LOGGER.warn("[{}] returned no responses", this.endpoint);
            throw new GeneralSecurityException("No response returned from REST endpoint to determine authentication policy");
        }
        if (resp.getStatusCode() != HttpStatus.OK) {
            final Exception ex = handleResponseStatusCode(resp.getStatusCode(), authentication.getPrincipal());
            throw new GeneralSecurityException(ex);
        }
        return true;
    } catch (final HttpClientErrorException e) {
        final Exception ex = handleResponseStatusCode(e.getStatusCode(), authentication.getPrincipal());
        throw new GeneralSecurityException(ex);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) GeneralSecurityException(java.security.GeneralSecurityException) Principal(org.apereo.cas.authentication.principal.Principal) AccountLockedException(javax.security.auth.login.AccountLockedException) AccountDisabledException(org.apereo.cas.authentication.exceptions.AccountDisabledException) AccountExpiredException(javax.security.auth.login.AccountExpiredException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException) GeneralSecurityException(java.security.GeneralSecurityException) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountPasswordMustChangeException(org.apereo.cas.authentication.exceptions.AccountPasswordMustChangeException)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)104 HttpHeaders (org.springframework.http.HttpHeaders)81 Test (org.junit.Test)46 RestTemplate (org.springframework.web.client.RestTemplate)17 URI (java.net.URI)15 ArrayList (java.util.ArrayList)13 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Map (java.util.Map)11 MediaType (org.springframework.http.MediaType)11 HashMap (java.util.HashMap)10 List (java.util.List)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6