Search in sources :

Example 6 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class AuthorizationCodeAccessTokenProviderWithConversionTests method testGetAccessTokenFromJson.

@Test
public void testGetAccessTokenFromJson() throws Exception {
    final OAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    requestFactory = new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(new ObjectMapper().writeValueAsString(token));
        }
    };
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    resource.setAccessTokenUri("http://localhost/oauth/token");
    request.setPreservedState(new Object());
    setUpRestTemplate();
    assertEquals(token, provider.obtainAccessToken(resource, request));
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) IOException(java.io.IOException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HttpMethod(org.springframework.http.HttpMethod) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 7 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project cas by apereo.

the class CasRestAuthenticationConfiguration method restAuthenticationTemplate.

@Bean
@RefreshScope
public RestTemplate restAuthenticationTemplate() {
    try {
        final URI casHost = new URI(casProperties.getServer().getName());
        final HttpHost host = new HttpHost(casHost.getHost(), casHost.getPort(), casHost.getScheme());
        final ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
        return new RestTemplate(factory);
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) HttpHost(org.apache.http.HttpHost) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 8 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project uPortal by Jasig.

the class BasicAuthInterceptorTest method doInterceptorTest.

private void doInterceptorTest(PropertyResolver resolver, String id, String expectedAuthCode) throws Exception {
    final String url = "http://www.test.com/lrs";
    final String data = "test";
    final String expectedHeader = "Basic " + expectedAuthCode;
    // holder for the headers...
    HttpHeaders headers = new HttpHeaders();
    // Mock guts of RestTemplate so no need to actually hit the web...
    ClientHttpResponse resp = mock(ClientHttpResponse.class);
    when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(resp.getHeaders()).thenReturn(new HttpHeaders());
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ClientHttpRequest client = mock(ClientHttpRequest.class);
    when(client.getHeaders()).thenReturn(headers);
    when(client.getBody()).thenReturn(buffer);
    when(client.execute()).thenReturn(resp);
    ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class);
    when(factory.createRequest(any(URI.class), any(HttpMethod.class))).thenReturn(client);
    // add the new interceptor...
    BasicAuthInterceptor interceptor = new BasicAuthInterceptor();
    interceptor.setPropertyResolver(resolver);
    interceptor.setId(id);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add(interceptor);
    RestTemplate rest = new RestTemplate(factory);
    rest.setInterceptors(interceptors);
    // do it...
    rest.postForLocation(url, data, Collections.emptyMap());
    // make sure auth header is correctly set...
    assertThat(headers, hasKey(Headers.Authorization.name()));
    assertThat(headers.get(Headers.Authorization.name()), contains(expectedHeader));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod)

Example 9 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project uPortal by Jasig.

the class ZeroLeggedOAuthInterceptorTest method testInterceptor.

@Test
public void testInterceptor() throws Exception {
    final String url = "http://www.test.com/lrs?param1=val1&param2=val2";
    final String data = "test";
    final String id = "test";
    final String realm = "realm";
    final String consumerKey = "consumerKey";
    final String secretKey = "secretKey";
    PropertyResolver resolver = mock(PropertyResolver.class);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".realm"))).thenReturn(realm);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".consumerKey"))).thenReturn(consumerKey);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".secretKey"))).thenReturn(secretKey);
    // holder for the headers...
    HttpHeaders headers = new HttpHeaders();
    // Mock guts of RestTemplate so no need to actually hit the web...
    ClientHttpResponse resp = mock(ClientHttpResponse.class);
    when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(resp.getHeaders()).thenReturn(new HttpHeaders());
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ClientHttpRequest client = mock(ClientHttpRequest.class);
    when(client.getHeaders()).thenReturn(headers);
    when(client.getBody()).thenReturn(buffer);
    when(client.execute()).thenReturn(resp);
    ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class);
    when(factory.createRequest(Matchers.any(URI.class), Matchers.any(HttpMethod.class))).thenReturn(client);
    // add the new interceptor...
    ZeroLeggedOAuthInterceptor interceptor = new ZeroLeggedOAuthInterceptor();
    interceptor.setPropertyResolver(resolver);
    interceptor.setId(id);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add(interceptor);
    RestTemplate rest = new RestTemplate(factory);
    rest.setInterceptors(interceptors);
    rest.postForLocation(url, data, Collections.emptyMap());
    // make sure auth header is correctly set...
    assertThat(headers, hasKey(Headers.Authorization.name()));
    String authHeader = headers.get(Headers.Authorization.name()).get(0);
    assertThat(authHeader, containsString("OAuth realm=\"" + realm + "\""));
    assertThat(authHeader, containsString("oauth_consumer_key=\"" + consumerKey + "\""));
    // for now, only supports HMAC-SHA1.  May have to fix later...
    assertThat(authHeader, containsString("oauth_signature_method=\"HMAC-SHA1\""));
    assertThat(authHeader, containsString("oauth_version=\"1.0\""));
    assertThat(authHeader, containsString("oauth_timestamp="));
    assertThat(authHeader, containsString("oauth_nonce="));
    assertThat(authHeader, containsString("oauth_signature="));
    // oauth lib will create 2 oauth_signature params if you call sign
    // multiple times.  Make sure only get 1.
    assertThat(StringUtils.countMatches(authHeader, "oauth_signature="), is(1));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PropertyResolver(org.springframework.core.env.PropertyResolver) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 10 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class OAuth2RestTemplateTests method testNoRetryAccessDeniedExceptionForNoExistingToken.

@Test(expected = AccessTokenRequiredException.class)
public void testNoRetryAccessDeniedExceptionForNoExistingToken() throws Exception {
    restTemplate.setAccessTokenProvider(new StubAccessTokenProvider());
    restTemplate.setRequestFactory(new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            throw new AccessTokenRequiredException(resource);
        }
    });
    restTemplate.doExecute(new URI("http://foo"), HttpMethod.GET, new NullRequestCallback(), new SimpleResponseExtractor());
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) AccessTokenRequiredException(org.springframework.security.oauth2.client.http.AccessTokenRequiredException) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)23 Test (org.junit.Test)17 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)12 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)12 URI (java.net.URI)11 BufferingClientHttpRequestFactory (org.springframework.http.client.BufferingClientHttpRequestFactory)10 Netty4ClientHttpRequestFactory (org.springframework.http.client.Netty4ClientHttpRequestFactory)10 OkHttp3ClientHttpRequestFactory (org.springframework.http.client.OkHttp3ClientHttpRequestFactory)10 HttpMethod (org.springframework.http.HttpMethod)9 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)9 IOException (java.io.IOException)7 RestTemplate (org.springframework.web.client.RestTemplate)6 HttpHeaders (org.springframework.http.HttpHeaders)4 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2