From a2b52b761941f81ccf2e70537c6a035dab7e060e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 11 Feb 2017 00:19:17 -0500 Subject: [PATCH] cache directives, just a little bit --- oauth3.browser.js | 14 ++++++++++++-- oauth3.js | 20 +++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/oauth3.browser.js b/oauth3.browser.js index dfe1d84..92621ce 100644 --- a/oauth3.browser.js +++ b/oauth3.browser.js @@ -20,8 +20,19 @@ if (!providerUri) { throw new Error('oauth3.discover(providerUri, opts) received providerUri as ' + providerUri); } + var directives = OAUTH3.hooks.getDirectives(providerUri); + if (directives && directives.issuer) { + return OAUTH3.PromiseA.resolve(directives); + } + return browser._discoverHelper(providerUri, opts).then(function (directives) { + directives.issuer = directives.issuer || OAUTH3_CORE.normalizeUrl(providerUri); + console.log('discoverHelper', directives); + return OAUTH3.hooks.setDirectives(providerUri, directives); + }); + } + , _discoverHelper: function (providerUri, opts) { opts = opts || {}; - opts.debug = true; + //opts.debug = true; providerUri = OAUTH3_CORE.normalizeUrl(providerUri); if (window.location.hostname.match(providerUri)) { console.warn("It looks like you're a provider checking for your own directive," @@ -44,7 +55,6 @@ return OAUTH3_CORE.formatError(providerUri, params.error); } var directives = JSON.parse(atob(OAUTH3_CORE.utils.urlSafeBase64ToBase64(params.result || params.directives))); - directives.issuer = directives.issuer || OAUTH3_CORE.normalizeUrl(providerUri); return directives; }, function (err) { return OAUTH3.PromiseA.reject(err); diff --git a/oauth3.js b/oauth3.js index dde6dfa..388f914 100644 --- a/oauth3.js +++ b/oauth3.js @@ -136,6 +136,9 @@ return oauth3.PromiseA.resolve(oauth3.hooks.setSession(providerUri, oldSession)); } , setSession: function (providerUri, newSession) { + if (!providerUri) { + console.error(new Error('no providerUri').stack); + } providerUri = oauth3.core.normalizeUri(providerUri); console.warn('[oauth3.hooks.setSession] PLEASE IMPLEMENT -- Your Fault'); console.warn(newSession); @@ -149,6 +152,22 @@ if (!oauth3.hooks._sessions) { oauth3.hooks._sessions = {}; } return oauth3.hooks._sessions[providerUri]; } + , setDirectives: function (providerUri, directives) { + providerUri = oauth3.core.normalizeUri(providerUri); + console.warn('[oauth3.hooks.setDirectives] PLEASE IMPLEMENT -- Your Fault'); + console.warn(directives); + if (!oauth3.hooks._directives) { oauth3.hooks._directives = {}; } + window.localStorage.setItem('directives-' + providerUri, JSON.stringify(directives)); + oauth3.hooks._directives[providerUri] = directives; + return directives; + } + , getDirectives: function (providerUri) { + providerUri = oauth3.core.normalizeUri(providerUri); + console.warn('[oauth3.hooks.getDirectives] PLEASE IMPLEMENT -- Your Fault'); + if (!oauth3.hooks._directives) { oauth3.hooks._directives = {}; } + return JSON.parse(window.localStorage.getItem('directives-' + providerUri) || '{}'); + //return oauth3.hooks._directives[providerUri]; + } // Provider Only , setGrants: function (clientUri, newGrants) { @@ -243,7 +262,6 @@ client_id: providerUri , debug: opts.debug }).then(function (directive) { - console.log('core.urls.grants(directive, opts)', core.urls.grants(directive, opts)); return oauth3.request(core.urls.grants(directive, opts)).then(function (grantsResult) { if ('POST' === opts.method) { // TODO this is clientToken