Java Examples for org.glassfish.jersey.client.oauth2.OAuth2ClientSupport

The following java examples will help you to understand the usage of org.glassfish.jersey.client.oauth2.OAuth2ClientSupport. These source code samples are taken from different open source projects.

Example 1
Project: jerseyoauth2-master  File: Jersey2Test.java View source code
@Test
public void simpleResourceTest() throws UnsupportedEncodingException {
    ClientIdentifier cliendId = new ClientIdentifier(clientEntity.getClientId(), clientEntity.getClientSecret());
    Builder<?> flowBuilder = OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(cliendId, "http://localhost:9998/testsuite/oauth2/auth", "http://localhost:9998/testsuite/oauth2/accessToken");
    OAuth2CodeGrantFlow flow = flowBuilder.scope("test1 test2").redirectUri("http://localhost:9998/testsuite").build();
    String authUrl = flow.start();
    Map<String, String> map = retrieveCode(authUrl);
    String code = map.get("code");
    String state = map.get("state");
    TokenResult tokenResult = flow.finish(code, state);
    client = ClientBuilder.newClient();
    client.register(LoggingFilter.class);
    client.register(OAuth2ClientSupport.feature(tokenResult.getAccessToken()));
    client.register(JacksonFeature.class);
    Invocation.Builder builder = client.target("http://localhost:9998/testsuite/rest/sample/1").request();
    Response response = builder.get();
    assertEquals(200, response.getStatus());
    SampleEntity entity = response.readEntity(SampleEntity.class);
    assertNotNull(entity);
}
Example 2
Project: jive-sdk-java-jersey-master  File: JiveOAuth2Service.java View source code
@GET
@Path("/authorize")
public Response authorize(@Context HttpServletRequest request, @Context UriInfo uriInfo) {
    //TODO: HEADER/QUERY - PARAM
    String userID = "TODO";
    //TODO: HEADER/QUERY - PARAM
    String instanceID = "TODO";
    ClientIdentifier clientID = new ClientIdentifier(serviceConfig.getClientID(), serviceConfig.getClientSecret());
    OAuth2CodeGrantFlow flow = OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(clientID, serviceConfig.getAuthorizeUrl(), serviceConfig.getTokenUrl()).scope(serviceConfig.getScope()).redirectUri(uriInfo.getBaseUri() + "oauth2/callback").build();
    String authorizationUrl = flow.start();
    try {
        URI authorizationUri = new URI(authorizationUrl);
        /** LOAD INTO SESSION FOR FOLLOW-UP HIT **/
        request.getSession().setAttribute(getFlowSessionKey(), flow);
        request.getSession().setAttribute(getInstanceIDSessionKey(), instanceID);
        request.getSession().setAttribute(getUserIDSessionKey(), userID);
        //*** NOTE: 303 "See Other" NEEDED FOR JERSEY FLOW TO PICK UP
        return Response.seeOther(new URI(authorizationUrl)).build();
    } catch (URISyntaxException use) {
        log.error("Invalid Authorization URI: " + authorizationUrl);
        return Response.serverError().entity("Unable to Process this Request").build();
    }
// end try/catch
}
Example 3
Project: jersey-master  File: OAuth2Test.java View source code
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"));
    }
}