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 javax.servlet.FilterConfig;
019import javax.servlet.ServletException;
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022
023import org.jasig.cas.client.validation.Assertion;
024import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
025import org.jasig.cas.client.validation.TicketValidator;
026
027/**
028 * Ametys implementation of Cas20ProxyReceivingTicketValidationFilter
029 */
030public class AmetysCas20ProxyReceivingTicketValidationFilter extends Cas20ProxyReceivingTicketValidationFilter
031{
032    /** The session attribute for the proxy granting ticket */
033    public static final String SESSION_ATTRIBUTE_PROXY_GRANTING_TICKET = "CasCredentialProvider:ProxyGrantingTicket";
034    
035    /** The ticket validator */
036    protected TicketValidator _validator;
037    
038    @Override
039    protected void initInternal(final FilterConfig filterConfig) throws ServletException
040    {
041        super.initInternal(filterConfig);
042        
043        // Workaround to get a reference to the ticket validator. As a result, org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter.getTicketValidator(FilterConfig) is called twice...
044        _validator = getTicketValidator(filterConfig);
045        setTicketValidator(_validator);
046    }
047    
048    @Override
049    protected void onSuccessfulValidation(HttpServletRequest request, HttpServletResponse response, Assertion assertion)
050    {
051        super.onSuccessfulValidation(request, response, assertion);
052        
053        if (_validator instanceof AmetysCas20ProxyTicketValidator)
054        {
055            String proxyGrantingTicket = ((AmetysCas20ProxyTicketValidator) _validator).getProxyGrantingTicket();
056            logger.debug("Ticket validation succeeded. The proxy granting ticket from Ametys Proxy Ticket Validator is: '{}'", proxyGrantingTicket);
057            request.getSession().setAttribute(SESSION_ATTRIBUTE_PROXY_GRANTING_TICKET, proxyGrantingTicket);
058        }
059    }
060}