angular.module('egCoreMod')
.factory('egAuth',
- ['$q','egNet','egHatch', function($q, egNet, egHatch) {
+ ['$q','$timeout','$rootScope','egNet','egHatch',
+function($q , $timeout , $rootScope , egNet , egHatch) {
var service = {
// the currently active user (au) object
return egHatch.getLocalItem('eg.auth.token');
},
+ // authtime in seconds
+ authtime : function() {
+ return egHatch.getLocalItem('eg.auth.time');
+ },
+
// the currently active workstation name
// For ws_ou or wsid(), see egAuth.user().ws_ou(), etc.
workstation : function() {
if (user && user.classname) {
// authtoken test succeeded
service._user = user;
+ service.poll();
if (user.wsid()) {
// user previously logged in with a workstation.
function(evt) {
if (evt.textcode == 'SUCCESS') {
service.ws = args.workstation;
+ service.poll();
egHatch.setLocalItem(
'eg.auth.token', evt.payload.authtoken);
+ egHatch.setLocalItem(
+ 'eg.auth.time', evt.payload.authtime);
deferred.resolve();
} else {
// note: the likely outcome here is a NO_SESION
return deferred.promise;
};
+ /**
+ * Force-check the validity of the authtoken on occasion.
+ * This allows us to redirect an idle staff client back to the login
+ * page after the session times out. Otherwise, the UI would stay
+ * open with potentially sensitive data visible.
+ * TODO: What is the practical difference (for a browser) between
+ * checking auth validity and the ui.general.idle_timeout setting?
+ * Does that setting serve a purpose in a browser environment?
+ */
+ service.poll = function() {
+ if (!service.authtime()) return;
+
+ $timeout(
+ function() {
+ if (!service.authtime()) return;
+ egNet.request(
+ 'open-ils.auth',
+ 'open-ils.auth.session.retrieve', service.token())
+ .then(function(user) {
+ if (user && user.classname) { // all good
+ service.poll();
+ } else {
+ $rootScope.$broadcast('egAuthExpired')
+ }
+ })
+ },
+ // add a 5 second delay to give the token plenty of time
+ // to expire on the server.
+ service.authtime() * 1000 + 5000
+ );
+ }
+
service.logout = function() {
if (service.token()) {
egNet.request(
'open-ils.auth.session.delete',
service.token()); // fire and forget
egHatch.removeLocalItem('eg.auth.token');
+ egHatch.removeLocalItem('eg.auth.time');
}
service._user = null;
};