Search in sources :

Example 56 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class OAuth2Test method testFlow.

private void testFlow(final boolean isArray) {
    ClientIdentifier clientId = new ClientIdentifier(CLIENT_PUBLIC, CLIENT_SECRET);
    final String authUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("authorization").build().toString();
    final String accessTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("access-token").build().toString();
    final String refreshTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("refresh-token").build().toString();
    final String state = STATE;
    final Client client = ClientBuilder.newClient();
    if (isArray) {
        client.register(new ClientRequestFilter() {

            @Override
            public void filter(final ClientRequestContext requestContext) throws IOException {
                requestContext.getHeaders().putSingle("isArray", true);
            }
        });
    }
    final OAuth2CodeGrantFlow.Builder builder = OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(clientId, authUri, accessTokenUri);
    final OAuth2CodeGrantFlow flow = builder.client(client).refreshTokenUri(refreshTokenUri).property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, "readOnly", "true").property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, OAuth2Parameters.STATE, state).scope("contact").build();
    final String finalAuthorizationUri = flow.start();
    final Response response = ClientBuilder.newClient().target(finalAuthorizationUri).request().get();
    assertEquals(200, response.getStatus());
    final String code = response.readEntity(String.class);
    assertEquals(CODE, code);
    final TokenResult result = flow.finish(code, state);
    assertEquals("access-token-aab999f", result.getAccessToken());
    assertEquals(new Long(3600), result.getExpiresIn());
    assertEquals("access-token", result.getTokenType());
    final TokenResult refreshResult = flow.refreshAccessToken(result.getRefreshToken());
    assertEquals("access-token-new", refreshResult.getAccessToken());
    assertEquals(new Long(3600), refreshResult.getExpiresIn());
    assertEquals("access-token", refreshResult.getTokenType());
    if (isArray) {
        final Collection<String> array = (Collection<String>) refreshResult.getAllProperties().get("access_token");
        assertThat(array.size(), is(1));
        assertThat(array, hasItem("access-token-new"));
    }
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientIdentifier(org.glassfish.jersey.client.oauth2.ClientIdentifier) TokenResult(org.glassfish.jersey.client.oauth2.TokenResult) OAuth2CodeGrantFlow(org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) Collection(java.util.Collection) Client(javax.ws.rs.client.Client)

Example 57 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class OAuthClientServerTest method testRequestSigningWithExceedingCache.

/**
     * Tests configuration of the nonce cache on the server side.
     */
@Test
public void testRequestSigningWithExceedingCache() {
    final Feature filterFeature = OAuth1ClientSupport.builder(new ConsumerCredentials(CONSUMER_KEY, SECRET_CONSUMER_KEY)).feature().accessToken(new AccessToken(PROMETHEUS_TOKEN, PROMETHEUS_SECRET)).build();
    final Client client = ClientBuilder.newBuilder().register(filterFeature).build();
    final URI resourceUri = UriBuilder.fromUri(getBaseUri()).path("resource").build();
    final WebTarget target = client.target(resourceUri);
    Response response;
    for (int i = 0; i < 20; i++) {
        System.out.println("request: " + i);
        response = target.request().get();
        assertEquals(200, response.getStatus());
        assertEquals("prometheus", response.readEntity(String.class));
        i++;
        response = target.path("admin").request().get();
        assertEquals(200, response.getStatus());
        assertEquals(true, response.readEntity(boolean.class));
    }
    // now the nonce cache is full
    response = target.request().get();
    assertEquals(401, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ConsumerCredentials(org.glassfish.jersey.client.oauth1.ConsumerCredentials) AccessToken(org.glassfish.jersey.client.oauth1.AccessToken) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Feature(javax.ws.rs.core.Feature) OAuth1ServerFeature(org.glassfish.jersey.server.oauth1.OAuth1ServerFeature) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) URI(java.net.URI) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 58 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class OAuthClientServerTest method testAuthorizationFlow.

/**
     * Tests client and server OAuth.
     * <p/>
     * Tests authorization flow including the request to a protected resource. The test uses {@link OAuth1AuthorizationFlow}
     * to perform user authorization and uses authorized client for requesting protected resource.
     * <p/>
     * The resource {@link OAuthAuthorizationResource} is used to perform user authorization (this is done
     * programmatically from the test). Finally, the Access Token is retrieved and used to request the
     * protected resource. In this resource the user principal is used to return the name of the user stored
     * in {@link SecurityContext}.
     */
@Test
public void testAuthorizationFlow() {
    String tempCredUri = UriBuilder.fromUri(getBaseUri()).path("requestTokenSpecialUri").build().toString();
    String accessTokenUri = UriBuilder.fromUri(getBaseUri()).path("accessTokenSpecialUri").build().toString();
    final String userAuthorizationUri = UriBuilder.fromUri(getBaseUri()).path("user-authorization").build().toString();
    final OAuth1AuthorizationFlow authFlow = OAuth1ClientSupport.builder(new ConsumerCredentials(CONSUMER_KEY, SECRET_CONSUMER_KEY)).authorizationFlow(tempCredUri, accessTokenUri, userAuthorizationUri).callbackUri("http://consumer/callback/homer").build();
    final String authUri = authFlow.start();
    // authorize by a request to authorization URI
    final Response userAuthResponse = ClientBuilder.newClient().target(authUri).request().get();
    assertEquals(200, userAuthResponse.getStatus());
    final String verifier = userAuthResponse.readEntity(String.class);
    System.out.println("Verifier: " + verifier);
    authFlow.finish(verifier);
    final Client authorizedClient = authFlow.getAuthorizedClient();
    Response response = authorizedClient.target(getBaseUri()).path("resource").request().get();
    assertEquals(200, response.getStatus());
    assertEquals("homer", response.readEntity(String.class));
    response = authorizedClient.target(getBaseUri()).path("resource").path("admin").request().get();
    assertEquals(200, response.getStatus());
    assertEquals(false, response.readEntity(boolean.class));
}
Also used : Response(javax.ws.rs.core.Response) OAuth1AuthorizationFlow(org.glassfish.jersey.client.oauth1.OAuth1AuthorizationFlow) ConsumerCredentials(org.glassfish.jersey.client.oauth1.ConsumerCredentials) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 59 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class OAuthClientServerTest method testRequestSigning.

/**
     * Tests {@link org.glassfish.jersey.client.oauth1.OAuth1ClientFilter} already configured with Access Token for signature
     * purposes only.
     */
@Test
public void testRequestSigning() {
    final Feature filterFeature = OAuth1ClientSupport.builder(new ConsumerCredentials(CONSUMER_KEY, SECRET_CONSUMER_KEY)).feature().accessToken(new AccessToken(PROMETHEUS_TOKEN, PROMETHEUS_SECRET)).build();
    final Client client = ClientBuilder.newBuilder().register(filterFeature).build();
    final URI resourceUri = UriBuilder.fromUri(getBaseUri()).path("resource").build();
    final WebTarget target = client.target(resourceUri);
    Response response;
    for (int i = 0; i < 15; i++) {
        System.out.println("request: " + i);
        response = target.request().get();
        assertEquals(200, response.getStatus());
        assertEquals("prometheus", response.readEntity(String.class));
        i++;
        response = target.path("admin").request().get();
        assertEquals(200, response.getStatus());
        assertEquals(true, response.readEntity(boolean.class));
    }
}
Also used : Response(javax.ws.rs.core.Response) ConsumerCredentials(org.glassfish.jersey.client.oauth1.ConsumerCredentials) AccessToken(org.glassfish.jersey.client.oauth1.AccessToken) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Feature(javax.ws.rs.core.Feature) OAuth1ServerFeature(org.glassfish.jersey.server.oauth1.OAuth1ServerFeature) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) URI(java.net.URI) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 60 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class OauthClientAuthorizationFlowTest method testOAuthClientFeature.

/**
     * Tests mainly the client functionality. The test client registers
     * {@link org.glassfish.jersey.client.oauth1.OAuth1ClientFilter} and uses the filter only to sign requests. So, it does not
     * use the filter to perform authorization flow. However, each request that this test performs is actually a request used
     * during the authorization flow.
     * <p/>
     * The server side of this test extracts header authorization values and tests that signatures are
     * correct for each request type.
     */
@Test
public void testOAuthClientFeature() {
    final URI baseUri = getBaseUri();
    // baseline for requests
    final OAuth1Builder oAuth1Builder = OAuth1ClientSupport.builder(new ConsumerCredentials("dpf43f3p2l4k3l03", "kd94hf93k423kf44")).timestamp("1191242090").nonce("hsu94j3884jdopsl").signatureMethod(PlaintextMethod.NAME).version("1.0");
    final Feature feature = oAuth1Builder.feature().build();
    final Client client = client();
    client.register(LoggingFeature.class);
    final WebTarget target = client.target(baseUri);
    // simulate request for Request Token (temporary credentials)
    String responseEntity = target.path("request_token").register(feature).request().post(Entity.entity("entity", MediaType.TEXT_PLAIN_TYPE), String.class);
    assertEquals(responseEntity, "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03");
    final Feature feature2 = oAuth1Builder.timestamp("1191242092").nonce("dji430splmx33448").feature().accessToken(new AccessToken("hh5s93j4hdidpola", "hdhd0244k9j7ao03")).build();
    // simulate request for Access Token
    responseEntity = target.path("access_token").register(feature2).request().post(Entity.entity("entity", MediaType.TEXT_PLAIN_TYPE), String.class);
    assertEquals(responseEntity, "oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00");
    final Feature feature3 = oAuth1Builder.nonce("kllo9940pd9333jh").signatureMethod("HMAC-SHA1").timestamp("1191242096").feature().accessToken(new AccessToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00")).build();
    // based on Access Token
    responseEntity = target.path("/photos").register(feature3).queryParam("file", "vacation.jpg").queryParam("size", "original").request().get(String.class);
    assertEquals(responseEntity, "PHOTO");
}
Also used : ConsumerCredentials(org.glassfish.jersey.client.oauth1.ConsumerCredentials) AccessToken(org.glassfish.jersey.client.oauth1.AccessToken) OAuth1Builder(org.glassfish.jersey.client.oauth1.OAuth1Builder) WebTarget(javax.ws.rs.client.WebTarget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Client(javax.ws.rs.client.Client) URI(java.net.URI) Feature(javax.ws.rs.core.Feature) OAuth1SignatureFeature(org.glassfish.jersey.oauth1.signature.OAuth1SignatureFeature) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

Client (javax.ws.rs.client.Client)279 Test (org.junit.Test)192 WebTarget (javax.ws.rs.client.WebTarget)120 Response (javax.ws.rs.core.Response)107 ClientConfig (org.glassfish.jersey.client.ClientConfig)77 JerseyTest (org.glassfish.jersey.test.JerseyTest)76 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)21 URL (java.net.URL)20 ClientResponse (org.glassfish.jersey.client.ClientResponse)19 Invocation (javax.ws.rs.client.Invocation)18 ClientBuilder (javax.ws.rs.client.ClientBuilder)17 Before (org.junit.Before)17 IOException (java.io.IOException)15 ProcessingException (javax.ws.rs.ProcessingException)15 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 URI (java.net.URI)12 WebClient (org.apache.cxf.jaxrs.client.WebClient)11 HttpServer (org.glassfish.grizzly.http.server.HttpServer)10 PrintWriter (java.io.PrintWriter)9 SSLContext (javax.net.ssl.SSLContext)9