Updating ActAs unit tests in the STS code # Conflicts: # services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderActAsTest.java
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4f1867ee Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4f1867ee Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4f1867ee Branch: refs/heads/3.0.x-fixes Commit: 4f1867eeeb13db3d38ec8e2171e4e9fee1602230 Parents: 6eed66b Author: Colm O hEigeartaigh <cohei...@apache.org> Authored: Thu Oct 20 12:20:00 2016 +0100 Committer: Colm O hEigeartaigh <cohei...@apache.org> Committed: Thu Oct 20 12:26:15 2016 +0100 ---------------------------------------------------------------------- .../token/provider/SAMLProviderActAsTest.java | 65 +++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/4f1867ee/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderActAsTest.java ---------------------------------------------------------------------- diff --git a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderActAsTest.java b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderActAsTest.java index 48a4263..d749e9c 100644 --- a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderActAsTest.java +++ b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderActAsTest.java @@ -25,7 +25,11 @@ import java.util.Properties; import javax.xml.bind.JAXBElement; import org.w3c.dom.Element; +<<<<<<< HEAD import org.apache.cxf.jaxws.context.WebServiceContextImpl; +======= + +>>>>>>> 0529afb... Updating ActAs unit tests in the STS code import org.apache.cxf.jaxws.context.WrappedMessageContext; import org.apache.cxf.message.MessageImpl; import org.apache.cxf.sts.QNameConstants; @@ -44,9 +48,13 @@ import org.apache.wss4j.common.crypto.Crypto; import org.apache.wss4j.common.crypto.CryptoFactory; import org.apache.wss4j.common.ext.WSSecurityException; import org.apache.wss4j.common.principal.CustomTokenPrincipal; +import org.apache.wss4j.common.saml.SamlAssertionWrapper; import org.apache.wss4j.common.util.DOM2Writer; import org.apache.wss4j.dom.WSConstants; +import org.junit.Assert; +import org.opensaml.core.xml.XMLObject; + /** * Some unit tests for creating SAML Tokens with an ActAs element. */ @@ -81,12 +89,38 @@ public class SAMLProviderActAsTest extends org.junit.Assert { assertTrue(providerResponse != null); assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null); +<<<<<<< HEAD Element token = providerResponse.getToken(); String tokenString = DOM2Writer.nodeToString(token); assertTrue(tokenString.contains(providerResponse.getTokenId())); assertTrue(tokenString.contains("AttributeStatement")); assertTrue(tokenString.contains("ActAs")); assertTrue(tokenString.contains("bob")); +======= + // Verify the token + Element token = (Element)providerResponse.getToken(); + SamlAssertionWrapper assertion = new SamlAssertionWrapper(token); + Assert.assertEquals("technical-user", assertion.getSubjectName()); + + boolean foundActAsAttribute = false; + for (org.opensaml.saml.saml1.core.AttributeStatement attributeStatement + : assertion.getSaml1().getAttributeStatements()) { + for (org.opensaml.saml.saml1.core.Attribute attribute : attributeStatement.getAttributes()) { + if ("ActAs".equals(attribute.getAttributeName())) { + for (XMLObject attributeValue : attribute.getAttributeValues()) { + Element attributeValueElement = attributeValue.getDOM(); + String text = attributeValueElement.getTextContent(); + if (text.contains("bob")) { + foundActAsAttribute = true; + break; + } + } + } + } + } + + Assert.assertTrue(foundActAsAttribute); +>>>>>>> 0529afb... Updating ActAs unit tests in the STS code } /** @@ -96,7 +130,7 @@ public class SAMLProviderActAsTest extends org.junit.Assert { public void testDefaultSaml2ActAsAssertion() throws Exception { TokenProvider samlTokenProvider = new SAMLTokenProvider(); - String user = "alice"; + String user = "bob"; Element saml1Assertion = getSAMLAssertion(); TokenProviderParameters providerParameters = @@ -112,11 +146,37 @@ public class SAMLProviderActAsTest extends org.junit.Assert { assertTrue(providerResponse != null); assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null); +<<<<<<< HEAD Element token = providerResponse.getToken(); String tokenString = DOM2Writer.nodeToString(token); assertTrue(tokenString.contains(providerResponse.getTokenId())); assertTrue(tokenString.contains("AttributeStatement")); assertTrue(tokenString.contains("ActAs")); +======= + // Verify the token + Element token = (Element)providerResponse.getToken(); + SamlAssertionWrapper assertion = new SamlAssertionWrapper(token); + Assert.assertEquals("technical-user", assertion.getSubjectName()); + + boolean foundActAsAttribute = false; + for (org.opensaml.saml.saml2.core.AttributeStatement attributeStatement + : assertion.getSaml2().getAttributeStatements()) { + for (org.opensaml.saml.saml2.core.Attribute attribute : attributeStatement.getAttributes()) { + if ("ActAs".equals(attribute.getName())) { + for (XMLObject attributeValue : attribute.getAttributeValues()) { + Element attributeValueElement = attributeValue.getDOM(); + String text = attributeValueElement.getTextContent(); + if (text.contains("bob")) { + foundActAsAttribute = true; + break; + } + } + } + } + } + + Assert.assertTrue(foundActAsAttribute); +>>>>>>> 0529afb... Updating ActAs unit tests in the STS code } /** @@ -179,6 +239,7 @@ public class SAMLProviderActAsTest extends org.junit.Assert { TokenProvider samlTokenProvider = new SAMLTokenProvider(); TokenProviderParameters providerParameters = createProviderParameters(WSConstants.WSS_SAML_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, null); + providerParameters.setPrincipal(new CustomTokenPrincipal("bob")); assertTrue(samlTokenProvider.canHandleToken(WSConstants.WSS_SAML_TOKEN_TYPE)); TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters); assertTrue(providerResponse != null); @@ -207,7 +268,7 @@ public class SAMLProviderActAsTest extends org.junit.Assert { keyRequirements.setKeyType(keyType); parameters.setKeyRequirements(keyRequirements); - parameters.setPrincipal(new CustomTokenPrincipal("alice")); + parameters.setPrincipal(new CustomTokenPrincipal("technical-user")); // Mock up message context MessageImpl msg = new MessageImpl(); WrappedMessageContext msgCtx = new WrappedMessageContext(msg);