[picketlink-commits] Picketlink SVN: r1440 - in federation/trunk/picketlink-fed-core/src/test: resources and 1 other directories.

picketlink-commits at lists.jboss.org picketlink-commits at lists.jboss.org
Fri Feb 24 14:10:06 EST 2012


Author: anil.saldhana at jboss.com
Date: 2012-02-24 14:10:05 -0500 (Fri, 24 Feb 2012)
New Revision: 1440

Added:
   federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/util/XMLSignatureUtilUnitTestCase.java
   federation/trunk/picketlink-fed-core/src/test/resources/signatures/
   federation/trunk/picketlink-fed-core/src/test/resources/signatures/saml2assertion.xml
   federation/trunk/picketlink-fed-core/src/test/resources/signatures/wstRequestCollection.xml
Log:
testcase for XMLSignatureUtil

Added: federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/util/XMLSignatureUtilUnitTestCase.java
===================================================================
--- federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/util/XMLSignatureUtilUnitTestCase.java	                        (rev 0)
+++ federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/util/XMLSignatureUtilUnitTestCase.java	2012-02-24 19:10:05 UTC (rev 1440)
@@ -0,0 +1,116 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.picketlink.test.identity.federation.core.util;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.InputStream;
+import java.security.KeyPair;
+
+import javax.xml.crypto.dsig.DigestMethod;
+import javax.xml.crypto.dsig.SignatureMethod;
+
+import org.junit.Test;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
+import org.picketlink.identity.federation.core.util.KeyStoreUtil;
+import org.picketlink.identity.federation.core.util.XMLSignatureUtil;
+import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * Unit test the {@link XMLSignatureUtil}
+ * @author Anil Saldhana
+ * @since Feb 24, 2012
+ */
+public class XMLSignatureUtilUnitTestCase
+{
+   @Test
+   public void testSigningWSTRequestCollection() throws Exception
+   {
+      String fileName = "signatures/wstRequestCollection.xml";
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      InputStream is = tcl.getResourceAsStream(fileName);
+      if (is == null)
+         throw new RuntimeException("InputStream is null");
+
+      Document rstrDocument = DocumentUtil.getDocument(is);
+      assertNotNull(rstrDocument);
+
+      Node rst = rstrDocument.getElementsByTagNameNS(WSTrustConstants.BASE_NAMESPACE, "RequestedSecurityToken").item(0);
+      Element tokenElement = null;
+      NodeList childNodes = rst.getChildNodes();
+
+      int len = childNodes.getLength();
+      for (int i = 0; i < len; i++)
+      {
+         Node theNode = childNodes.item(i);
+         if (theNode instanceof Element)
+         {
+            tokenElement = (Element) theNode;
+            break;
+         }
+      }
+
+      String signatureMethod = SignatureMethod.RSA_SHA1;
+      KeyPair keyPair = KeyStoreUtil.generateKeyPair("RSA");
+
+      rstrDocument = XMLSignatureUtil.sign(rstrDocument, tokenElement, keyPair, DigestMethod.SHA1, signatureMethod, "#"
+            + tokenElement.getAttribute("ID"));
+
+      assertNotNull(rstrDocument);
+
+      System.out.println(DocumentUtil.asString(rstrDocument));
+
+      assertTrue(XMLSignatureUtil.validate(rstrDocument, keyPair.getPublic()));
+   }
+
+   @Test
+   public void testSAML2Assertion() throws Exception
+   {
+      String fileName = "signatures/saml2assertion.xml";
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      InputStream is = tcl.getResourceAsStream(fileName);
+      if (is == null)
+         throw new RuntimeException("InputStream is null");
+
+      Document rstrDocument = DocumentUtil.getDocument(is);
+      assertNotNull(rstrDocument);
+
+      String signatureMethod = SignatureMethod.RSA_SHA1;
+      KeyPair keyPair = KeyStoreUtil.generateKeyPair("RSA");
+
+      Element tokenElement = (Element) rstrDocument.getFirstChild();
+
+      rstrDocument = XMLSignatureUtil.sign(rstrDocument, tokenElement, keyPair, DigestMethod.SHA1, signatureMethod, "#"
+            + tokenElement.getAttribute("ID"));
+
+      assertNotNull(rstrDocument);
+
+      System.out.println(DocumentUtil.asString(rstrDocument));
+
+      assertTrue(XMLSignatureUtil.validate(rstrDocument, keyPair.getPublic()));
+   }
+}
\ No newline at end of file

Added: federation/trunk/picketlink-fed-core/src/test/resources/signatures/saml2assertion.xml
===================================================================
--- federation/trunk/picketlink-fed-core/src/test/resources/signatures/saml2assertion.xml	                        (rev 0)
+++ federation/trunk/picketlink-fed-core/src/test/resources/signatures/saml2assertion.xml	2012-02-24 19:10:05 UTC (rev 1440)
@@ -0,0 +1,17 @@
+<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"
+	AssertionID="ID_4756863b-eb25-4572-935f-af4ccc8a34ac" IssueInstant="2012-02-24T15:57:15.975Z"
+	Issuer="PicketLinkSTS" MajorVersion="1" MinorVersion="1">
+	<saml:Conditions NotBefore="2012-02-24T15:57:15.975Z"
+		NotOnOrAfter="2012-02-24T17:57:15.975Z" />
+	<saml:AuthenticationStatement
+		AuthenticationInstant="2012-02-24T15:57:15.975Z" AuthenticationMethod="urn:picketlink:auth">
+		<saml:Subject>
+			<saml:NameIdentifier
+				Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">admin</saml:NameIdentifier>
+			<saml:SubjectConfirmation>
+				<saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer
+				</saml:ConfirmationMethod>
+			</saml:SubjectConfirmation>
+		</saml:Subject>
+	</saml:AuthenticationStatement>
+</saml:Assertion>
\ No newline at end of file

Added: federation/trunk/picketlink-fed-core/src/test/resources/signatures/wstRequestCollection.xml
===================================================================
--- federation/trunk/picketlink-fed-core/src/test/resources/signatures/wstRequestCollection.xml	                        (rev 0)
+++ federation/trunk/picketlink-fed-core/src/test/resources/signatures/wstRequestCollection.xml	2012-02-24 19:10:05 UTC (rev 1440)
@@ -0,0 +1,47 @@
+<wst:RequestSecurityTokenResponseCollection
+	xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
+	<wst:RequestSecurityTokenResponse
+		Context="default-context">
+		<wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1
+		</wst:TokenType>
+		<wst:Lifetime>
+			<wsu:Created
+				xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2012-02-24T15:57:15.975Z</wsu:Created>
+			<wsu:Expires
+				xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2012-02-24T17:57:15.975Z</wsu:Expires>
+		</wst:Lifetime>
+		<wst:KeySize>128</wst:KeySize>
+		<wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer
+		</wst:KeyType>
+		<wst:RequestedSecurityToken>
+		   <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"
+				AssertionID="ID_4756863b-eb25-4572-935f-af4ccc8a34ac" IssueInstant="2012-02-24T15:57:15.975Z"
+				Issuer="PicketLinkSTS" MajorVersion="1" MinorVersion="1">
+				<saml:Conditions NotBefore="2012-02-24T15:57:15.975Z"
+					NotOnOrAfter="2012-02-24T17:57:15.975Z" />
+				<saml:AuthenticationStatement
+					AuthenticationInstant="2012-02-24T15:57:15.975Z"
+					AuthenticationMethod="urn:picketlink:auth">
+					<saml:Subject>
+						<saml:NameIdentifier
+							Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">admin</saml:NameIdentifier>
+						<saml:SubjectConfirmation>
+							<saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer
+							</saml:ConfirmationMethod>
+						</saml:SubjectConfirmation>
+					</saml:Subject>
+				</saml:AuthenticationStatement>
+			</saml:Assertion>
+		</wst:RequestedSecurityToken>
+		<wst:RequestedAttachedReference>
+			<wsse:SecurityTokenReference
+				xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
+				xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"
+				wsse11:TokenType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1">
+				<wsse:KeyIdentifier
+					ValueType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID">#ID_4756863b-eb25-4572-935f-af4ccc8a34ac
+				</wsse:KeyIdentifier>
+			</wsse:SecurityTokenReference>
+		</wst:RequestedAttachedReference>
+	</wst:RequestSecurityTokenResponse>
+</wst:RequestSecurityTokenResponseCollection>
\ No newline at end of file



More information about the picketlink-commits mailing list