Search in sources :

Example 21 with Book

use of org.apache.cxf.systest.jaxrs.security.Book in project cxf by apache.

the class OAuth2FiltersTest method testServiceWithTokenUsingAudience.

@org.junit.Test
public void testServiceWithTokenUsingAudience() throws Exception {
    URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
    // Get Authorization Code
    String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
    WebClient oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String code = OAuth2TestUtils.getAuthorizationCode(oauthClient, null, "consumer-id-aud");
    assertNotNull(code);
    // Now get the access token
    oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "consumer-id-aud", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String address = "https://localhost:" + PORT + "/secured/bookstore/books";
    ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(oauthClient, code, "consumer-id-aud", address);
    assertNotNull(accessToken.getTokenKey());
    // Now invoke on the service with the access token
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
    client.header("Authorization", "Bearer " + accessToken.getTokenKey());
    Response response = client.type("application/xml").post(new Book("book", 123L));
    assertEquals(response.getStatus(), 200);
    Book returnedBook = response.readEntity(Book.class);
    assertEquals(returnedBook.getName(), "book");
    assertEquals(returnedBook.getId(), 123L);
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 22 with Book

use of org.apache.cxf.systest.jaxrs.security.Book in project cxf by apache.

the class OAuth2FiltersTest method testServiceWithFakeToken.

@org.junit.Test
public void testServiceWithFakeToken() throws Exception {
    URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
    // Now invoke on the service with the faked access token
    String address = "https://localhost:" + PORT + "/secured/bookstore/books";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
    client.header("Authorization", "Bearer " + UUID.randomUUID().toString());
    Response response = client.post(new Book("book", 123L));
    assertNotEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 23 with Book

use of org.apache.cxf.systest.jaxrs.security.Book in project cxf by apache.

the class OAuth2FiltersTest method testServiceWithTokenUsingIncorrectAudience.

@org.junit.Test
public void testServiceWithTokenUsingIncorrectAudience() throws Exception {
    URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
    // Get Authorization Code
    String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
    WebClient oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String code = OAuth2TestUtils.getAuthorizationCode(oauthClient, null, "consumer-id-aud2");
    assertNotNull(code);
    // Now get the access token
    oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "consumer-id-aud2", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String address = "https://localhost:" + PORT + "/securedxyz/bookstore/books";
    ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(oauthClient, code, "consumer-id-aud2", address);
    assertNotNull(accessToken.getTokenKey());
    // Now invoke on the service with the access token
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
    client.header("Authorization", "Bearer " + accessToken.getTokenKey());
    Response response = client.post(new Book("book", 123L));
    assertNotEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 24 with Book

use of org.apache.cxf.systest.jaxrs.security.Book in project cxf by apache.

the class OAuth2FiltersTest method testServiceWithEmptyToken.

@org.junit.Test
public void testServiceWithEmptyToken() throws Exception {
    URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
    // Now invoke on the service with the faked access token
    String address = "https://localhost:" + PORT + "/secured/bookstore/books";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
    client.header("Authorization", "Bearer ");
    Response response = client.post(new Book("book", 123L));
    assertNotEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 25 with Book

use of org.apache.cxf.systest.jaxrs.security.Book in project cxf by apache.

the class OAuth2FiltersTest method testServiceWithTokenAndMultipleScopes.

@org.junit.Test
public void testServiceWithTokenAndMultipleScopes() throws Exception {
    URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
    // Get Authorization Code
    String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
    WebClient oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String code = OAuth2TestUtils.getAuthorizationCode(oauthClient, "read_book create_image create_book");
    assertNotNull(code);
    // Now get the access token
    oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(oauthClient, code);
    assertNotNull(accessToken.getTokenKey());
    // Now invoke on the service with the access token
    String address = "https://localhost:" + PORT + "/secured/bookstore/books";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
    client.header("Authorization", "Bearer " + accessToken.getTokenKey());
    Response response = client.type("application/xml").post(new Book("book", 123L));
    assertEquals(response.getStatus(), 200);
    Book returnedBook = response.readEntity(Book.class);
    assertEquals(returnedBook.getName(), "book");
    assertEquals(returnedBook.getId(), 123L);
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Aggregations

Book (org.apache.cxf.systest.jaxrs.security.Book)114 WebClient (org.apache.cxf.jaxrs.client.WebClient)94 URL (java.net.URL)85 HashMap (java.util.HashMap)74 Response (javax.ws.rs.core.Response)73 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)64 ArrayList (java.util.ArrayList)60 Test (org.junit.Test)36 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)34 JwtAuthenticationClientFilter (org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)32 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)28 WebApplicationException (javax.ws.rs.WebApplicationException)15 JweWriterInterceptor (org.apache.cxf.rs.security.jose.jaxrs.JweWriterInterceptor)15 BookStore (org.apache.cxf.systest.jaxrs.security.jose.BookStore)15 Bus (org.apache.cxf.Bus)12 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)12 JAXRSClientFactoryBean (org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean)12 JwsWriterInterceptor (org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor)12 ZonedDateTime (java.time.ZonedDateTime)10 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)10