SecuritySession.java

/*
** Module   : SecuritySession.java
** Abstract : Maintains per connection data.
**
** Copyright (c) 2004-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ---------------------------------Description-----------------------------------
** 001 NVS 20050307   @20214 Created. The Session class maintains per connection data. 
** 002 NVS 20050505   @21111 Added getCache() method that returns the instance of the security cache this
**                           session is bound to. This helps in fixing the security cache refresh bug.
** 003 GES 20090610   @42763 Added timestamp of when the session was created which is essentially the same
**                           thing as when the account logon occurred.
** 004 GES 20090622   @42785 Removed unnecessary set and accessor.
** 005 GES 20210921          Added flags to remember the userid and if the session is interactive.
** 006 GBB 20231117          Added fields sessionUuid, relatedSessionUuid, pid, osUser, driverType,
**                           httpPort, description, browserWebSocket.
** 007 GBB 20231130          driverClass replaced by driverName.
** 008 GBB 20240313          Class made public, while setters package-private.
**                           Renamed to SecuritySession to resolve ambigious imports.
*/ 

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.security;

/**
 * Package private class that maintains all per connection data. Particularly,
 * the reference to the generation of the <code>SecurityCache</code> that is
 * in effect when the session is created, is kept here. This allows for a safe
 * fading out of all older generations of <code>SecurityCache</code> as soon
 * as the last session referring to the old <code>SecurityCache</code> goes 
 * away.
 */
public class SecuritySession
{
   /** An object that serves as an ID of this connection */
   private Object id = null;

   /** security context created for this session */
   private SecurityContext context = null;

   /** security cache in effect by the time of session creation */
   private SecurityCache cache = null;

   /** Timestamp of when this session was created. */
   private long timestamp = System.currentTimeMillis();
   
   /** Account name. */
   private String userid = null;
   
   /** Flag to mark a session as an interactive user. */
   private boolean interactive = false;

   /** The session UUID. */
   private String sessionUuid;
   
   /** The related session UUID. */
   private String relatedSessionUuid;
   
   /** The client driver name. */
   private String driverName;

   /** The related session UUID. */
   private long pid;

   /** The related session UUID. */
   private String osUserName;

   /** The web client HTTP port. */
   private int httpPort;

   /** Session description from SsoAuthenticator. */
   private String description;

   /** The browser WebSocket address. */
   private String browserWebSocket;

   /**
    * Package private constructor for client side Session.
    *
    * @param id
    *        session ID, normally the socket that maps the connection
    * @param cache
    *        reference to a <code>SecurityCache</code>
    */
   SecuritySession(Object id, SecurityCache cache)
   {
      this.id = id;
      this.cache = cache;
   }
   
   /**
    * Package private constructor for server side Session.
    *
    * @param id
    *        session ID, normally the socket that maps the connection
    * @param cache
    *        reference to a <code>SecurityCache</code>
    * @param context
    *        security context to be associated with the session
    */
   SecuritySession(Object id, SecurityCache cache, SecurityContext context)
   {
      this(id, cache);
      this.context = context;
   }

   /**
    * Sets the ui driver class.
    *
    * @param   driverName
    *          The ui driver class. 
    */
   void setDriverName(String driverName)
   {
      this.driverName = driverName;
   }
   
   /**
    * Returns the client ui driver class.
    *
    * @return See above.
    */
   public String getDriverName()
   {
      return driverName;
   }
   
   /**
    * Sets the client pid.
    *
    * @param   pid
    *          The client pid. 
    */
   void setPid(long pid)
   {
      this.pid = pid;
   }
   
   /**
    * Returns the client pid.
    *
    * @return See above.
    */
   public long getPid()
   {
      return pid;
   }
   
   /**
    * Sets the OS user name.
    * 
    * @param   osUserName
    *          The OS user name. 
    */
   void setOsUserName(String osUserName)
   {
      this.osUserName = osUserName;
   }
   
   /**
    * Returns the OS user name.
    *
    * @return See above.
    */
   public String getOsUserName()
   {
      return osUserName;
   }

   /**
    * Returns the session description from SsoAuthenticator.
    *
    * @return See above.
    */
   public String getDescription()
   {
      return description;
   }

   /**
    * Sets the session description from SsoAuthenticator.
    *
    * @param   description
    *          The session description from SsoAuthenticator.
    */
   void setDescription(String description)
   {
      this.description = description;
   }

   /**
    * Returns the web client HTTP port.
    * 
    * @return See above.
    */
   public int getHttpPort()
   {
      return httpPort;
   }

   /**
    * Sets the web client HTTP port.
    *
    * @param   httpPort
    *          The web client HTTP port.
    */
   void setHttpPort(int httpPort)
   {
      this.httpPort = httpPort;
   }

   /**
    * Gets the browser WebSocket address.
    *
    * @return The browser WebSocket address.
    */
   public String getBrowserWebSocket()
   {
      return browserWebSocket;
   }

   /**
    * Sets the browser WebSocket address.
    *
    * @param   browserWebSocket
    *          The browser WebSocket address.
    */
   void setBrowserWebSocket(String browserWebSocket)
   {
      this.browserWebSocket = browserWebSocket;
   }

   /**
    * Checks whether this session was created with the specified security
    * context.
    *
    * @param  context
    *         security context to check
    * @return <code>true</code> if security context matches
    */
   boolean hasContext(SecurityContext context)
   {
      return this.context == context;
   }

   /**
    * Gets the session ID object.
    *
    * @return session ID object
    */
   Object getSessionId()
   {
      return id;
   }

   /**
    * Gets session UUID.
    *
    * @return session UUID.
    */
   public String getUid()
   {
      return sessionUuid;
   }

   /**
    * Gets related session UUID.
    *
    * @return related session UUID.
    */
   public String getRelatedSessionUuid()
   {
      return relatedSessionUuid;
   }

   /**
    * Sets the session UUID.
    *
    * @param   uuid
    *          The session UUID.
    */
   void setUuid(String uuid)
   {
      this.sessionUuid = uuid;
   }

   /**
    * Sets the uuid of the original session (different from uuid when forked).
    *
    * @param   relatedUuid
    *          The uuid of the related session.
    */
   void setRelatedSessionUuid(String relatedUuid)
   {
      this.relatedSessionUuid = relatedUuid;
   }

   /**
    * Gets the session's account name.
    *
    * @return    The userid.
    */
   String getUserid()
   {
      return userid;
   }

   /**
    * Set the session's account name.
    *
    * @param    userid
    *           The account name.
    */
   void setUserid(String userid)
   {
      this.userid = userid;
   }
   
   /**
    * Mark the session as interactive.
    */
   void setInteractive()
   {
      interactive = true;
   }
   
   /**
    * Reports if the session is interactive.
    * 
    * @return   {@code true} if the session is interactive.
    */
   boolean isInteractive()
   {
      return interactive;
   }

   /**
    * Gets the session security context.
    *
    * @return session <code>SecurityContext</code>
    */
   SecurityContext getSecurityContext()
   {
      return context;
   }

   /**
    * Gets the security cache this session is bound to.
    *
    * @return session <code>SecurityCache</code>
    */
   SecurityCache getCache()
   {
      return cache;
   }
   
   /**
    * Obtain the timestamp of when this session was created.
    *
    * @return   Milliseconds since the January 1, 1970.
    */
   public long getTimeStamp()
   {
      return timestamp;
   }
}