You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by co...@apache.org on 2011/03/10 16:23:18 UTC
svn commit: r1080247 - in /webservices/wss4j/trunk/src:
main/java/org/apache/ws/security/saml/SAMLUtil.java
test/java/org/apache/ws/security/saml/SamlTokenTest.java
Author: coheigea
Date: Thu Mar 10 15:23:17 2011
New Revision: 1080247
URL: http://svn.apache.org/viewvc?rev=1080247&view=rev
Log:
Added example of a custom SAML Token validator.
Modified:
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/SAMLUtil.java
webservices/wss4j/trunk/src/test/java/org/apache/ws/security/saml/SamlTokenTest.java
Modified: webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/SAMLUtil.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/SAMLUtil.java?rev=1080247&r1=1080246&r2=1080247&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/SAMLUtil.java (original)
+++ webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/SAMLUtil.java Thu Mar 10 15:23:17 2011
@@ -191,10 +191,9 @@ public class SAMLUtil {
(org.opensaml.saml1.core.AuthenticationStatement) stmt;
samlSubject = authStmt.getSubject();
} else {
- throw new WSSecurityException(
- WSSecurityException.FAILURE, "invalidSAMLsecurity",
- new Object[] {"cannot get certificate or key"}
- );
+ org.opensaml.saml1.core.AuthorizationDecisionStatement authzStmt =
+ (org.opensaml.saml1.core.AuthorizationDecisionStatement)stmt;
+ samlSubject = authzStmt.getSubject();
}
if (samlSubject == null) {
Modified: webservices/wss4j/trunk/src/test/java/org/apache/ws/security/saml/SamlTokenTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/test/java/org/apache/ws/security/saml/SamlTokenTest.java?rev=1080247&r1=1080246&r2=1080247&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/test/java/org/apache/ws/security/saml/SamlTokenTest.java (original)
+++ webservices/wss4j/trunk/src/test/java/org/apache/ws/security/saml/SamlTokenTest.java Thu Mar 10 15:23:17 2011
@@ -26,6 +26,7 @@ import org.apache.ws.security.WSConstant
import org.apache.ws.security.WSSConfig;
import org.apache.ws.security.WSSecurityEngine;
import org.apache.ws.security.WSSecurityEngineResult;
+import org.apache.ws.security.WSSecurityException;
import org.apache.ws.security.common.CustomHandler;
import org.apache.ws.security.common.SAML1CallbackHandler;
import org.apache.ws.security.common.SAML2CallbackHandler;
@@ -36,6 +37,8 @@ import org.apache.ws.security.message.WS
import org.apache.ws.security.message.WSSecSAMLToken;
import org.apache.ws.security.saml.ext.AssertionWrapper;
import org.apache.ws.security.util.WSSecurityUtil;
+import org.apache.ws.security.validate.Credential;
+import org.apache.ws.security.validate.SamlAssertionValidator;
import org.w3c.dom.Document;
@@ -50,6 +53,13 @@ public class SamlTokenTest extends org.j
private static final Log LOG = LogFactory.getLog(SamlTokenTest.class);
private WSSecurityEngine secEngine = new WSSecurityEngine();
+ public SamlTokenTest() {
+ WSSConfig config = WSSConfig.getNewInstance();
+ config.setValidator(WSSecurityEngine.SAML_TOKEN, new CustomSamlAssertionValidator());
+ config.setValidator(WSSecurityEngine.SAML2_TOKEN, new CustomSamlAssertionValidator());
+ secEngine.setWssConfig(config);
+ }
+
/**
* Test that creates, sends and processes an unsigned SAML 1.1 authentication assertion.
*/
@@ -345,6 +355,81 @@ public class SamlTokenTest extends org.j
}
/**
+ * Test that creates, sends and processes an unsigned SAML 1.1 authentication assertion.
+ * The issuer is different from what the custom Validator is expecting, so it throws an
+ * exception.
+ */
+ @org.junit.Test
+ public void testSAML1AuthnBadIssuerAssertion() throws Exception {
+ SAML1CallbackHandler callbackHandler = new SAML1CallbackHandler();
+ callbackHandler.setStatement(SAML1CallbackHandler.Statement.AUTHN);
+ SAMLIssuer saml = new SAMLIssuerImpl();
+ saml.setIssuerName("www.example2.com");
+ saml.setCallbackHandler(callbackHandler);
+ AssertionWrapper assertion = saml.newAssertion();
+
+ WSSecSAMLToken wsSign = new WSSecSAMLToken();
+
+ Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document unsignedDoc = wsSign.build(doc, assertion, secHeader);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("SAML 1.1 Authn Assertion (sender vouches):");
+ String outputString =
+ org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(unsignedDoc);
+ LOG.debug(outputString);
+ }
+
+ try {
+ verify(unsignedDoc);
+ fail("Failure expected on a bad issuer");
+ } catch (WSSecurityException ex) {
+ // expected
+ }
+ }
+
+ /**
+ * Test that creates, sends and processes an unsigned SAML 2 authentication assertion.
+ * The issuer is different from what the custom Validator is expecting, so it throws an
+ * exception.
+ */
+ @org.junit.Test
+ public void testSAML2AuthnBadIssuerAssertion() throws Exception {
+ SAML2CallbackHandler callbackHandler = new SAML2CallbackHandler();
+ callbackHandler.setStatement(SAML2CallbackHandler.Statement.AUTHN);
+ SAMLIssuer saml = new SAMLIssuerImpl();
+ saml.setIssuerName("www.example2.com");
+ saml.setSamlVersion("2.0");
+ saml.setCallbackHandler(callbackHandler);
+ AssertionWrapper assertion = saml.newAssertion();
+
+ WSSecSAMLToken wsSign = new WSSecSAMLToken();
+
+ Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
+ WSSecHeader secHeader = new WSSecHeader();
+ secHeader.insertSecurityHeader(doc);
+
+ Document unsignedDoc = wsSign.build(doc, assertion, secHeader);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("SAML 2 Authn Assertion (sender vouches):");
+ String outputString =
+ org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(unsignedDoc);
+ LOG.debug(outputString);
+ }
+
+ try {
+ verify(unsignedDoc);
+ fail("Failure expected on a bad issuer");
+ } catch (WSSecurityException ex) {
+ // expected
+ }
+ }
+
+ /**
* Verifies the soap envelope
* <p/>
*
@@ -359,5 +444,64 @@ public class SamlTokenTest extends org.j
assertTrue(outputString.indexOf("counter_port_type") > 0 ? true : false);
return results;
}
+
+ public static class CustomSamlAssertionValidator extends SamlAssertionValidator {
+
+ @Override
+ public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
+ Credential returnedCredential = super.validate(credential, data);
+
+ //
+ // Do some custom validation on the assertion
+ //
+ AssertionWrapper assertion = credential.getAssertion();
+ if (!"www.example.com".equals(assertion.getIssuerString())) {
+ throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity");
+ }
+ if (assertion.getSaml1() != null) {
+ // Get the SAML subject and validate it
+ org.opensaml.saml1.core.Assertion saml1Assertion = assertion.getSaml1();
+ org.opensaml.saml1.core.Subject samlSubject = null;
+ for (org.opensaml.saml1.core.Statement stmt : saml1Assertion.getStatements()) {
+ if (stmt instanceof org.opensaml.saml1.core.AttributeStatement) {
+ org.opensaml.saml1.core.AttributeStatement attrStmt =
+ (org.opensaml.saml1.core.AttributeStatement) stmt;
+ samlSubject = attrStmt.getSubject();
+ break;
+ } else if (stmt instanceof org.opensaml.saml1.core.AuthenticationStatement) {
+ org.opensaml.saml1.core.AuthenticationStatement authStmt =
+ (org.opensaml.saml1.core.AuthenticationStatement) stmt;
+ samlSubject = authStmt.getSubject();
+ break;
+ } else {
+ org.opensaml.saml1.core.AuthorizationDecisionStatement authzStmt =
+ (org.opensaml.saml1.core.AuthorizationDecisionStatement)stmt;
+ samlSubject = authzStmt.getSubject();
+ }
+ }
+
+ if (samlSubject == null) {
+ throw new WSSecurityException(
+ WSSecurityException.FAILURE, "invalidSAMLToken",
+ new Object[] {"for Signature (no Subject)"}
+ );
+ }
+ String nameIdentifier = samlSubject.getNameIdentifier().getNameIdentifier();
+ if (nameIdentifier == null || !nameIdentifier.contains("uid=joe")) {
+ throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity");
+ }
+ } else {
+ org.opensaml.saml2.core.Assertion saml2Assertion = assertion.getSaml2();
+ org.opensaml.saml2.core.Subject subject = saml2Assertion.getSubject();
+ String nameIdentifier = subject.getNameID().getValue();
+ if (nameIdentifier == null || !nameIdentifier.contains("uid=joe")) {
+ throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity");
+ }
+ }
+
+ return returnedCredential;
+ }
+
+ }
}