From a55aac6fc1baf7091c212f737115402223ca677b Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 16 Aug 2017 22:35:04 +0000 Subject: [PATCH] browse multiple sessions --- assets/oauth3.org | 2 +- js/controllers/login-controller.js | 8 ++--- js/controllers/website-controller.js | 45 ++++++++++++++++++++++++++++ js/services/auth-service.js | 33 +++++++++++++++++++- templates/website.html | 4 +-- 5 files changed, 84 insertions(+), 8 deletions(-) diff --git a/assets/oauth3.org b/assets/oauth3.org index 9a7aa32..8961a4e 160000 --- a/assets/oauth3.org +++ b/assets/oauth3.org @@ -1 +1 @@ -Subproject commit 9a7aa3261f2ad97ebd436c8b1e86d168cf93023d +Subproject commit 8961a4e51962a21690b5e68af3abaad24be8207c diff --git a/js/controllers/login-controller.js b/js/controllers/login-controller.js index 599eb0f..af32c6d 100644 --- a/js/controllers/login-controller.js +++ b/js/controllers/login-controller.js @@ -81,11 +81,11 @@ app.controller('loginCtrl', [ }; vm.selectSession = function (session) { + vm.xauth = true; vm.session = session; - vm.oauth3.init({ - location: location - , issuer: session.issuer - , audience: session.audience || session.issuer + return Auth.select(session).then(function (oauth3) { + vm.xauth = false; + vm.oauth3 = oauth3; }); }; diff --git a/js/controllers/website-controller.js b/js/controllers/website-controller.js index f9f0ef2..0d8a097 100644 --- a/js/controllers/website-controller.js +++ b/js/controllers/website-controller.js @@ -4,6 +4,30 @@ app.controller('websiteCtrl', [ var vm = this; vm.oauth3 = Auth.oauth3; + vm.domains = []; + + Auth.sessions.forEach(function (session) { + return Auth.select(session).then(function (oauth3) { + return oauth3.api('domains.list', {}).then(function (domains) { + if (domains.error) { + // not all tokens support all apis + return; + } + if (!Array.isArray(domains)) { + console.error('domains are not domains'); + console.error(domains); + return; + } + + console.log('domains'); + console.log(domains); + domains.forEach(function (domain) { + domain.session = session.token.sub + '@' + session.token.iss; + vm.domains.push(domain); + }); + }); + }); + }); vm.getDomains = function () { vm.oauth3.api('domains.list', {}).then(function (result) { @@ -11,8 +35,28 @@ app.controller('websiteCtrl', [ }); }; + vm.setDomain = function () { + if (!vm.domains || !vm.domains.length) { + vm.domain = { domain: vm.newDomain }; + return; + } + + vm.domains.some(function (domain) { + if (domain.domain === vm.newDomain) { + vm.domain = domain; + return true; + } + }); + + if (!vm.domain) { + vm.domain = { domain: vm.newDomain }; + } + }; + vm.selectDomain = function (domain) { vm.domain = domain; + vm.newDomain = domain.domain; + /* return vm.oauth3.api('dns.list', { }).then(function (records) { records = records.filter(function (r) { return /^A(AAA)?$/i.test(r.type) && ((r.sld + '.' + r.tld) === domain || r.zone === domain.domain); @@ -21,6 +65,7 @@ app.controller('websiteCtrl', [ console.log('records'); console.log(records); }); + */ }; vm.selectRecord = function (record) { diff --git a/js/services/auth-service.js b/js/services/auth-service.js index 8afc60e..8b32517 100644 --- a/js/services/auth-service.js +++ b/js/services/auth-service.js @@ -23,8 +23,16 @@ app.factory('Auth', [ var obj = JSON.parse(localStorage.getItem(dapSessions) || 'null') || {}; var dapName = 'dap-' + session.subject + '|' + session.issuer; - console.log('session', session); + console.log('add session', session); + /* + Object.keys(Auth.session).forEach(function (key) { + delete Auth.session[key]; + }); + Object.keys(session).forEach(function (key) { + Auth.session[key] = session[key]; + }); + */ Auth.session = session; Auth.sessions.push(session); @@ -53,6 +61,28 @@ app.factory('Auth', [ return Auth.session; } + , select: function (session) { + var name = session.token.sub + '@' + session.token.iss; + var promise; + + if (!Auth._oauth3s[name]) { + Auth._oauth3s[name] = Oauth3.create(window.location); + promise = Auth._oauth3s[name].init({ + location: location + , issuer: session.issuer + , audience: session.audience || session.issuer + , session: session + }).then(function () { + console.log('a Auth._oauth3s[name]', Auth._oauth3s[name]); + return Auth._oauth3s[name]; + }); + } else { + console.log('b Auth._oauth3s[name]', Auth._oauth3s[name]); + promise = Oauth3.PromiseA.resolve(Auth._oauth3s[name]); + } + + return promise; + } , signOut: function () { var session = Auth.session; var dapName = 'dap-' + session.subject + '|' + session.issuer; @@ -75,6 +105,7 @@ app.factory('Auth', [ }); // localStorage.clear(); } + , _oauth3s: {} , sessions: [] , session: null , oauth3: null diff --git a/templates/website.html b/templates/website.html index ae2d90c..0aaa7f8 100644 --- a/templates/website.html +++ b/templates/website.html @@ -14,14 +14,14 @@ . - +