Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Findeton committed Jun 29, 2024
1 parent dce045f commit bee12b9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
28 changes: 23 additions & 5 deletions avRegistration/auth-method-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 21 additions & 7 deletions dist/appCommon-v10.4.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("///");
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit bee12b9

Please sign in to comment.