17229: Webshell add idle timer and auto log out
authorStephen Smith <stephen@curii.com>
Mon, 20 Sep 2021 17:03:44 +0000 (13:03 -0400)
committerStephen Smith <stephen@curii.com>
Mon, 20 Sep 2021 17:03:44 +0000 (13:03 -0400)
Arvados-DCO-1.1-Signed-off-by: Stephen Smith <stephen@curii.com>

public/webshell/index.html
src/views/virtual-machine-panel/virtual-machine-user-panel.tsx

index 722cab5a887ef733df22babefcec33c3b1303620..126962b6a764c6d49082a1a41d50a2eb9e4d8a17 100644 (file)
       var token = urlParams.get('token');
       var user = urlParams.get('login');
       var host = urlParams.get('host');
+      var timeout = urlParams.get('timeout');
       urlParams = null;
 
+      var idleTimeoutMs = timeout * 1000;
+
+      function updateIdleTimer() {
+        var currentTime = Date.now();
+        var lastTime = localStorage.getItem('lastActiveTimestamp');
+        if (currentTime - lastTime > 1000) {
+          localStorage.setItem('lastActiveTimestamp', currentTime);
+        }
+      }
+
+      function checkIdleTimer() {
+        var currentTime = Date.now();
+        var lastTime = localStorage.getItem('lastActiveTimestamp');
+        if (currentTime - lastTime > idleTimeoutMs) {
+          //logout
+          sh.sendKeys('03'); // Ctrl + c
+          sh.sendKeys('04'); // Ctrl + d
+        } else {
+          setTimeout(checkIdleTimer, 1000);
+        }
+      }
+
       function login() {
         sh = new ShellInABox(host);
 
              sh.keysPressed(token + "\n");
              sh.vt100('(sent authentication token)\n');
              token = null;
+             updateIdleTimer();
+             document.body.onmousemove = updateIdleTimer;
+             document.body.onkeydown = updateIdleTimer;
+             setTimeout(checkIdleTimer, 1000);
           } else {
             setTimeout(trySendToken, 200);
           }
 
       function init() {
         if (token) {
-          history.replaceState(null, "", `/webshell/?host=${encodeURIComponent(host)}&login=${encodeURIComponent(user)}`);
+          history.replaceState(null, "", `/webshell/?host=${encodeURIComponent(host)}&timeout=${timeout}&login=${encodeURIComponent(user)}`);
         } else if (localStorage.getItem('apiToken')) {
           token = localStorage.getItem('apiToken');
         } else {
index 196cba1643606c86a2e3953a6ca154664d543847..c0f33644b781f3cc5498385b20b84ee4ee0e260b 100644 (file)
@@ -14,6 +14,7 @@ import { ListResults } from 'services/common-service/common-service';
 import { HelpIcon } from 'components/icon/icon';
 import { SESSION_STORAGE } from "services/auth-service/auth-service";
 // import * as CopyToClipboard from 'react-copy-to-clipboard';
+import parse from "parse-duration";
 
 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
 
@@ -67,6 +68,7 @@ const mapStateToProps = (state: RootState) => {
         token: state.auth.extraApiToken || state.auth.apiToken || '',
         tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
         webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
+        idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
         ...state.virtualMachines
     };
 };
@@ -84,8 +86,9 @@ interface VirtualMachinesPanelDataProps {
     helpText: string;
     hostSuffix: string;
     token: string;
-    tokenLocation: string,
+    tokenLocation: string;
     webshellUrl: string;
+    idleTimeout: number;
 }
 
 interface VirtualMachinesPanelActionProps {
@@ -192,7 +195,7 @@ const virtualMachinesTable = (props: VirtualMachineProps) =>
                                 {command}
                             </TableCell>
                             <TableCell>
-                                <a href={`/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&login=${encodeURIComponent(username)}${tokenParam}`} target="_blank" rel="noopener noreferrer" className={props.classes.link}>
+                                <a href={`/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`} target="_blank" rel="noopener noreferrer" className={props.classes.link}>
                                     Log in as {username}
                                 </a>
                             </TableCell>