Search in sources :

Example 16 with TokenIntrospection

use of org.apache.cxf.rs.security.oauth2.common.TokenIntrospection in project testcases by coheigea.

the class IntrospectionServiceTest method testTokenIntrospectionWithScope.

@org.junit.Test
public void testTokenIntrospectionWithScope() throws Exception {
    URL busFile = IntrospectionServiceTest.class.getResource("cxf-client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Get Authorization Code
    String code = getAuthorizationCode(client, "read_balance");
    assertNotNull(code);
    // Now get the access token
    client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code);
    assertNotNull(accessToken.getTokenKey());
    assertTrue(accessToken.getApprovedScope().contains("read_balance"));
    // Now query the token introspection service
    client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    client.accept("application/json").type("application/x-www-form-urlencoded");
    Form form = new Form();
    form.param("token", accessToken.getTokenKey());
    client.path("introspect/");
    Response response = client.post(form);
    TokenIntrospection tokenIntrospection = response.readEntity(TokenIntrospection.class);
    assertEquals(tokenIntrospection.isActive(), true);
    assertEquals(tokenIntrospection.getUsername(), "alice");
    assertEquals(tokenIntrospection.getClientId(), "consumer-id");
    assertEquals(tokenIntrospection.getScope(), accessToken.getApprovedScope());
    Long validity = tokenIntrospection.getExp() - tokenIntrospection.getIat();
    assertTrue(validity == accessToken.getExpiresIn());
}
Also used : Response(javax.ws.rs.core.Response) TokenIntrospection(org.apache.cxf.rs.security.oauth2.common.TokenIntrospection) Form(javax.ws.rs.core.Form) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Aggregations

TokenIntrospection (org.apache.cxf.rs.security.oauth2.common.TokenIntrospection)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)10 URL (java.net.URL)9 Form (javax.ws.rs.core.Form)9 Response (javax.ws.rs.core.Response)9 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)9 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Annotation (java.lang.annotation.Annotation)3 Test (org.junit.Test)3 UserSubject (org.apache.cxf.rs.security.oauth2.common.UserSubject)2 Instant (java.time.Instant)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JsonMapObjectReaderWriter (org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter)1 AccessTokenValidation (org.apache.cxf.rs.security.oauth2.common.AccessTokenValidation)1