Java tutorial
/* * Licensed to the University Corporation for Advanced Internet Development, * Inc. (UCAID) under one or more contributor license agreements. See the * NOTICE file distributed with this work for additional information regarding * copyright ownership. The UCAID licenses this file to You under the Apache * License, Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.opensaml.soap.wssecurity.impl; import org.testng.annotations.Test; import org.testng.Assert; import java.util.List; import org.custommonkey.xmlunit.Diff; import net.shibboleth.utilities.java.support.xml.XMLAssertTestNG; import org.joda.time.DateTime; import org.opensaml.core.xml.XMLObject; import org.opensaml.core.xml.io.Marshaller; import org.opensaml.soap.WSBaseTestCase; import org.opensaml.soap.wssecurity.BinarySecurityToken; import org.opensaml.soap.wssecurity.Created; import org.opensaml.soap.wssecurity.Embedded; import org.opensaml.soap.wssecurity.EncryptedHeader; import org.opensaml.soap.wssecurity.Expires; import org.opensaml.soap.wssecurity.Iteration; import org.opensaml.soap.wssecurity.Nonce; import org.opensaml.soap.wssecurity.Password; import org.opensaml.soap.wssecurity.Reference; import org.opensaml.soap.wssecurity.Salt; import org.opensaml.soap.wssecurity.Timestamp; import org.opensaml.soap.wssecurity.Username; import org.opensaml.soap.wssecurity.UsernameToken; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * WSSecurityObjectsTestCase is the base test case for the WS-Security * objects. * */ public class WSSecurityObjectsTestCase extends WSBaseTestCase { public Logger log = LoggerFactory.getLogger(WSSecurityObjectsTestCase.class); protected void unmarshallAndMarshall(String filename) throws Exception { // TODO implementation } @Test public void testBinarySecurityToken() throws Exception { BinarySecurityToken token = buildXMLObject(BinarySecurityToken.ELEMENT_NAME); token.setWSUId("BinarySecurityToken-" + System.currentTimeMillis()); token.setValue("Base64Encoded_X509_CERTIFICATE..."); token.setValueType("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"); // check default encoding type Assert.assertEquals(token.getEncodingType(), BinarySecurityToken.ENCODING_TYPE_BASE64_BINARY); marshallAndUnmarshall(token); } @Test public void testCreated() throws Exception { //TODO } @Test public void testEmbedded() throws Exception { Embedded embedded = buildXMLObject(Embedded.ELEMENT_NAME); UsernameToken usernameToken = createUsernameToken("EmbeddedUT", "EmbeddedUT"); embedded.setValueType( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken"); embedded.getUnknownXMLObjects().add(usernameToken); marshallAndUnmarshall(embedded); } @Test public void testEncryptedHeader() throws Exception { EncryptedHeader eh = buildXMLObject(EncryptedHeader.ELEMENT_NAME); eh.setWSUId("abc123"); eh.setSOAP11MustUnderstand(true); eh.setSOAP11Actor("urn:test:soap11actor"); eh.setSOAP12MustUnderstand(true); eh.setSOAP12Role("urn:test:soap12role"); eh.setSOAP12Relay(true); marshallAndUnmarshall(eh); } @Test public void testExpires() throws Exception { //TODO } @Test public void testIteration() throws Exception { Iteration iteration = buildXMLObject(Iteration.ELEMENT_NAME); iteration.setValue(new Integer(1000)); marshallAndUnmarshall(iteration); } @Test public void testKeyIdentifier() throws Exception { //TODO } @Test public void testNonce() throws Exception { Nonce nonce = buildXMLObject(Nonce.ELEMENT_NAME); nonce.setValue("Base64EncodedValue..."); marshallAndUnmarshall(nonce); } @Test public void testPassword() throws Exception { Password password = buildXMLObject(Password.ELEMENT_NAME); password.setValue("test"); // check default Assert.assertEquals(password.getType(), Password.TYPE_PASSWORD_TEXT); marshallAndUnmarshall(password); } @Test public void testReference() throws Exception { Reference reference = buildXMLObject(Reference.ELEMENT_NAME); reference.setValueType( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken"); reference.setURI("#UsernameToken-0000001"); marshallAndUnmarshall(reference); } @Test public void testSalt() throws Exception { Salt salt = buildXMLObject(Salt.ELEMENT_NAME); salt.setValue("Base64Encoded_Salt_VALUE..."); marshallAndUnmarshall(salt); } @Test public void testSecurity() throws Exception { //TODO } @Test public void testSecurityTokenReference() throws Exception { //TODO } @Test public void testSignatureConfirmation() throws Exception { //TODO } @Test public void testTimestamp() throws Exception { Timestamp timestamp = buildXMLObject(Timestamp.ELEMENT_NAME); Created created = buildXMLObject(Created.ELEMENT_NAME); DateTime now = new DateTime(); created.setDateTime(now); timestamp.setCreated(created); Expires expires = buildXMLObject(Expires.ELEMENT_NAME); expires.setDateTime(now.plusMinutes(10)); timestamp.setExpires(expires); timestamp.setWSUId("Timestamp-" + System.currentTimeMillis()); marshallAndUnmarshall(timestamp); } @Test public void testTransformationParameters() throws Exception { //TODO } @Test public void testUsername() throws Exception { Username username = buildXMLObject(Username.ELEMENT_NAME); username.setValue("test"); marshallAndUnmarshall(username); } @Test public void testUsernameToken() throws Exception { String refId = "UsernameToken-007"; String refDateTimeStr = "2007-12-19T09:53:08.335Z"; UsernameToken usernameToken = createUsernameToken("test", "test"); usernameToken.setWSUId(refId); DateTime refDateTime = new DateTime(refDateTimeStr); Created usernameCreated = (Created) usernameToken.getUnknownXMLObjects(Created.ELEMENT_NAME).get(0); usernameCreated.setDateTime(refDateTime); // check default password type Password password = (Password) usernameToken.getUnknownXMLObjects(Password.ELEMENT_NAME).get(0); Assert.assertNotNull(password); Assert.assertEquals(password.getType(), Password.TYPE_PASSWORD_TEXT); List<XMLObject> children = usernameToken.getOrderedChildren(); Assert.assertEquals(children.size(), 3); marshallAndUnmarshall(usernameToken); // TODO impl unmarshallAndMarshall method // UsernameToken refUsernameToken= // unmarshallXML("/data/usernametoken.xml"); // Document refDocument= refUsernameToken.getDOM().getOwnerDocument(); // refUsernameToken.releaseDOM(); Document refDocument = parseXMLDocument("/data/org/opensaml/soap/wssecurity/impl/UsernameToken.xml"); //System.out.println("XXX: " + XMLHelper.nodeToString(refDocument.getDocumentElement())); Marshaller marshaller = getMarshaller(usernameToken); Element element = marshaller.marshall(usernameToken); Document document = element.getOwnerDocument(); // compare with XMLUnit XMLAssertTestNG.assertXMLIdentical(new Diff(refDocument, document), true); // unmarshall directly from file UsernameToken ut = unmarshallElement("/data/org/opensaml/soap/wssecurity/impl/UsernameToken.xml"); Assert.assertEquals(ut.getUsername().getValue(), "test"); Password utPassword = (Password) ut.getUnknownXMLObjects(Password.ELEMENT_NAME).get(0); Assert.assertNotNull(utPassword); Assert.assertEquals(utPassword.getValue(), "test"); Created utCreated = (Created) ut.getUnknownXMLObjects(Created.ELEMENT_NAME).get(0); Assert.assertNotNull(utCreated); DateTime created = utCreated.getDateTime(); System.out.println(created); } protected UsernameToken createUsernameToken(String user, String pass) throws Exception { UsernameToken usernameToken = buildXMLObject(UsernameToken.ELEMENT_NAME); Username username = buildXMLObject(Username.ELEMENT_NAME); username.setValue(user); Password password = buildXMLObject(Password.ELEMENT_NAME); password.setValue(pass); Created created = buildXMLObject(Created.ELEMENT_NAME); DateTime now = new DateTime(); created.setDateTime(now); String id = "UsernameToken-" + System.currentTimeMillis(); usernameToken.setWSUId(id); usernameToken.setUsername(username); usernameToken.getUnknownXMLObjects().add(password); usernameToken.getUnknownXMLObjects().add(created); return usernameToken; } }