Search in sources :

Example 1 with OAuth1Builder

use of org.glassfish.jersey.client.oauth1.OAuth1Builder 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

URI (java.net.URI)1 Client (javax.ws.rs.client.Client)1 WebTarget (javax.ws.rs.client.WebTarget)1 Feature (javax.ws.rs.core.Feature)1 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)1 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)1 OAuth1Builder (org.glassfish.jersey.client.oauth1.OAuth1Builder)1 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)1 OAuth1SignatureFeature (org.glassfish.jersey.oauth1.signature.OAuth1SignatureFeature)1 JerseyTest (org.glassfish.jersey.test.JerseyTest)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1