Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Countdown until logout #314

Merged
merged 37 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions SequentConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ var SequentConfigData = {
// Default: '/admin/login'
defaultRoute: '/admin/login',

timeoutSeconds: 3600,

publicURL: "https://sequent/elections/public/",

// if we are in debug mode or not
Expand Down Expand Up @@ -233,7 +231,10 @@ var SequentConfigData = {
},

mainVersion: "master",
repoVersions: []
repoVersions: [],

// Number of seconds after which an authentication token expires.
authTokenExpirationSeconds: 600
};

angular.module('SequentConfig', [])
Expand Down
8 changes: 4 additions & 4 deletions avRegistration/auth-method-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ angular.module('avRegistration')
authmethod.launchPingDaemon(autheventid);
authmethod.pingTimeout = $interval(
function() { authmethod.launchPingDaemon(autheventid); },
ConfigService.timeoutSeconds*500 // ms * 500 mean seconds * 1/2
ConfigService.authTokenExpirationSeconds*500 // renew token when 50% of the expiration time has passed
);
}
return false;
Expand Down Expand Up @@ -724,12 +724,12 @@ angular.module('avRegistration')
}
return;
}
var now = Date.now();
authmethod.ping()
.then(function(response) {
var options = {};
if (ConfigService.cookies && ConfigService.cookies.expires) {
options.expires = new Date();
options.expires.setMinutes(options.expires.getMinutes() + ConfigService.cookies.expires);
if (ConfigService.authTokenExpirationSeconds) {
options.expires = new Date(now + 1000 * ConfigService.authTokenExpirationSeconds);
}
// update cookies expiration
$cookies.put(
Expand Down
13 changes: 8 additions & 5 deletions avRegistration/login-directive/login-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,17 @@ angular.module('avRegistration')

scope.sendingData = true;
scope.error = null;

var sessionStartedAtMs = Date.now();
Authmethod
.login(data, autheventid)
.then(
function onSuccess(response) {
if (response.data.status === "ok") {
var postfix = "_authevent_" + autheventid;
var options = {};
if (ConfigService.cookies && ConfigService.cookies.expires) {
options.expires = new Date();
options.expires.setMinutes(options.expires.getMinutes() + ConfigService.cookies.expires);
if (ConfigService.authTokenExpirationSeconds) {
options.expires = new Date(Date.now() + 1000 * ConfigService.authTokenExpirationSeconds);
}
$cookies.put("authevent_" + autheventid, autheventid, options);
$cookies.put("userid" + postfix, response.data.username, options);
Expand Down Expand Up @@ -409,7 +410,8 @@ angular.module('avRegistration')
JSON.stringify([{
electionId: autheventid,
token: response.data['vote-permission-token'],
isFirst: true
isFirst: true,
sessionStartedAtMs: sessionStartedAtMs
}])
);
$window.sessionStorage.setItem(
Expand All @@ -432,7 +434,8 @@ angular.module('avRegistration')
voted: false,
numSuccessfulLoginsAllowed: child['num-successful-logins-allowed'],
numSuccessfulLogins: child['num-successful-logins'],
isFirst: index === 0
isFirst: index === 0,
sessionStartedAtMs: sessionStartedAtMs
};
})
.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,8 @@ angular.module('avRegistration')
};

var options = {};
if (ConfigService.cookies && ConfigService.cookies.expires) {
options.expires = new Date();
options.expires.setMinutes(options.expires.getMinutes() + ConfigService.cookies.expires);
if (ConfigService.authTokenExpirationSeconds) {
options.expires = new Date(Date.now() + 1000 * ConfigService.authTokenExpirationSeconds);
}

var postfix = "_authevent_" + scope.csrf.eventId;
Expand Down
37 changes: 29 additions & 8 deletions avUi/common-header-directive/common-header-directive.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,37 @@
av-change-lang
>
</span>
<a
<span class="logout-container"
ng-if="enableLogOut() && !isElectionPortal"
target="_top"
tabindex="0"
class="log-out-button"
ng-click="confirmLogoutModal()"
ng-class="{ 'countdown': showCountdown}"
>
<span class="glyphicon glyphicon-off"></span>
<span class="logout-text hidden-xs" ng-i18next>avBooth.logout</span>
</a>
<a
target="_top"
tabindex="0"
class="log-out-button"
ng-click="confirmLogoutModal()"
>
<div class="logout-bar"></div>
<span class="glyphicon glyphicon-off"></span>
<span class="logout-text hidden-xs" ng-i18next>avBooth.logout</span>
</a>
<div class="custom-tooltip">
<i class="fa fa-fw fa-lg fa-caret-up"></i>
<div class="tooltip-inner">
<b ng-i18next>avBooth.countdownTooltip.title</b>
<p
ng-if="countdownSecs >= 60"
ng-i18next="[i18next]({mins: countdownMins})avBooth.countdownTooltip.contentMins"
>
</p>
<p
ng-if="countdownSecs < 60"
ng-i18next="[i18next]({secs: countdownSecs})avBooth.countdownTooltip.contentSecs"
>
</p>
</div>
</div>
</span>
</div>
</div>
</nav>
Expand Down
83 changes: 83 additions & 0 deletions avUi/common-header-directive/common-header-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,89 @@ angular
};

scope.showVersionsModal = ShowVersionsModalService;

function calculateCountdownPercent() {
var ratio = (scope.logoutTimeMs - Date.now())/(scope.logoutTimeMs - scope.countdownStartTimeMs);
return Math.min(100, Math.round(10000*ratio)/100) + '%';
}

// find progress bar and update its width
function updateProgressBar(percent) {
var element = $(".logout-bar")[0];
if (!element) {
// There's no logout on the login page
return;
}
element.style.setProperty('width', percent);
}

// helper function for enableLogoutCountdown()
function updateTimedown() {
scope.showCountdown = true;
var now = Date.now();
scope.countdownSecs = Math.round((scope.logoutTimeMs - now) / 1000);
scope.countdownMins = Math.round((scope.logoutTimeMs - now) / (60 * 1000));
scope.countdownPercent = calculateCountdownPercent();
updateProgressBar(scope.countdownPercent);
scope.$apply();
if (scope.countdownSecs <= 1) {
return;
}
var targetMins = Math.floor((scope.logoutTimeMs - now) / (60 * 1000));
var targetNextTime = scope.logoutTimeMs - targetMins * 60 * 1000;
var targetElapsedTime = targetNextTime - now;
setTimeout(
updateTimedown,
targetMins > 0? targetElapsedTime : 1000
);
}

// Show countdown on logout button based on cookies
function enableLogoutCountdown() {
scope.showCountdown = false;

var election = (
(!!scope.parentElection) ?
scope.parentElection :
scope.election
);

if (
ConfigService.authTokenExpirationSeconds &&
(
election &&
election.presentation &&
_.isNumber(election.presentation.booth_log_out__countdown_seconds)
)
) {
scope.showCountdown = false;
scope.countdownSecs = 0;
scope.countdownMins = 0;

var initialTimeMs = scope.$parent.getSessionStartTime && scope.$parent.getSessionStartTime() || Date.now();
scope.elapsedCountdownMs = (
election.presentation.booth_log_out__countdown_seconds > 0?
election.presentation.booth_log_out__countdown_seconds :
ConfigService.authTokenExpirationSeconds
) * 1000;
scope.logoutTimeMs = initialTimeMs + ConfigService.authTokenExpirationSeconds * 1000;
scope.countdownStartTimeMs = scope.logoutTimeMs - scope.elapsedCountdownMs;
scope.countdownPercent = calculateCountdownPercent();
updateProgressBar(scope.countdownPercent);

// If we're on a demo/live preview, the bar is fixed at 100%
if (scope.isDemo || scope.isPreview) {
return;
}

setTimeout(
updateTimedown,
election.presentation.booth_log_out__countdown_seconds > 0? scope.countdownStartTimeMs - Date.now() : 0
);

}
}
setTimeout(enableLogoutCountdown, 0);
};
return {
restrict: 'AE',
Expand Down
74 changes: 74 additions & 0 deletions avUi/common-header-directive/common-header-directive.less
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,80 @@
.log-out-button {
display: flex;
align-items: center;
position: relative;
}

.logout-bar {
position: absolute;
background-color: #CCE5FF;
border-bottom: 2px solid #0F054C;
border-radius: 4px 0px 0px 4px;
z-index: -1;
height: 100%;
left: 0;
}

.logout-container {
position: relative;

.custom-tooltip, .logout-bar {
display: none;
}
}

.logout-container.countdown {
.logout-bar {
display: block;
}

&:hover {
.custom-tooltip {
display: block;
}
}
}

.logout-container .custom-tooltip {
position: absolute;
top: 120%;
right: 40px;
z-index: -1;
width: 231px;

@media(max-width: @screen-xs-max) {
right: 0;

.fa-caret-up {
padding-right: 12px !important;
}
}

.fa-caret-up {
color: #CCE5FF;
font-size: 34px;
height: 14px;
line-height: 10px;
width: 100%;
text-align: right;
padding-right: 20px;
}

.tooltip-inner {
background-color: #CCE5FF;
margin-top: -9px;
font-size: 12px;
line-height: 15px;
padding: 16px;
gap: 8px;
display: flex;
flex-direction: column;
text-align: justify;
color: black;
max-width: unset;
b {
color: #0F054C;
}
}
}

.logout-text {
Expand Down
7 changes: 4 additions & 3 deletions dist/SequentConfig-vmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ var SequentConfigData = {
// Default: '/admin/login'
defaultRoute: '/admin/login',

timeoutSeconds: 3600,

publicURL: "https://sequent/elections/public/",

// if we are in debug mode or not
Expand Down Expand Up @@ -233,7 +231,10 @@ var SequentConfigData = {
},

mainVersion: "master",
repoVersions: []
repoVersions: [],

// Number of seconds after which an authentication token expires.
authTokenExpirationSeconds: 600
};

angular.module('SequentConfig', [])
Expand Down
Loading