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 java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.context.Context;
023import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage;
024import org.slf4j.LoggerFactory;
025
026import org.ametys.runtime.plugin.component.AbstractLogEnabled;
027
028/**
029 * CAS Proxy Granting Ticket manager, correlating PGTIOU with PGT
030 */
031public class CasProxyGrantingTicketManager extends AbstractLogEnabled implements ProxyGrantingTicketStorage, Component
032{
033    /** Avalon role */
034    public static final String ROLE = CasProxyGrantingTicketManager.class.getName();
035    
036    private static Map<String, String> _internalMap = new HashMap<>();
037    
038    /** Avalon context */
039    protected Context _context;
040    
041    /**
042     * Constructor for the instance not created by Avalon, but by AmetysCas20ProxyReceivingTicketValidationFilter
043     */
044    public CasProxyGrantingTicketManager()
045    {
046        setLogger(LoggerFactory.getLogger(getClass()));
047    }
048    
049    @Override
050    public void save(String proxyGrantingTicketIou, String proxyGrantingTicket)
051    {
052        getLogger().debug("Save proxyGrantingTicket '{}' for proxyGrantingTicketIou '{}'", proxyGrantingTicket, proxyGrantingTicketIou);
053        _internalMap.put(proxyGrantingTicketIou, proxyGrantingTicket);
054    }
055    
056    @Override
057    public String retrieve(String proxyGrantingTicketIou)
058    {
059        String proxyGrantingTicket = _internalMap.get(proxyGrantingTicketIou);
060        getLogger().debug("Retrieve proxyGrantingTicket for proxyGrantingTicketIou '{}'. Its returned value is '{}'", proxyGrantingTicketIou, proxyGrantingTicket);
061        return proxyGrantingTicket;
062    }
063    
064    @Override
065    public void cleanUp()
066    {
067        getLogger().debug("Cleaning up");
068        _internalMap.clear();
069    }
070}