001/*
002 * Copyright (c) 2013 - 2016 TDP Ltd All Rights Reserved.
003 * TDP Ltd grants permission, free of charge, to any person obtaining copies
004 * of this software and its associated documentation files (the "Software"),
005 * to deal in the Software without restriction, including to use, copy, adapt,
006 * publish, distribute, display, perform, sublicense, and sell copies of the
007 * Software, subject to the following condition: You must include the above
008 * copyright notice and this permission notice in all full or partial copies
009 * of the Software.
010 * 
011 * TDP LTD PROVIDES THE SOFTWARE "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
012 * INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
013 * PARTICULAR PURPOSE, AND NON-INFRINGMENT. TDP LTD, THE AUTHORS OF THE SOFTWARE,
014 * AND THE OWNERS OF COPYRIGHT IN THE SOFTWARE ARE NOT LIABLE FOR ANY CLAIM, DAMAGES,
015 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
016 * FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
017 * THE SOFTWARE.
018 */
019package cz.tdp.kshield.integration;
020
021import java.util.ArrayList;
022import java.util.Collection;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027
028/**
029 * Holds information about current user session
030 */
031public final class KShieldContext
032{
033  private static final ThreadLocal<Boolean> kshield = new ThreadLocal<>();
034  
035  private static final Collection<Runnable> initSessionHooks = new ArrayList<>(1);
036  private static final Collection<Runnable> startSessionHooks = new ArrayList<>(1);
037  private static final Collection<Runnable> closeSessionHooks = new ArrayList<>(1);
038  
039  /**
040   * Add task perfomed before any request to KeyShield SSO Server
041   * 
042   * @param hook
043   */
044  public static void addInitHook(Runnable hook) {
045    initSessionHooks.add(hook);
046  }
047  
048  /**
049   * Add tak perfomed after succesful UserInfo retrieval and validation
050   * 
051   * @param hook
052   */
053  public static void addStartHook(Runnable hook) {
054    startSessionHooks.add(hook);
055  }
056  
057  /**
058   * Add task performed when KeyShield SSO Server session is finished or in case of retrieval or validation failure
059   * 
060   * @param hook
061   */
062  public static void addCloseHook(Runnable hook) {
063    closeSessionHooks.add(hook);
064  }
065  
066  /**
067   * Called before initiating KeyShield SSO Server session
068   */
069  public static void initKShieldSession() {
070    kshield.remove();
071    
072    runHooks(initSessionHooks);
073  }
074  
075  /**
076   * Called after succesful UserInfo retrieval and validation
077   */
078  public static void startKShieldSession() {
079    kshield.set(Boolean.TRUE);
080    
081    runHooks(startSessionHooks);
082  }
083  
084  /**
085   * Called when KeyShield SSO Server session is finished or in case of retrieval or validation failure
086   */
087  public static void closeKShieldSession() {
088    kshield.remove();
089    
090    runHooks(closeSessionHooks);
091  }
092  
093  /**
094   * @return true if current user was succesfully identified to KeyShield SSO Server
095   */
096  public static boolean isKShieldSession() {
097    final Boolean sess = kshield.get();
098    return sess != null && sess;
099  }
100  
101  private static void runHooks(Collection<Runnable> hooks) {
102    for (Runnable hook : hooks) {
103      try {
104        hook.run();
105      }
106      catch (Exception e) {
107        log.warn("Exception while executing kshield context hook", e);
108      }
109    }
110  }
111  
112  private static final Log log = LogFactory.getLog(KShieldContext.class);
113  
114  private KShieldContext() {
115    throw new UnsupportedOperationException();
116  }
117}