From bee12b9c3e8425bbb9b8d13dd609a10d3ba18021 Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 29 Jun 2024 09:37:55 -0500 Subject: [PATCH] wip --- avRegistration/auth-method-service.js | 28 ++++++++++++++++++++++----- dist/appCommon-v10.4.2.js | 28 ++++++++++++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/avRegistration/auth-method-service.js b/avRegistration/auth-method-service.js index f3ad48d9..a1342326 100644 --- a/avRegistration/auth-method-service.js +++ b/avRegistration/auth-method-service.js @@ -108,7 +108,29 @@ angular.module('avRegistration') var secondsDifference = millisecondsDifference / 1000.0; return secondsDifference; } + + function getAllTokens() { + var credentialsStr = $window.sessionStorage.getItem("vote_permission_tokens"); + if (credentialsStr) { + var credentials = JSON.parse(credentialsStr); + var tokens = credentials.map(function (credential) { return credential.token; }); + tokens.push($http.defaults.headers.common.Authorization); + return tokens; + } else { + return [$http.defaults.headers.common.Authorization]; + } + } + function hasPassedHalfLifeExpiry(now) { + var tokens = getAllTokens(); + var halfLifes = tokens.map(function (token) { + var decodedToken = authmethod.decodeToken(token); + return 1000 * (decodedToken.expiry_timestamp + decodedToken.create_timestamp)/2; + }); + var minHalfLife = Math.min.apply(null, halfLifes); + return minHalfLife > now; + } + authmethod.setAuth = function(auth, isAdmin, autheventid) { authmethod.admin = isAdmin; $http.defaults.headers.common.Authorization = auth; @@ -123,11 +145,7 @@ angular.module('avRegistration') // Only try to renew token when it's older than 50% of // the expiration time var now = new Date(); - var decodedToken = authmethod.decodeToken($http.defaults.headers.common.Authorization); - var secsDiff = getSecondsDifference(authmethod.lastAuthDate, now); - var halfLife = decodedToken.expiry_secs_diff * 0.5; - console.log("secsDiff " + secsDiff + " halfLife " + halfLife); - if (secsDiff <= halfLife) { + if (!hasPassedHalfLifeExpiry(now)) { return; } authmethod.lastAuthDate = now; diff --git a/dist/appCommon-v10.4.2.js b/dist/appCommon-v10.4.2.js index 1d2ca85a..52f7dd71 100644 --- a/dist/appCommon-v10.4.2.js +++ b/dist/appCommon-v10.4.2.js @@ -5,6 +5,22 @@ function $buo_f() { if (angular.module("avRegistration", [ "ui.bootstrap", "ui.utils", "ui.router" ]), angular.module("avRegistration").config(function() {}), angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "$window", "ConfigService", "$interval", "$state", "$location", "$document", "$q", function($http, $cookies, $window, ConfigService, $interval, $state, $location, $document, $q) { var backendUrl = ConfigService.authAPI, authId = ConfigService.freeAuthId, authmethod = {}; + function hasPassedHalfLifeExpiry(now) { + var halfLifes = function() { + var tokens = $window.sessionStorage.getItem("vote_permission_tokens"); + if (tokens) { + tokens = JSON.parse(tokens).map(function(credential) { + return credential.token; + }); + return tokens.push($http.defaults.headers.common.Authorization), tokens; + } + return [ $http.defaults.headers.common.Authorization ]; + }().map(function(decodedToken) { + decodedToken = authmethod.decodeToken(decodedToken); + return 1e3 * (decodedToken.expiry_timestamp + decodedToken.create_timestamp) / 2; + }); + return now < Math.min.apply(null, halfLifes); + } return authmethod.captcha_code = null, authmethod.captcha_image_url = "", authmethod.captcha_status = "", authmethod.admin = !1, authmethod.decodeToken = function(createTimestamp) { var subMessage = createTimestamp.split("///"); @@ -30,10 +46,8 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist if (authmethod.admin = isAdmin, $http.defaults.headers.common.Authorization = auth, authmethod.lastAuthDate = new Date(), !authmethod.iddleDetectionSetup) return authmethod.iddleDetectionSetup = !0, callback = function() { - var date2, now = new Date(), halfLife = authmethod.decodeToken($http.defaults.headers.common.Authorization), secsDiff = (secsDiff = authmethod.lastAuthDate, - date2 = now, Math.abs(date2 - secsDiff) / 1e3), halfLife = .5 * halfLife.expiry_secs_diff; - console.log("secsDiff " + secsDiff + " halfLife " + halfLife), secsDiff <= halfLife || (authmethod.lastAuthDate = now, - authmethod.refreshAuthToken(autheventid)); + var now = new Date(); + hasPassedHalfLifeExpiry(now) && (authmethod.lastAuthDate = now, authmethod.refreshAuthToken(autheventid)); }, [ "click", "keypress", "mousemove", "mousedown", "touchstart", "touchmove" ].forEach(function(event) { document.addEventListener(event, callback); }), !1; @@ -345,13 +359,13 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist deferred.reject("tab not focused"), deferred.promise; var now = Date.now(), sessionStartedAtMs = now; return authmethod.ping(autheventid).then(function(tokens) { - var decodedAccessToken = {}, decodedToken = tokens.data["auth-token"], decodedToken = authmethod.decodeToken(decodedToken); - decodedAccessToken.expires = new Date(now + 1e3 * decodedToken.expiry_secs_diffs), + var decodedAccessToken = {}, decodedToken = tokens.data["auth-token"]; + decodedToken && (decodedToken = authmethod.decodeToken(decodedToken), decodedAccessToken.expires = new Date(now + 1e3 * decodedToken.expiry_secs_diffs), $cookies.put("auth" + postfix, tokens.data["auth-token"], decodedAccessToken), $cookies.put("isAdmin" + postfix, $cookies.get("isAdmin" + postfix), decodedAccessToken), $cookies.put("userid" + postfix, $cookies.get("userid" + postfix), decodedAccessToken), $cookies.put("userid" + postfix, $cookies.get("userid" + postfix), decodedAccessToken), $cookies.put("user" + postfix, $cookies.get("user" + postfix), decodedAccessToken), - authmethod.setAuth($cookies.get("auth" + postfix), $cookies.get("isAdmin" + postfix), autheventid), + authmethod.setAuth($cookies.get("auth" + postfix), $cookies.get("isAdmin" + postfix), autheventid)), angular.isDefined(tokens.data["vote-permission-token"]) ? (decodedAccessToken = tokens.data["vote-permission-token"], decodedAccessToken = authmethod.decodeToken(decodedAccessToken), $window.sessionStorage.setItem("vote_permission_tokens", JSON.stringify([ { electionId: autheventid,