at.gv.egovernment.moa.id.protocols.pvp2x.requestHandler.AuthnRequestHandler.java Source code

Java tutorial

Introduction

Here is the source code for at.gv.egovernment.moa.id.protocols.pvp2x.requestHandler.AuthnRequestHandler.java

Source

/*******************************************************************************
 * Copyright 2014 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 *******************************************************************************/
package at.gv.egovernment.moa.id.protocols.pvp2x.requestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.opensaml.common.xml.SAMLConstants;
import org.opensaml.saml2.core.Assertion;
import org.opensaml.saml2.core.AuthnRequest;
import org.opensaml.saml2.core.Response;
import org.opensaml.saml2.metadata.AssertionConsumerService;
import org.opensaml.saml2.metadata.EntityDescriptor;
import org.opensaml.ws.message.encoder.MessageEncodingException;
import org.opensaml.xml.security.SecurityException;

import at.gv.egovernment.moa.id.auth.exception.MOAIDException;
import at.gv.egovernment.moa.id.data.IAuthData;
import at.gv.egovernment.moa.id.data.SLOInformationImpl;
import at.gv.egovernment.moa.id.data.SLOInformationInterface;
import at.gv.egovernment.moa.id.protocols.pvp2x.PVPConstants;
import at.gv.egovernment.moa.id.protocols.pvp2x.PVPTargetConfiguration;
import at.gv.egovernment.moa.id.protocols.pvp2x.binding.ArtifactBinding;
import at.gv.egovernment.moa.id.protocols.pvp2x.binding.IEncoder;
import at.gv.egovernment.moa.id.protocols.pvp2x.messages.InboundMessage;
import at.gv.egovernment.moa.id.protocols.pvp2x.messages.MOARequest;
import at.gv.egovernment.moa.id.protocols.pvp2x.binding.PostBinding;
import at.gv.egovernment.moa.id.protocols.pvp2x.binding.RedirectBinding;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.AuthResponseBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.assertion.PVP2AssertionBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.exceptions.BindingNotSupportedException;
import at.gv.egovernment.moa.id.protocols.pvp2x.utils.SAML2Utils;
import at.gv.egovernment.moa.logging.Logger;

public class AuthnRequestHandler implements IRequestHandler, PVPConstants {

    public boolean handleObject(InboundMessage obj) {

        return (obj instanceof MOARequest && ((MOARequest) obj).getSamlRequest() instanceof AuthnRequest);
    }

    public SLOInformationInterface process(PVPTargetConfiguration obj, HttpServletRequest req,
            HttpServletResponse resp, IAuthData authData) throws MOAIDException {
        if (!handleObject(obj.getRequest())) {
            throw new MOAIDException("pvp2.13", null);
        }

        //get basic information
        MOARequest moaRequest = (MOARequest) obj.getRequest();
        AuthnRequest authnRequest = (AuthnRequest) moaRequest.getSamlRequest();
        EntityDescriptor peerEntity = moaRequest.getEntityMetadata();

        AssertionConsumerService consumerService = SAML2Utils.createSAMLObject(AssertionConsumerService.class);
        consumerService.setBinding(obj.getBinding());
        consumerService.setLocation(obj.getConsumerURL());

        DateTime date = new DateTime();

        SLOInformationImpl sloInformation = new SLOInformationImpl();

        //build Assertion
        Assertion assertion = PVP2AssertionBuilder.buildAssertion(authnRequest, authData, peerEntity, date,
                consumerService, sloInformation);

        Response authResponse = AuthResponseBuilder.buildResponse(authnRequest, date, assertion);

        IEncoder binding = null;

        if (consumerService.getBinding().equals(SAMLConstants.SAML2_REDIRECT_BINDING_URI)) {
            binding = new RedirectBinding();

        } else if (consumerService.getBinding().equals(SAMLConstants.SAML2_ARTIFACT_BINDING_URI)) {
            // TODO: not supported YET!!
            binding = new ArtifactBinding();

        } else if (consumerService.getBinding().equals(SAMLConstants.SAML2_POST_BINDING_URI)) {
            binding = new PostBinding();

        }

        if (binding == null) {
            throw new BindingNotSupportedException(consumerService.getBinding());
        }

        try {
            binding.encodeRespone(req, resp, authResponse, consumerService.getLocation(),
                    moaRequest.getRelayState());

            return sloInformation;

        } catch (MessageEncodingException e) {
            Logger.error("Message Encoding exception", e);
            throw new MOAIDException("pvp2.01", null, e);

        } catch (SecurityException e) {
            Logger.error("Security exception", e);
            throw new MOAIDException("pvp2.01", null, e);

        }
    }
}