walnut_launchpad.html/js/services/auth-service.js

140 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2017-08-14 19:35:53 +00:00
app.factory('Auth', [
'$rootScope', 'localStorageService', '$location', 'azp@oauth3.org'
, function($rootScope, localStorageService, $location, Oauth3) {
2017-08-14 23:00:01 +00:00
var dapSession = 'dap-session';
var dapSessions = 'dap-sessions';
var Auth = {
isLoggedIn: function () {
Auth.restore();
return Auth.session || false;
2017-08-11 15:09:41 +00:00
},
2017-08-14 23:00:01 +00:00
getProfile: function (profile) {
Auth.restore();
return Auth.session || false;
2017-08-18 21:13:43 +00:00
},
getActiveSessions: function () {
Auth.restore();
return Auth.sessions || false;
2017-08-14 23:00:01 +00:00
}
, add: function (session) {
var obj = JSON.parse(localStorage.getItem(dapSessions) || 'null') || {};
var dapName = 'dap-' + session.subject + '|' + session.issuer;
2017-08-16 22:35:04 +00:00
/*
Object.keys(Auth.session).forEach(function (key) {
delete Auth.session[key];
});
Object.keys(session).forEach(function (key) {
Auth.session[key] = session[key];
});
*/
2017-08-14 23:00:01 +00:00
Auth.session = session;
Auth.sessions.push(session);
localStorage.setItem(dapName, JSON.stringify(session));
localStorage.setItem(dapSession, dapName);
2017-08-15 17:40:10 +00:00
obj[dapName] = Date.now();
2017-08-14 23:00:01 +00:00
localStorage.setItem(dapSessions, JSON.stringify(obj));
2017-08-09 18:41:43 +00:00
}
2017-08-14 23:00:01 +00:00
, restore: function () {
var dapName = localStorage.getItem(dapSession);
Auth.sessions.length = 0; // don't overwrite with a new array, keep original references
(Object.keys(JSON.parse(localStorage.getItem(dapSessions) || 'null') || {})).forEach(function (name) {
var session = JSON.parse(localStorage.getItem(name) || 'null');
if (session) {
session.email = session.subject;
}
2017-08-18 19:41:38 +00:00
if (!session.issuer) {
console.error(session);
throw new Error('restored session without audience');
}
2017-08-14 23:00:01 +00:00
if (dapName === name) {
Auth.session = session;
}
Auth.sessions.push(session);
});
if (Auth.session) {
Auth.select(Auth.session);
}
2017-08-14 23:00:01 +00:00
return Auth.session;
}
2017-08-23 18:54:50 +00:00
, get: function (session) {
if (!session) {
return $q.resolve(null);
}
2017-08-18 19:41:38 +00:00
if (!session.issuer) {
2017-08-21 17:21:00 +00:00
throw new Error("session doesn't have an issuer");
2017-08-18 19:41:38 +00:00
}
2017-08-16 22:35:04 +00:00
var name = session.token.sub + '@' + session.token.iss;
var promise;
2017-08-22 18:37:57 +00:00
var sess;
2017-08-16 22:35:04 +00:00
if (!Auth._oauth3s[name]) {
2017-08-22 18:37:57 +00:00
sess = Oauth3.create(window.location);
promise = Auth._oauth3s[name] = sess.init({
2017-08-16 22:35:04 +00:00
location: location
, issuer: session.issuer
, audience: session.audience || session.issuer
, session: session
}).then(function () {
2017-08-22 18:37:57 +00:00
return sess;
2017-08-16 22:35:04 +00:00
});
} else {
promise = Oauth3.PromiseA.resolve(Auth._oauth3s[name]);
}
2017-08-23 18:54:50 +00:00
return promise;
}
, select: function (session) {
return Auth.get(session).then(function (oauth3) {
var dapName = 'dap-' + session.subject + '|' + session.issuer;
localStorage.setItem(dapSession, dapName);
2017-08-18 20:12:22 +00:00
Auth.session = session;
Auth.oauth3 = oauth3;
2017-08-23 18:54:50 +00:00
});
2017-08-16 22:35:04 +00:00
}
, signOut: function () {
2017-08-15 17:40:10 +00:00
var session = Auth.session;
var dapName = 'dap-' + session.subject + '|' + session.issuer;
// TODO logout url should be created upon login and remain fixed throughout the duration of the session (or on session restoration)
return Auth.oauth3.logout().then(function () {
var obj = JSON.parse(localStorage.getItem(dapSessions) || '{}');
delete obj[dapName];
var newDapName = Object.keys(obj).sort(function (a, b) { return obj[a] - obj[b]; })[0];
localStorage.setItem(dapSession, newDapName);
localStorage.setItem(dapSessions, JSON.stringify(obj));
localStorage.removeItem(dapName);
if (!newDapName) {
localStorage.removeItem(dapSession);
}
return Auth.restore();
});
// localStorage.clear();
}
2017-08-16 22:35:04 +00:00
, _oauth3s: {}
2017-08-14 23:00:01 +00:00
, sessions: []
, session: null
2017-08-15 16:53:10 +00:00
, oauth3: null
2017-08-09 18:41:43 +00:00
};
2017-08-14 23:00:01 +00:00
2017-08-15 16:53:10 +00:00
Auth.oauth3 = Oauth3.create(window.location);
2017-08-14 23:00:01 +00:00
return Auth;
2017-08-09 04:23:19 +00:00
}]);