Bo's blog

Saturday, November 14, 2009

SAML request and response

 

1. jar files:

.m2\repository\org\opensaml\opensaml\2.2.3\opensaml-2.2.3.jar
.m2\repository\org\opensaml\openws\1.2.2\openws-1.2.2.jar
.m2\repository\org\opensaml\xmltooling\1.2.0\xmltooling-1.2.0.jar
.m2\repository\org\slf4j\slf4j-api\1.5.5\slf4j-api-1.5.5.jar
.m2\repository\org\slf4j\jcl-over-slf4j\1.5.5\jcl-over-slf4j-1.5.5.jar
.m2\repository\org\slf4j\log4j-over-slf4j\1.5.5\log4j-over-slf4j-1.5.5.jar
.m2\repository\joda-time\joda-time\1.5.2\joda-time-1.5.2.jar
.m2\repository\org\bouncycastle\bcprov-ext-jdk15\1.40\bcprov-ext-jdk15-1.40.jar
.m2\repository\org\apache\santuario\xmlsec\1.4.2\xmlsec-1.4.2.jar
.m2\repository\org\apache\commons\ssl\not-yet-commons-ssl\0.3.9\not-yet-commons-ssl-0.3.9.jar
.m2\repository\net\jcip\jcip-annotations\1.0\jcip-annotations-1.0.jar
.m2\repository\org\apache\xerces\xml-apis\2.9.1\xml-apis-2.9.1.jar
.m2\repository\org\apache\xerces\xercesImpl\2.9.1\xercesImpl-2.9.1.jar
.m2\repository\org\apache\xerces\resolver\2.9.1\resolver-2.9.1.jar
.m2\repository\org\apache\xerces\serializer\2.9.1\serializer-2.9.1.jar
.m2\repository\org\apache\xalan\xalan\2.7.1\xalan-2.7.1.jar
.m2\repository\commons-codec\commons-codec\1.3\commons-codec-1.3.jar
.m2\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar
.m2\repository\commons-collections\commons-collections\3.1\commons-collections-3.1.jar
.m2\repository\commons-lang\commons-lang\2.1\commons-lang-2.1.jar
.m2\repository\jargs\jargs\1.0\jargs-1.0.jar
.m2\repository\velocity\velocity\1.5\velocity-1.5.jar

.m2\repository\xmlunit\xmlunit\1.3\xmlunit-1.3.jar

.m2\repository\org\slf4j\slf4j-simple\1.5.5\slf4j-simple-1.5.5.jar

2. test the request build

package test;

import javax.xml.namespace.QName;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.opensaml.Configuration;
import org.opensaml.DefaultBootstrap;
import org.opensaml.common.xml.SAMLConstants;
import org.opensaml.saml1.core.AuthenticationQuery;
import org.opensaml.saml1.core.ConfirmationMethod;
import org.opensaml.saml1.core.NameIdentifier;
import org.opensaml.saml1.core.Request;
import org.opensaml.saml1.core.Subject;
import org.opensaml.saml1.core.SubjectConfirmation;
import org.opensaml.saml1.core.SubjectConfirmationData;
import org.opensaml.saml1.core.impl.RequestMarshaller;
import org.opensaml.xml.Namespace;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.io.MarshallerFactory;
import org.opensaml.xml.io.MarshallingException;
import org.opensaml.xml.io.UnmarshallerFactory;
import org.opensaml.xml.parse.BasicParserPool;
import org.opensaml.xml.schema.XSAny;
import org.opensaml.xml.schema.impl.XSAnyBuilder;
import org.opensaml.xml.util.XMLHelper;
import org.w3c.dom.Element;

public class ResquestTest {

    private BasicParserPool parser;
    private XMLObjectBuilderFactory builderFactory;
    private MarshallerFactory marshallerFactory;
    private UnmarshallerFactory unmarshallerFactory;

    @Before
    public void setUp() throws Exception {
        DefaultBootstrap.bootstrap();
        parser = new BasicParserPool();
        parser.setNamespaceAware(true);
        builderFactory = Configuration.getBuilderFactory();
        marshallerFactory = Configuration.getMarshallerFactory();
        unmarshallerFactory = Configuration.getUnmarshallerFactory();
    }

    @Test()
    public void testRequest() throws MarshallingException {
        Request request = (Request) builderFactory.getBuilder(
                Request.DEFAULT_ELEMENT_NAME).buildObject(
                Request.DEFAULT_ELEMENT_NAME);
        AuthenticationQuery authQuery = (AuthenticationQuery) builderFactory
                .getBuilder(AuthenticationQuery.DEFAULT_ELEMENT_NAME)
                .buildObject(AuthenticationQuery.DEFAULT_ELEMENT_NAME);
        request.setQuery(authQuery);
        request.setIssueInstant(new DateTime());
        request.setID("test id");
        request.addNamespace(new Namespace("urn:oasis:names:tc:SAML:1.0:protocol", "samlp"));
        authQuery
                .setAuthenticationMethod("urn:oasis:names:tc:SAML:1.0:am:password");

        Subject subject = (Subject) builderFactory.getBuilder(
                Subject.DEFAULT_ELEMENT_NAME).buildObject(
                Subject.DEFAULT_ELEMENT_NAME);
        authQuery.setSubject(subject);

        NameIdentifier nameId = (NameIdentifier) builderFactory.getBuilder(
                NameIdentifier.DEFAULT_ELEMENT_NAME).buildObject(
                NameIdentifier.DEFAULT_ELEMENT_NAME);
        subject.setNameIdentifier(nameId);

        nameId.setFormat(NameIdentifier.UNSPECIFIED);
        nameId.setNameIdentifier("oriadmin:ORI");

        SubjectConfirmation subjectConfirm = (SubjectConfirmation) builderFactory
                .getBuilder(SubjectConfirmation.DEFAULT_ELEMENT_NAME)
                .buildObject(SubjectConfirmation.DEFAULT_ELEMENT_NAME);
        subject.setSubjectConfirmation(subjectConfirm);
        ConfirmationMethod confirmMethod = (ConfirmationMethod) builderFactory
                .getBuilder(ConfirmationMethod.DEFAULT_ELEMENT_NAME)
                .buildObject(ConfirmationMethod.DEFAULT_ELEMENT_NAME);
        subjectConfirm.getConfirmationMethods().add(confirmMethod);

        confirmMethod
                .setConfirmationMethod("urn:oasis:names:tc:SAML:1.0:cm:bearer");

        XSAnyBuilder proxyBuilder = new XSAnyBuilder();
        QName oqname = new QName(SAMLConstants.SAML1_NS,
                SubjectConfirmationData.DEFAULT_ELEMENT_LOCAL_NAME,
                SAMLConstants.SAML1_PREFIX);
        XSAny confirmData = proxyBuilder
                .buildObject(oqname);
        subjectConfirm.setSubjectConfirmationData(confirmData);

        confirmData.setTextContent("password");


        RequestMarshaller reqMarshaller = (RequestMarshaller) marshallerFactory
                .getMarshaller(Request.DEFAULT_ELEMENT_NAME);
        Element elem = reqMarshaller.marshall(request);

        System.out.println(XMLHelper.prettyPrintXML(elem));
    }
}

 

3. test response

/*
* Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
*
* Licensed 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.saml1.core;

import static java.lang.System.out;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.opensaml.common.BaseTestCase;
import org.opensaml.xml.Configuration;
import org.opensaml.xml.io.Unmarshaller;
import org.opensaml.xml.io.UnmarshallingException;
import org.opensaml.xml.parse.XMLParserException;
import org.w3c.dom.Document;

/**
* Tests unmarshalling and marshalling for various response messages.
*/
public class ResponseTest extends BaseTestCase {

    /** Path to file with full response message */
    private String fullResponsePath;

    /**
     * Constructor
     */
    public ResponseTest() {
        fullResponsePath = "/data/org/opensaml/saml1/core/FullResponse.xml";
        // fullResponsePath = "/data/org/opensaml/saml1/core/response.xml";
    }

    /**
     * Tests unmarshalling a full response message.
     */

    private String readFileAsString(String filePath) throws java.io.IOException {
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(ResponseTest.class
                .getResource(fullResponsePath).getFile()));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(buf, 0, numRead);
        }
        reader.close();
        return fileData.toString();
    }
    public void testResponseUnmarshallFromString() throws IOException {

        try {
            String responseStr = readFileAsString(fullResponsePath);
            out.println(responseStr);
            Document responseDoc = parser.parse(new ByteArrayInputStream(responseStr.getBytes()));
            Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory()
                    .getUnmarshaller(responseDoc.getDocumentElement());

            Response response = (Response) unmarshaller.unmarshall(responseDoc
                    .getDocumentElement());
            out.println(response.getStatus().getStatusCode().getDOM()
                    .getAttribute("Value"));
            out.println(response.getStatus().getStatusMessage().getDOM()
                    .getTextContent());

            List<Assertion> assertions = response.getAssertions();

            for (Assertion assertion : assertions) {
                List<AttributeStatement> attrSts = assertion
                        .getAttributeStatements();
                for (AttributeStatement attrSt : attrSts) {
                    List<Attribute> attrs = attrSt.getAttributes();
                    for (Attribute attr : attrs) {
                        out.printf("AttributeName: %s, Value: %s %n", attr
                                .getAttributeName(), attr.getAttributeValues()
                                .get(0).getDOM().getTextContent());
                    }
                }
            }

            assertEquals(
                    "First element of response data was not expected Response",
                    "Response", response.getElementQName().getLocalPart());
        } catch (XMLParserException xe) {
            fail("Unable to parse XML file: " + xe);
        } catch (UnmarshallingException ue) {
            fail("Unable to unmarshall XML: " + ue);
        }
    }

    public void testResponseUnmarshall() {

        try {
            InputStream in = ResponseTest.class
                    .getResourceAsStream(fullResponsePath);
            Document responseDoc = parser.parse(in);
            Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory()
                    .getUnmarshaller(responseDoc.getDocumentElement());

            Response response = (Response) unmarshaller.unmarshall(responseDoc
                    .getDocumentElement());
            out.println(response.getStatus().getStatusCode().getDOM()
                    .getAttribute("Value"));
            out.println(response.getStatus().getStatusMessage().getDOM()
                    .getTextContent());

            List<Assertion> assertions = response.getAssertions();

            for (Assertion assertion : assertions) {
                List<AttributeStatement> attrSts = assertion
                        .getAttributeStatements();
                for (AttributeStatement attrSt : attrSts) {
                    List<Attribute> attrs = attrSt.getAttributes();
                    for (Attribute attr : attrs) {
                        out.printf("AttributeName: %s, Value: %s %n", attr
                                .getAttributeName(), attr.getAttributeValues()
                                .get(0).getDOM().getTextContent());
                    }
                }
            }

            assertEquals(
                    "First element of response data was not expected Response",
                    "Response", response.getElementQName().getLocalPart());
        } catch (XMLParserException xe) {
            fail("Unable to parse XML file: " + xe);
        } catch (UnmarshallingException ue) {
            fail("Unable to unmarshall XML: " + ue);
        }
    }

    /**
     * Tests marshalling a full response message.
     */
    public void testResponseMarshall() {
        // TODO
    }
}

0 Comments:

Post a Comment

<< Home