Package org.wso2.carbon.identity.sso.saml.ui

Source Code of org.wso2.carbon.identity.sso.saml.ui.ErrorResponseBuilder

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. 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.wso2.carbon.identity.sso.saml.ui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.axiom.om.util.UUIDGenerator;
import org.opensaml.DefaultBootstrap;
import org.opensaml.common.SAMLVersion;
import org.opensaml.saml2.core.*;
import org.opensaml.saml2.core.impl.*;
import org.opensaml.xml.ConfigurationException;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.util.Base64;
import org.opensaml.xml.io.MarshallerFactory;
import org.opensaml.xml.io.Marshaller;
import org.wso2.carbon.identity.base.*;
import org.wso2.carbon.identity.base.IdentityException;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;

import java.io.ByteArrayOutputStream;

public class ErrorResponseBuilder {

    private static Log log = LogFactory.getLog(ErrorResponseBuilder.class);

    //Do the bootstrap first
    static {
        try {
            DefaultBootstrap.bootstrap();
        } catch (ConfigurationException e) {
            log.error("Errors when bootstrapping the OpenSAML2 library", e);
        }
    }

    public static String generateErrorneousResponse() {
        Response response = new ResponseBuilder().buildObject();
        response.setIssuer(getIssuer());
        response.setStatus(buildStatus());
        response.setVersion(SAMLVersion.VERSION_20);
        response.setID(UUIDGenerator.getUUID());

        try {
            return encode(marshall(response));
        } catch (IdentityException e) {
            return null;
        }
    }


    private static Status buildStatus() {

        Status stat = new StatusBuilder().buildObject();

        //Set the status code
        StatusCode statCode = new StatusCodeBuilder().buildObject();
        statCode.setValue("urn:oasis:names:tc:SAML:2.0:status:Responder");
        stat.setStatusCode(statCode);
        StatusMessage statMesssage = new StatusMessageBuilder().buildObject();
        statMesssage.setMessage("Error when processing the Authentication Request");
        stat.setStatusMessage(statMesssage);

        return stat;
    }

    private static String marshall(XMLObject xmlObject) throws org.wso2.carbon.identity.base.IdentityException {
        try {
            System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                    "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

            MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
            Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
            Element element = marshaller.marshall(xmlObject);

            ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            DOMImplementationLS impl =
                    (DOMImplementationLS) registry.getDOMImplementation("LS");
            LSSerializer writer = impl.createLSSerializer();
            LSOutput output = impl.createLSOutput();
            output.setByteStream(byteArrayOutputStrm);
            writer.write(element, output);
            return byteArrayOutputStrm.toString();
        } catch (Exception e) {
            log.error("Error Serializing the SAML Response");
            throw new IdentityException("Error Serializing the SAML Response", e);
        }
    }

    private static Issuer getIssuer() {
        Issuer issuer = new IssuerBuilder().buildObject();
        issuer.setValue("WSO2 Identity Server");
        issuer.setFormat("urn:oasis:names:tc:SAML:2.0:nameid-format:entity");
        return issuer;
    }

   public static String encode(String authReq) {
        return new String(Base64.encodeBytes(authReq.getBytes()));
    }
}
TOP

Related Classes of org.wso2.carbon.identity.sso.saml.ui.ErrorResponseBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.