Skip to content

Commit

Permalink
Merge pull request #2327 from constantine2nd/develop
Browse files Browse the repository at this point in the history
Add endpoint suggestedSessionTimeout v5.1.0
  • Loading branch information
simonredfern authored Nov 16, 2023
2 parents 2759550 + e6502da commit fe3277b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
27 changes: 27 additions & 0 deletions obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,33 @@ trait APIMethods510 {
}
}
}

staticResourceDocs += ResourceDoc(
suggestedSessionTimeout,
implementedInApiVersion,
nameOf(suggestedSessionTimeout),
"GET",
"/ui/suggested-session-timeout",
"Get Suggested Session Timeout",
"""Returns information about:
|
|* Suggested session timeout in case of a user inactivity
""",
EmptyBody,
SuggestedSessionTimeoutV510("300"),
List(UnknownError),
apiTagApi :: Nil)

lazy val suggestedSessionTimeout: OBPEndpoint = {
case "ui" :: "suggested-session-timeout" :: Nil JsonGet _ =>
cc => implicit val ec = EndpointContext(Some(cc))
for {
timeout: Int <- Future(APIUtil.getPropsAsIntValue("session_inactivity_timeout_in_minutes", 5))
timeoutInSeconds = (timeout * 60).toString
} yield {
(SuggestedSessionTimeoutV510(timeoutInSeconds), HttpCode.`200`(cc.callContext))
}
}

staticResourceDocs += ResourceDoc(
waitingForGodot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import scala.collection.immutable.List
import scala.util.Try


case class SuggestedSessionTimeoutV510(timeout_in_seconds: String)
case class APIInfoJsonV510(
version : String,
version_status: String,
Expand Down
54 changes: 37 additions & 17 deletions obp-api/src/main/webapp/media/js/inactivity.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as countdownTimer from './inactivity-timer.js'

// holds the idle duration in ms (current value = 5 minutes)
var timeoutInterval = 5 * 60 * 1000;
// holds the idle duration in ms (current value = 301 seconds)
var timeoutIntervalInMillis = 5 * 60 * 1000 + 1000;
// holds the timeout variables for easy destruction and reconstruction of the setTimeout hooks
var timeHook = null;

function initializeTimeHook() {
// this method has the purpose of creating our timehooks and scheduling the call to our logout function when the idle time has been reached
if (timeHook == null) {
timeHook = setTimeout( function () { destroyTimeHook(); logout(); }.bind(this), timeoutInterval);
timeHook = setTimeout( function () { destroyTimeHook(); logout(); }.bind(this), timeoutIntervalInMillis);
}
}

Expand All @@ -18,43 +18,63 @@ function destroyTimeHook() {
timeHook = null;
}

function resetTimeHook() {
function resetTimeHook(event) {
// this method replaces the current time hook with a new time hook
destroyTimeHook();
initializeTimeHook();
countdownTimer.resetCountdownTimer(timeoutInterval / 1000);
countdownTimer.resetCountdownTimer(timeoutIntervalInMillis / 1000);
// show event type, element and coordinates of the click
// console.log(event.type + " at " + event.currentTarget);
// console.log("Coordinates: " + event.clientX + ":" + event.clientY);
console.log("Reset inactivity of a user");
}

function setupListeners() {
// here we setup the event listener for the mouse click operation
document.addEventListener("click", function () { resetTimeHook(); }.bind(this));
document.addEventListener("mousemove", function () { resetTimeHook(); }.bind(this));
document.addEventListener("mousedown", function () { resetTimeHook(); }.bind(this));
document.addEventListener("keypress", function () { resetTimeHook(); }.bind(this));
document.addEventListener("touchmove", function () { resetTimeHook(); }.bind(this));
document.addEventListener("click", resetTimeHook);
document.addEventListener("mousemove", resetTimeHook);
document.addEventListener("mousedown", resetTimeHook);
document.addEventListener("keypress", resetTimeHook);
document.addEventListener("touchmove", resetTimeHook);
console.log("Listeners for user inactivity activated");
}

function destroyListeners() {
// here we destroy event listeners for the mouse click operation
document.removeEventListener("click", function () { resetTimeHook(); }.bind(this));
document.removeEventListener("mousemove", function () { resetTimeHook(); }.bind(this));
document.removeEventListener("mousedown", function () { resetTimeHook(); }.bind(this));
document.removeEventListener("keypress", function () { resetTimeHook(); }.bind(this));
document.removeEventListener("touchmove", function () { resetTimeHook(); }.bind(this));
document.removeEventListener("click", resetTimeHook);
document.removeEventListener("mousemove", resetTimeHook);
document.removeEventListener("mousedown", resetTimeHook);
document.removeEventListener("keypress", resetTimeHook);
document.removeEventListener("touchmove", resetTimeHook);
console.log("Listeners for user inactivity deactivated");
}

function logout() {
destroyListeners();
countdownTimer.destroyCountdownTimer();
location.href = '/user_mgt/logout';
console.log("Logging you out due to inactivity..");
location.href = '/user_mgt/logout';
}

async function makeObpApiCall() {
let response = await fetch('/obp/v5.1.0/ui/suggested-session-timeout');
let json = await response.json();
return json.timeout_in_seconds;
}

function getSuggestedSessionTimeout() {
let timeoutInSeconds = localStorage.getItem("suggested-session-timeout-in-seconds");
if(!timeoutInSeconds) {
timeoutInSeconds = makeObpApiCall;
localStorage.setItem("suggested-session-timeout-in-seconds", timeoutInSeconds);
}
return timeoutInSeconds * 1000 + 1000; // We need timeout in millis
}

// self executing function to trigger the operation on page load
(function () {
timeoutIntervalInMillis = getSuggestedSessionTimeout();
console.log(timeoutIntervalInMillis);
const elem = document.getElementById("loggedIn-username");
if(elem) {
// to prevent any lingering timeout handlers preventing memory leaks
Expand All @@ -64,6 +84,6 @@ function logout() {
// setup initial event listeners
setupListeners();
// Reset countdown timer
countdownTimer.resetCountdownTimer(timeoutInterval / 1000);
countdownTimer.resetCountdownTimer(timeoutIntervalInMillis / 1000);
}
})();

0 comments on commit fe3277b

Please sign in to comment.