diff --git a/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/api/BaseResponse.java b/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/api/BaseResponse.java index 2d59d47ff52..9672123aec8 100644 --- a/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/api/BaseResponse.java +++ b/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/api/BaseResponse.java @@ -52,8 +52,8 @@ public BaseResponse() { this.status = StatusType.SUCCESS.name(); } - public BaseResponse(final String message) { - this.status = StatusType.ERROR.name(); + public BaseResponse(final StatusType status, final String message) { + this.status = status.name(); this.message = message; } diff --git a/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/impl/RestExceptionMapper.java b/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/impl/RestExceptionMapper.java index 6a703542f9c..0e4d1efb402 100644 --- a/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/impl/RestExceptionMapper.java +++ b/services/sts/sts-rest/src/main/java/org/apache/cxf/sts/rest/impl/RestExceptionMapper.java @@ -38,7 +38,7 @@ public class RestExceptionMapper implements ExceptionMapper { public Response toResponse(Exception exception) { return Response .status(Status.INTERNAL_SERVER_ERROR) - .entity(new BaseResponse(exception.getMessage())) + .entity(new BaseResponse(BaseResponse.StatusType.ERROR, exception.getMessage())) .type(getResponseType()) .build(); } diff --git a/services/sts/systests/advanced/pom.xml b/services/sts/systests/advanced/pom.xml index 7e125154bbe..f2df102c00e 100644 --- a/services/sts/systests/advanced/pom.xml +++ b/services/sts/systests/advanced/pom.xml @@ -47,6 +47,12 @@ ${project.version} test + + org.apache.cxf.services.sts + cxf-services-sts-rest + ${project.version} + test + org.apache.cxf cxf-rt-transports-http-jetty diff --git a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomClaimsHandler.java b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomClaimsHandler.java index dc5ec324982..8d6340e90b2 100644 --- a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomClaimsHandler.java +++ b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomClaimsHandler.java @@ -18,7 +18,7 @@ */ package org.apache.cxf.systest.sts.custom; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.w3c.dom.Element; @@ -31,15 +31,17 @@ import org.apache.cxf.sts.claims.ProcessedClaimCollection; import org.apache.wss4j.common.util.XMLUtils; +import static org.apache.cxf.sts.STSConstants.IDT_NS_05_05; + /** * A custom ClaimsHandler implementation for use in the tests. */ public class CustomClaimsHandler implements ClaimsHandler { public static final String ROLE = - "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"; + IDT_NS_05_05 + "/claims/role"; public static final String GIVEN_NAME = - "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"; + IDT_NS_05_05 + "/claims/givenname"; public static final String LANGUAGE = "http://schemas.mycompany.com/claims/language"; @@ -47,7 +49,6 @@ public ProcessedClaimCollection retrieveClaimValues( ClaimCollection claims, ClaimsParameters parameters) { if (claims != null && !claims.isEmpty()) { - ProcessedClaimCollection claimCollection = new ProcessedClaimCollection(); List customContent = parameters.getTokenRequirements().getCustomContent(); boolean foundContent = false; if (customContent != null) { @@ -66,12 +67,13 @@ public ProcessedClaimCollection retrieveClaimValues( } } + ProcessedClaimCollection claimCollection = new ProcessedClaimCollection(); for (Claim requestClaim : claims) { ProcessedClaim claim = new ProcessedClaim(); claim.setClaimType(requestClaim.getClaimType()); claim.setIssuer("Test Issuer"); claim.setOriginalIssuer("Original Issuer"); - if (foundContent) { + if (foundContent || "custom-realm".equals(parameters.getRealm())) { if (ROLE.equals(requestClaim.getClaimType())) { claim.addValue("admin-user"); } else if (GIVEN_NAME.equals(requestClaim.getClaimType())) { @@ -88,11 +90,10 @@ public ProcessedClaimCollection retrieveClaimValues( } public List getSupportedClaimTypes() { - List list = new ArrayList<>(); - list.add(ROLE); - list.add(GIVEN_NAME); - list.add(LANGUAGE); - return list; + return Arrays.asList( + ROLE, + GIVEN_NAME, + LANGUAGE); } } diff --git a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomParameterTest.java b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomParameterTest.java index d8dcfc17b9f..8ea2ed4a27f 100644 --- a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomParameterTest.java +++ b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/CustomParameterTest.java @@ -19,33 +19,39 @@ package org.apache.cxf.systest.sts.custom; import java.net.URL; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.security.auth.callback.CallbackHandler; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.MediaType; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; -import javax.xml.transform.dom.DOMSource; import javax.xml.ws.BindingProvider; import javax.xml.ws.Service; +import org.w3c.dom.Document; import org.w3c.dom.Element; import org.apache.cxf.Bus; import org.apache.cxf.BusFactory; import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.helpers.DOMUtils; +import org.apache.cxf.jaxrs.client.JAXRSClientFactory; import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.rt.security.SecurityConstants; -import org.apache.cxf.staxutils.W3CDOMStreamWriter; +import org.apache.cxf.sts.rest.api.GetTokenRequest; +import org.apache.cxf.sts.rest.api.RealmSecurityTokenService; import org.apache.cxf.systest.sts.common.SecurityTestUtil; import org.apache.cxf.systest.sts.common.TokenTestUtils; import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; +import org.apache.cxf.ws.security.sts.provider.model.ClaimsType; +import org.apache.cxf.ws.security.sts.provider.model.ObjectFactory; import org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType; +import org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType; import org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType; import org.apache.cxf.ws.security.trust.STSClient; -import org.apache.cxf.ws.security.trust.STSUtils; import org.apache.wss4j.common.crypto.Crypto; import org.apache.wss4j.common.crypto.CryptoFactory; import org.apache.wss4j.common.saml.SamlAssertionWrapper; @@ -59,6 +65,8 @@ import org.junit.BeforeClass; +import static org.apache.cxf.sts.STSConstants.IDT_NS_05_05; +import static org.apache.cxf.sts.STSConstants.WST_NS_05_12; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -296,66 +304,69 @@ public void testCustomParameterInRSTClaimsHandler2() throws Exception { @org.junit.Test public void testCustomParameterToRESTInterface() throws Exception { - SpringBusFactory bf = new SpringBusFactory(); - URL busFile = CustomParameterTest.class.getResource("cxf-client.xml"); - - Bus bus = bf.createBus(busFile.toString()); - BusFactory.setDefaultBus(bus); - BusFactory.setThreadDefaultBus(bus); - String address = "https://localhost:" + STSPORT + "/SecurityTokenServiceREST/token"; - WebClient client = WebClient.create(address, busFile.toString()); - - client.type("application/xml").accept("application/xml"); - - // Create RequestSecurityToken - W3CDOMStreamWriter writer = new W3CDOMStreamWriter(); - String namespace = STSUtils.WST_NS_05_12; - writer.writeStartElement("wst", "RequestSecurityToken", namespace); - writer.writeNamespace("wst", namespace); + WebClient client = WebClient.create(address, getClass().getResource("cxf-client.xml").toString()) + .type(MediaType.APPLICATION_XML) + .accept(MediaType.APPLICATION_XML); + + ObjectFactory of = new ObjectFactory(); + RequestSecurityTokenType request = of.createRequestSecurityTokenType(); + request.getAny().add(of.createRequestType(WST_NS_05_12 + "/Issue")); + request.getAny().add(of.createTokenType(SAML2_TOKEN_TYPE)); + + ClaimsType claims = of.createClaimsType(); + claims.setDialect(IDT_NS_05_05); + Element claimsType = DOMUtils.getEmptyDocument().createElementNS(IDT_NS_05_05, "ClaimType"); + claimsType.setAttribute("Uri", IDT_NS_05_05 + "/claims/role"); + claims.getAny().add(claimsType); + request.getAny().add(of.createClaims(claims)); - writer.writeStartElement("wst", "RequestType", namespace); - writer.writeCharacters(namespace + "/Issue"); - writer.writeEndElement(); + // Add custom content to the RST + Element realm = DOMUtils.getEmptyDocument().createElementNS("http://cxf.apache.org/custom", "realm"); + realm.setTextContent("custom-realm"); + request.getAny().add(realm); - writer.writeStartElement("wst", "TokenType", namespace); - writer.writeCharacters(SAML2_TOKEN_TYPE); - writer.writeEndElement(); + RequestSecurityTokenResponseType securityResponse = + client.post( + of.createRequestSecurityToken(request), + RequestSecurityTokenResponseType.class); - writer.writeStartElement("wst", "Claims", namespace); - writer.writeAttribute("Dialect", "http://schemas.xmlsoap.org/ws/2005/05/identity"); - writer.writeStartElement("ic", "ClaimType", "http://schemas.xmlsoap.org/ws/2005/05/identity"); - writer.writeAttribute("Uri", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role"); - writer.writeEndElement(); - writer.writeEndElement(); + Element assertion = validateSAMLSecurityTokenResponse(securityResponse, true); + assertTrue(DOM2Writer.nodeToString(assertion).contains("admin-user")); - // Add custom content to the RST - writer.writeStartElement("", "realm", "http://cxf.apache.org/custom"); - writer.writeCharacters("custom-realm"); - writer.writeEndElement(); + client.close(); + } - writer.writeEndElement(); + @org.junit.Test + public void testCustomParameterToRealmRestInterface() throws Exception { - Response response = client.post(new DOMSource(writer.getDocument().getDocumentElement())); + GetTokenRequest getTokenRequest = new GetTokenRequest(); + getTokenRequest.setTokenType(SAML2_TOKEN_TYPE); + getTokenRequest.setClaims(Collections.singletonList("role")); - RequestSecurityTokenResponseType securityResponse = - response.readEntity(RequestSecurityTokenResponseType.class); + final RealmSecurityTokenService client = JAXRSClientFactory.create( + "https://localhost:" + STSPORT + "/RealmSecurityTokenService", + RealmSecurityTokenService.class, + getClass().getResource("cxf-client.xml").toString()); + Document assertionDoc = + client.getToken("custom-realm", getTokenRequest) + .readEntity(Document.class); - Element assertion = validateSAMLSecurityTokenResponse(securityResponse, true); + Element assertion = validateSAMLToken(assertionDoc.getDocumentElement(), true); assertTrue(DOM2Writer.nodeToString(assertion).contains("admin-user")); - - bus.shutdown(true); } - private Element validateSAMLSecurityTokenResponse( + private static Element validateSAMLSecurityTokenResponse( RequestSecurityTokenResponseType securityResponse, boolean saml2 ) throws Exception { RequestedSecurityTokenType requestedSecurityToken = getRequestedSecurityToken(securityResponse); assertNotNull(requestedSecurityToken); - // Process the token - List results = - processToken((Element)requestedSecurityToken.getAny()); + return validateSAMLToken((Element)requestedSecurityToken.getAny(), saml2); + } + + private static Element validateSAMLToken(Element assertionElement, boolean saml2) throws Exception { + List results = processToken(assertionElement); assertTrue(results != null && results.size() == 1); SamlAssertionWrapper assertion = @@ -371,7 +382,8 @@ private Element validateSAMLSecurityTokenResponse( return (Element)results.get(0).get(WSSecurityEngineResult.TAG_TOKEN_ELEMENT); } - private RequestedSecurityTokenType getRequestedSecurityToken(RequestSecurityTokenResponseType securityResponse) { + private static RequestedSecurityTokenType getRequestedSecurityToken( + RequestSecurityTokenResponseType securityResponse) { for (Object obj : securityResponse.getAny()) { if (obj instanceof JAXBElement) { JAXBElement jaxbElement = (JAXBElement)obj; @@ -383,7 +395,7 @@ private RequestedSecurityTokenType getRequestedSecurityToken(RequestSecurityToke return null; } - private List processToken(Element assertionElement) + private static List processToken(Element assertionElement) throws Exception { RequestData requestData = new RequestData(); requestData.setDisableBSPEnforcement(true); diff --git a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/STSServer.java b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/STSServer.java index dbf4f21aa76..033deeb8cf3 100644 --- a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/STSServer.java +++ b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/STSServer.java @@ -18,30 +18,13 @@ */ package org.apache.cxf.systest.sts.custom; -import java.net.URL; - -import org.apache.cxf.Bus; -import org.apache.cxf.BusFactory; import org.apache.cxf.bus.spring.SpringBusFactory; import org.apache.cxf.testutil.common.AbstractBusTestServerBase; public class STSServer extends AbstractBusTestServerBase { - public STSServer() { - - } - protected void run() { - URL busFile = STSServer.class.getResource("cxf-sts.xml"); - Bus busLocal = new SpringBusFactory().createBus(busFile); - BusFactory.setDefaultBus(busLocal); - setBus(busLocal); - - try { - new STSServer(); - } catch (Exception e) { - e.printStackTrace(); - } + setBus(new SpringBusFactory().createBus(STSServer.class.getResource("cxf-sts.xml"))); } public static void main(String[] args) { diff --git a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/Server.java b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/Server.java index 860a306fcda..9f20ea32746 100644 --- a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/Server.java +++ b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/custom/Server.java @@ -18,29 +18,12 @@ */ package org.apache.cxf.systest.sts.custom; -import java.net.URL; - -import org.apache.cxf.Bus; -import org.apache.cxf.BusFactory; import org.apache.cxf.bus.spring.SpringBusFactory; import org.apache.cxf.testutil.common.AbstractBusTestServerBase; public class Server extends AbstractBusTestServerBase { - public Server() { - - } - protected void run() { - URL busFile = Server.class.getResource("cxf-service.xml"); - Bus busLocal = new SpringBusFactory().createBus(busFile); - BusFactory.setDefaultBus(busLocal); - setBus(busLocal); - - try { - new Server(); - } catch (Exception e) { - e.printStackTrace(); - } + setBus(new SpringBusFactory().createBus(Server.class.getResource("cxf-service.xml"))); } } diff --git a/services/sts/systests/advanced/src/test/resources/org/apache/cxf/systest/sts/custom/cxf-sts-common.xml b/services/sts/systests/advanced/src/test/resources/org/apache/cxf/systest/sts/custom/cxf-sts-common.xml index 028dd227ce5..9403d15020d 100644 --- a/services/sts/systests/advanced/src/test/resources/org/apache/cxf/systest/sts/custom/cxf-sts-common.xml +++ b/services/sts/systests/advanced/src/test/resources/org/apache/cxf/systest/sts/custom/cxf-sts-common.xml @@ -73,12 +73,26 @@ - + + + + + + + + + + + + + + + - + - + xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration" + xmlns:sec="http://cxf.apache.org/configuration/security" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd + http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd + http://cxf.apache.org/transports/http-jetty/configuration http://cxf.apache.org/schemas/configuration/http-jetty.xsd + http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd + "> + - + @@ -41,9 +46,9 @@ - + - + @@ -58,7 +63,27 @@ - + + + + + + + + + + + + + + + + + + + diff --git a/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/rest/STSRealmRestTest.java b/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/rest/STSRealmRestTest.java index 3bcf871c6d9..b80fa731794 100644 --- a/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/rest/STSRealmRestTest.java +++ b/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/rest/STSRealmRestTest.java @@ -83,7 +83,7 @@ */ public class STSRealmRestTest extends AbstractBusClientServerTestBase { - static final String STSPORT = allocatePort(STSRealmRestServer.class); + private static final String STSPORT = allocatePort(STSRealmRestServer.class); private static final String REALM = "realmA"; @@ -310,9 +310,7 @@ public void testIssueSAML2TokenViaPOST() throws Exception { GetTokenRequest getTokenRequest = new GetTokenRequest(); getTokenRequest.setTokenType(SAML2_TOKEN_TYPE); - RealmSecurityTokenService client = client(); - WebClient.client(client).accept(MediaType.APPLICATION_XML); - Document assertionDoc = client.getToken(REALM, getTokenRequest) + Document assertionDoc = client().getToken(REALM, getTokenRequest) .readEntity(Document.class); SamlAssertionWrapper assertion = validateSAMLToken(assertionDoc); @@ -325,9 +323,7 @@ public void testIssueSAML1TokenViaPOST() throws Exception { GetTokenRequest getTokenRequest = new GetTokenRequest(); getTokenRequest.setTokenType(SAML1_TOKEN_TYPE); - RealmSecurityTokenService client = client(); - WebClient.client(client).accept(MediaType.APPLICATION_XML); - Document assertionDoc = client.getToken(REALM, getTokenRequest) + Document assertionDoc = client().getToken(REALM, getTokenRequest) .readEntity(Document.class); SamlAssertionWrapper assertion = validateSAMLToken(assertionDoc); @@ -424,9 +420,7 @@ public void testIssueJWTTokenViaPOST() throws Exception { GetTokenRequest getTokenRequest = new GetTokenRequest(); getTokenRequest.setTokenType(JWT_TOKEN_TYPE); - RealmSecurityTokenService client = client(); - WebClient.client(client).accept(MediaType.APPLICATION_XML); - Document assertionDoc = client.getToken(REALM, getTokenRequest) + Document assertionDoc = client().getToken(REALM, getTokenRequest) .readEntity(Document.class); // Process the token