001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.extrausermgt.authentication.cas;
017
018import org.jasig.cas.client.util.CommonUtils;
019import org.jasig.cas.client.util.XmlUtils;
020import org.jasig.cas.client.validation.Assertion;
021import org.jasig.cas.client.validation.Cas20ProxyTicketValidator;
022import org.jasig.cas.client.validation.TicketValidationException;
023
024/**
025 * Ametys implementation of the TicketValidator that will validate Service Tickets in compliance with the CAS 2.
026 */
027public class AmetysCas20ProxyTicketValidator extends Cas20ProxyTicketValidator
028{
029    /** The proxy granting ticket */
030    protected String _proxyGrantingTicket;
031
032    /**
033     * Constructs an instance of the CAS 2.0 Service Ticket Validator with the supplied
034     * CAS server url prefix.
035     *
036     * @param casServerUrlPrefix the CAS Server URL prefix.
037     */
038    public AmetysCas20ProxyTicketValidator(final String casServerUrlPrefix)
039    {
040        super(casServerUrlPrefix);
041        _proxyGrantingTicket = null;
042    }
043    
044    @Override
045    protected void customParseResponse(final String response, final Assertion assertion) throws TicketValidationException
046    {
047        super.customParseResponse(response, assertion);
048        
049        final String proxyGrantingTicketIou = XmlUtils.getTextForElement(response, "proxyGrantingTicket");
050
051        if (CommonUtils.isNotBlank(proxyGrantingTicketIou))
052        {
053            _proxyGrantingTicket = getProxyGrantingTicketStorage().retrieve(proxyGrantingTicketIou);
054            logger.debug("proxyGrantingTicketIou was found in response ({}), the associated proxyGrantingTicket is '{}'", proxyGrantingTicketIou, _proxyGrantingTicket);
055        }
056        else
057        {
058            logger.debug("While parsing response, the proxyGrantingTicketIou was not found.");
059        }
060    }
061    
062    /**
063     * Gets the proxy granting ticket of the current request
064     * @return The proxy granting ticket of the current request
065     */
066    public String getProxyGrantingTicket()
067    {
068        return _proxyGrantingTicket;
069    }
070}