Search in sources :

Example 1 with TokenResult

use of org.glassfish.jersey.client.oauth2.TokenResult 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 2 with TokenResult

use of org.glassfish.jersey.client.oauth2.TokenResult in project jersey by jersey.

the class AuthorizationResource method authorize.

@GET
@Path("authorize")
public Response authorize(@QueryParam("code") String code, @QueryParam("state") String state) {
    final OAuth2CodeGrantFlow flow = SimpleOAuthService.getFlow();
    final TokenResult tokenResult = flow.finish(code, state);
    SimpleOAuthService.setAccessToken(tokenResult.getAccessToken());
    // authorization is finished -> now redirect back to the task resource
    final URI uri = UriBuilder.fromUri(uriInfo.getBaseUri()).path("tasks").build();
    return Response.seeOther(uri).build();
}
Also used : TokenResult(org.glassfish.jersey.client.oauth2.TokenResult) OAuth2CodeGrantFlow(org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow) URI(java.net.URI) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

OAuth2CodeGrantFlow (org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow)2 TokenResult (org.glassfish.jersey.client.oauth2.TokenResult)2 IOException (java.io.IOException)1 URI (java.net.URI)1 Collection (java.util.Collection)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Client (javax.ws.rs.client.Client)1 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)1 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)1 Response (javax.ws.rs.core.Response)1 ClientIdentifier (org.glassfish.jersey.client.oauth2.ClientIdentifier)1