[SOLVED] Client-side session logout after idle time elapses implementation lazarus / 21-General ---------------------------------------------------------------------- JD · 2021-07-14 09:19 Hi there everyone, I am trying to implement a client-side session logout after a predetermined idle time (say 20 minutes). This is to ensure that my users don't carelessly leave forms with sensitive information unattended and open to prying eyes. At the moment, I have in place an event (TEvent) which fires every 20 minutes and displays a non-modal client form with login fields that hides the form the user was working on until the user logs in again. An excerpt of my code is shown below: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //// Thread for the idle session event//unit uidlesessiontimeout; {$mode objfpc}{$H+} interface uses  Classes, SysUtils,  DateUtils,           SyncObjs;             type  { TErrorMsg }  TErrorMsg = procedure (AString: string) of object;   { TIdleSessionTimeOutThread }  TIdleSessionTimeOutThread = class(TThread)  private    FErrorMsg: string;    FEvent: TEvent;    FInterval: int64;    FOnTimer: TNotifyEvent;    FOnError: TErrorMsg;    procedure DoError;  protected    procedure Execute; override;    procedure DoTimer;    // for syncing data request with the main thread    procedure DoSessionTimedOut;  public    constructor Create(ACreateSuspended: boolean; AInterval: Int64);    destructor  Destroy; override;    procedure Terminate; reintroduce; // redefined    //    property Terminated;      // expose the parent property again    property ErrorMsg: string read FErrorMsg write FErrorMsg;    //    property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;    //    property OnError: TErrorMsg read FOnError write FOnError;  end;  var  idlesessionformvisible: Boolean; implementation uses  main{, sessionlogin}; { TIdleSessionTimeOutThread } procedure TIdleSessionTimeOutThread.DoError;begin  // check if the event is assigned  if Assigned(FOnError) then    // call it    FOnError(ErrorMsg);end; procedure TIdleSessionTimeOutThread.Execute;begin  //  while not Terminated do  begin    try      case FEvent.WaitFor(FInterval) of        wrTimeout:          begin            //if not idlesessionformvisible then          //begin            //  // Show the idle session timeout form          //  frmMain.actSessionLoginExecute(nil);            //  //          //  idlesessionformvisible := True;            //end            //else            //  Sleep(5000);            //  Suspend;            //  WaitFor(5000);             // Show the idle session timeout form          Synchronize(@DoSessionTimedOut);          end;        wrSignaled, wrError:          Terminate;      end;    finally      //    end;  end;    // while not Terminated doend; procedure TIdleSessionTimeOutThread.DoTimer;begin  //  if Assigned(FOnTimer) then    FOnTimer(self);end; procedure TIdleSessionTimeOutThread.DoSessionTimedOut;begin  frmMain.actSessionLoginExecute(nil);          // Display the non-modal session login formend; constructor TIdleSessionTimeOutThread.Create(ACreateSuspended: boolean;  AInterval: Int64);begin  //idlesessionformvisible := False;  //  FEvent    := TEvent.Create(nil, False, False, '');      FInterval := AInterval;  Priority  := tpLower;     // tpIdle, tpLowest, tpLower  inherited Create(ACreateSuspended);end; destructor TIdleSessionTimeOutThread.Destroy;begin  FreeAndNil(FEvent);  inherited Destroy;end; procedure TIdleSessionTimeOutThread.Terminate;begin  // Activate the event  FEvent.SetEvent;end; end.  This works very well and is fired properly after 20 minutes regardless of what the user is doing. However I would like the event to be fired ONLY IF the user is idle: a) the user did NOT press a key; pressing a key in any form should reset the idle time interval to 20 minutes b) the mouse was not used (this part is optional  ) My application is modular and uses many non-modal and modal forms depending on the task at hand. I thought of using the TIdleTimer component but I am not sure of how it works. Can anyone help me achieve at least (a) above application-wide? Cheers, JD ---------------------------------------------------------------------- 440bx · 2021-07-14 10:54 I can suggest something that may be helpful but it's only for Windows.  In Windows, you can use GetLastInputInfo which will tell you the last time that the user interacted with your program.  The important part is to keep in mind that it is NOT a system-wide idle condition, it's just a "no input to the program" condition which, for what you're trying to accomplish, sounds like it would fit the bill. GetLastInputInfo is a documented Windows function.  You can find all the info for it on MSDN. HTH. ---------------------------------------------------------------------- HeavyUser · 2021-07-14 10:55 the application object has a number of events you can attach to one. I would start by using the onIDdle event to start the timer and see if the onuserinput can be used to to stop your timer. You could also attach your self to the hint mechanism and start the timer when the hitn timer goes of allowing some grace period between or even extend the hint to 20 minutes and log the out. Opinions only, I do not have lazarus install to check it out. ---------------------------------------------------------------------- alpine · 2021-07-14 11:51 Quote from: JD on July 14, 2021, 11:19:46 am*snip* I thought of using the TIdleTimer component but I am not sure of how it works. I'm using TIdleTimer to auto-hide my UI side panels. You may set it that way for example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Interval := AInterval;AutoStartEvent := itaOnIdle;AutoEndEvent := itaOnUserInput;OnTimer := @DoSessionTimedOut; // when you want it activeAutoEnabled := True; // to disengageAutoEnabled := False;Enabled := False; ---------------------------------------------------------------------- JD · 2021-07-15 09:33 Quote from: 440bx on July 14, 2021, 12:54:52 pmI can suggest something that may be helpful but it's only for Windows.  In Windows, you can use GetLastInputInfo which will tell you the last time that the user interacted with your program.  The important part is to keep in mind that it is NOT a system-wide idle condition, it's just a "no input to the program" condition which, for what you're trying to accomplish, sounds like it would fit the bill. GetLastInputInfo is a documented Windows function.  You can find all the info for it on MSDN. HTH. Thanks for this. I had completely forgotten about it. I'll look it up again. JD ---------------------------------------------------------------------- JD · 2021-07-15 09:36 Quote from: y.ivanov on July 14, 2021, 01:51:49 pmQuote from: JD on July 14, 2021, 11:19:46 am*snip* I thought of using the TIdleTimer component but I am not sure of how it works. I'm using TIdleTimer to auto-hide my UI side panels. You may set it that way for example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Interval := AInterval;AutoStartEvent := itaOnIdle;AutoEndEvent := itaOnUserInput;OnTimer := @DoSessionTimedOut; // when you want it activeAutoEnabled := True; // to disengageAutoEnabled := False;Enabled := False; Thanks for the tip.  My application has side panels too but I allow the user to hide it when it is not needed. What delay (i.e the value of Interval variable) do you give your side- panels before they are hidden? JD ---------------------------------------------------------------------- JD · 2021-07-15 09:51 Quote from: HeavyUser on July 14, 2021, 12:55:35 pmthe application object has a number of events you can attach to one. I would start by using the onIDdle event to start the timer and see if the onuserinput can be used to to stop your timer. You could also attach your self to the hint mechanism and start the timer when the hitn timer goes of allowing some grace period between or even extend the hint to 20 minutes and log the out. Opinions only, I do not have lazarus install to check it out. Thanks a lot. I am currently testing this option because it is cross- platform. My initial tests look very promising. When the application is running normally Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Application.OnIdle          := @OnIdleEvent;               // enables the timerApplication.OnUserInput  := @OnUserInputEvent;       // disables the timer tmIdletimer.OnTimer       := @DoSessionTimeout     // displays the session timeout login form  Inside of the DoSessionTimeout's Create procedure Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Application.OnIdle  := nil;  // no need for testing "idleness" as long as the session login form is active  Inside of the DoSessionTimeout's (successful) login Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Application.OnIdle  := @OnIdleEvent;   // start testing for "idleness" again  This is what I've done and am currently testing. If I get the desired results, I might even abandon the thread based TEvent option I was using before. JD ---------------------------------------------------------------------- alpine · 2021-07-15 11:12 Quote from: JD on July 15, 2021, 11:36:46 amThanks for the tip.  My application has side panels too but I allow the user to hide it when it is not needed. What delay (i.e the value of Interval variable) do you give your side- panels before they are hidden? The delay is configurable together with the auto-hide. In my particular case, the panel is more like a "drawer" - when you press a button on the toolbar, then the panel shows up with a buttons to select the second choice. Once selected the panel hides again. Or hides after some time passed and nothing selected (when configured). Quote from: JD on July 15, 2021, 11:51:12 am*snip* Thanks a lot. I am currently testing this option because it is cross- platform. My initial tests look very promising. When the application is running normally Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Application.OnIdle          := @OnIdleEvent;               // enables the timerApplication.OnUserInput  := @OnUserInputEvent;       // disables the timer tmIdletimer.OnTimer       := @DoSessionTimeout     // displays the session timeout login form  The TIdleTimer (TCustomIdleTimer) does it similar. You can inspect the idletimer.inc to see. The convenience is that with TIdleTimer you can attach the corresponding event handlers with the form designer and also to use AutoEnabled and Enabled properties instead of assigning directly Application.OnIdle. Also it uses a chained event handlers versus the single OnIdle property, which is an advantage when you'd want to use the idle mechanism in more than one place or form. ---------------------------------------------------------------------- JD · 2021-07-15 16:58 Quote from: y.ivanov on July 15, 2021, 01:12:07 pmThe TIdleTimer (TCustomIdleTimer) does it similar. You can inspect the idletimer.inc to see. The convenience is that with TIdleTimer you can attach the corresponding event handlers with the form designer and also to use AutoEnabled and Enabled properties instead of assigning directly Application.OnIdle. Also it uses a chained event handlers versus the single OnIdle property, which is an advantage when you'd want to use the idle mechanism in more than one place or form. Thanks a lot for the information. I'll definitely check it out. JD