Search in sources :

Example 56 with Book

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

the class JwsHTTPHeaderTest method testSignAdditionalCustomHeader.

@org.junit.Test
public void testSignAdditionalCustomHeader() throws Exception {
    URL busFile = JwsHTTPHeaderTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    JwsWriterInterceptor jwsWriterInterceptor = new JwsWriterInterceptor();
    jwsWriterInterceptor.setProtectHttpHeaders(true);
    Set<String> headersToSign = new HashSet<>();
    headersToSign.add(HttpHeaders.CONTENT_TYPE);
    headersToSign.add(HttpHeaders.ACCEPT);
    headersToSign.add("customheader");
    jwsWriterInterceptor.setProtectedHttpHeaders(headersToSign);
    providers.add(jwsWriterInterceptor);
    String address = "http://localhost:" + PORT + "/jwsheaderdefault/bookstore/books";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jwk");
    properties.put("rs.security.keystore.alias", "2011-04-29");
    properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt");
    properties.put("rs.security.signature.algorithm", "RS256");
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    WebClient.getConfig(client).getOutInterceptors().add(new CustomHeaderInterceptor(Phase.PRE_STREAM));
    Response response = client.post(new Book("book", 123L));
    response = client.post(new Book("book", 123L));
    assertEquals(response.getStatus(), 200);
}
Also used : HashMap(java.util.HashMap) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) JwsWriterInterceptor(org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor) HashSet(java.util.HashSet)

Example 57 with Book

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

the class JwsHTTPHeaderTest method testSignEmptyCustomHeader.

@org.junit.Test
public void testSignEmptyCustomHeader() throws Exception {
    URL busFile = JwsHTTPHeaderTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    JwsWriterInterceptor jwsWriterInterceptor = new JwsWriterInterceptor();
    jwsWriterInterceptor.setProtectHttpHeaders(true);
    Set<String> headersToSign = new HashSet<>();
    headersToSign.add(HttpHeaders.CONTENT_TYPE);
    headersToSign.add(HttpHeaders.ACCEPT);
    headersToSign.add("customheader");
    jwsWriterInterceptor.setProtectedHttpHeaders(headersToSign);
    providers.add(jwsWriterInterceptor);
    String address = "http://localhost:" + PORT + "/jwsheadercustom/bookstore/books";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jwk");
    properties.put("rs.security.keystore.alias", "2011-04-29");
    properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt");
    properties.put("rs.security.signature.algorithm", "RS256");
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    CustomHeaderInterceptor customHeaderInterceptor = new CustomHeaderInterceptor(Phase.PRE_STREAM);
    customHeaderInterceptor.setEmpty(true);
    assertTrue(customHeaderInterceptor.isEmpty());
    WebClient.getConfig(client).getOutInterceptors().add(customHeaderInterceptor);
    Response response = client.post(new Book("book", 123L));
    response = client.post(new Book("book", 123L));
    assertEquals(response.getStatus(), 200);
}
Also used : HashMap(java.util.HashMap) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) JwsWriterInterceptor(org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor) HashSet(java.util.HashSet)

Example 58 with Book

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

the class JWTAuthnAuthzTest method testClaimsAuthorizationWeakClaims.

@org.junit.Test
public void testClaimsAuthorizationWeakClaims() throws Exception {
    URL busFile = JWTAuthnAuthzTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    providers.add(new JwtAuthenticationClientFilter());
    String address = "https://localhost:" + PORT + "/signedjwtauthz/bookstore/booksclaims";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    // Create the JWT Token
    JwtClaims claims = new JwtClaims();
    claims.setSubject("alice");
    claims.setIssuer("DoubleItSTSIssuer");
    claims.setIssuedAt(Instant.now().getEpochSecond());
    claims.setAudiences(toList(address));
    // The endpoint requires a role of "boss"
    claims.setProperty("role", "boss");
    claims.setProperty("http://claims/authentication", "password");
    JwtToken token = new JwtToken(claims);
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jwk");
    properties.put("rs.security.keystore.alias", "2011-04-29");
    properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt");
    properties.put("rs.security.signature.algorithm", "RS256");
    properties.put(JwtConstants.JWT_TOKEN, token);
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    Response response = client.post(new Book("book", 123L));
    assertEquals(response.getStatus(), 403);
}
Also used : JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) HashMap(java.util.HashMap) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) JwtAuthenticationClientFilter(org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)

Example 59 with Book

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

the class JWTAuthnAuthzTest method testAuthorization.

@org.junit.Test
public void testAuthorization() throws Exception {
    URL busFile = JWTAuthnAuthzTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    providers.add(new JwtAuthenticationClientFilter());
    String address = "https://localhost:" + PORT + "/signedjwtauthz/bookstore/books";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    // Create the JWT Token
    JwtClaims claims = new JwtClaims();
    claims.setSubject("alice");
    claims.setIssuer("DoubleItSTSIssuer");
    claims.setIssuedAt(Instant.now().getEpochSecond());
    claims.setAudiences(toList(address));
    // The endpoint requires a role of "boss"
    claims.setProperty("role", "boss");
    JwtToken token = new JwtToken(claims);
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jwk");
    properties.put("rs.security.keystore.alias", "2011-04-29");
    properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt");
    properties.put("rs.security.signature.algorithm", "RS256");
    properties.put(JwtConstants.JWT_TOKEN, token);
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    Response response = client.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 : JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) HashMap(java.util.HashMap) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) JwtAuthenticationClientFilter(org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)

Example 60 with Book

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

the class JWTAuthnAuthzTest method testAuthorizationRolesAllowedAnnotationGET.

@org.junit.Test
public void testAuthorizationRolesAllowedAnnotationGET() throws Exception {
    URL busFile = JWTAuthnAuthzTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    providers.add(new JwtAuthenticationClientFilter());
    String address = "https://localhost:" + PORT + "/signedjwtauthzannotations/bookstore/booksrolesallowed";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    // Create the JWT Token
    JwtClaims claims = new JwtClaims();
    claims.setSubject("alice");
    claims.setIssuer("DoubleItSTSIssuer");
    claims.setIssuedAt(Instant.now().getEpochSecond());
    claims.setAudiences(toList(address));
    // The endpoint requires a role of "boss"
    claims.setProperty("role", "boss");
    JwtToken token = new JwtToken(claims);
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jwk");
    properties.put("rs.security.keystore.alias", "2011-04-29");
    properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt");
    properties.put("rs.security.signature.algorithm", "RS256");
    properties.put(JwtConstants.JWT_TOKEN, token);
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    Response response = client.get();
    assertEquals(response.getStatus(), 200);
    Book returnedBook = response.readEntity(Book.class);
    assertEquals(returnedBook.getName(), "book");
    assertEquals(returnedBook.getId(), 123L);
}
Also used : JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) HashMap(java.util.HashMap) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) JwtAuthenticationClientFilter(org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)

Aggregations

Book (org.apache.cxf.systest.jaxrs.security.Book)164 WebClient (org.apache.cxf.jaxrs.client.WebClient)144 URL (java.net.URL)121 Response (javax.ws.rs.core.Response)120 HashMap (java.util.HashMap)96 ArrayList (java.util.ArrayList)77 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)76 Test (org.junit.Test)74 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)41 JwtAuthenticationClientFilter (org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)39 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)35 KeyStore (java.security.KeyStore)30 PrivateKey (java.security.PrivateKey)30 MessageSigner (org.apache.cxf.rs.security.httpsignature.MessageSigner)30 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)27 JwsWriterInterceptor (org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor)17 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)13