From 4d4d1af45d10a26422c5715edf1cfdb57c5dead1 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Wed, 8 Nov 2017 19:03:28 -0700 Subject: [PATCH] cleaned up all uses of `_getCached` on hooks the _getCached function disappeared when the hooks were re-written, but some of the places that still relied on the old way of things still survived --- oauth3.core.js | 86 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/oauth3.core.js b/oauth3.core.js index 8d178d7..8e98e6d 100644 --- a/oauth3.core.js +++ b/oauth3.core.js @@ -717,7 +717,9 @@ } else { // Discovery must take place before calling implicitGrant - promise = OAUTH3._implicitGrant(OAUTH3.hooks.directives._getCached(providerUri), opts); + promise = OAUTH3.hooks.directives.get(providerUri).then(function (directives) { + return OAUTH3._implicitGrant(directives, opts); + }); } return promise.then(function (tokens) { @@ -815,9 +817,11 @@ }); } , logout: function(providerUri, opts) { - return OAUTH3._logoutHelper(OAUTH3.hooks.directives._getCached(providerUri), opts); + return OAUTH3.hooks.directives.get(providerUri).then(function (directives) { + return OAUTH3._logoutHelper(directives, opts); + }); } - , _logoutHelper: function(directives, opts) { + , _logoutHelper: function(providerUri, directives, opts) { var logoutReq = OAUTH3.urls.logout( directives , { client_id: (opts.client_id || opts.client_uri || OAUTH3.clientUri(OAUTH3._browser.window.location)) @@ -843,7 +847,7 @@ return OAUTH3.PromiseA.reject(OAUTH3.error.parse(directives.issuer /*providerUri*/, params)); } - OAUTH3.hooks.session.clear(); + OAUTH3.hooks.session.clear(providerUri); return params; }); } @@ -1390,22 +1394,24 @@ } , login: function (opts) { var me = this; - if (me.session()) { - me._session = true; - return OAUTH3.PromiseA.resolve(me.session()); - } + return OAUTH3.hooks.session.get(me._identityProviderUri).then(function (session) { + if (session) { + me._session = true; + return session; + } - opts = opts || {}; - opts.client_uri = me._clientUri; + opts = opts || {}; + opts.client_uri = me._clientUri; - return OAUTH3.implicitGrant(me._identityProviderDirectives, opts).then(function (session) { - me._session = true; - return session; + return OAUTH3.implicitGrant(me._identityProviderDirectives, opts).then(function (session) { + me._session = true; + return session; + }); }); } , session: function (session, id) { if (!session) { - return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._identityProviderUri) || null)); + return OAUTH3.hooks.session.get(this._identityProviderUri); } return OAUTH3.hooks.session.set(this._identityProviderUri, session, id); } @@ -1414,29 +1420,37 @@ preq.client_uri = this._clientUri; preq.client_id = this._clientUri; preq.method = preq.method || 'GET'; - if (this._session) { - preq.session = preq.session || this.session(); // OAUTH3.hooks.session._getCached(this._identityProviderUri); - } // TODO maybe use a baseUrl from the directives file? preq.url = OAUTH3.url.resolve(this._resourceProviderUri, preq.url); - return OAUTH3.request(preq, opts); + if (preq.session || !this._session) { + return OAUTH3.request(preq, opts); + } + + return this.session().then(function (session) { + preq.session = session; + return OAUTH3.request(preq, opts); + }); } , logout: function (opts) { this._session = false; opts = opts || {}; - opts.client_uri = this._clientUri; - opts.client_id = this._clientUri; - opts.session = OAUTH3.hooks.session._getCached(this._identityProviderUri); + return OAUTH3.hooks.session.get(this._identityProviderUri).then(function (session) { + opts.client_uri = this._clientUri; + opts.client_id = this._clientUri; + opts.session = session; - return OAUTH3.logout(this._identityProviderUri, opts); + return OAUTH3.logout(this._identityProviderUri, opts); + }); } , api: function (api, opts) { opts = opts || {}; - opts.api = api; - opts.session = OAUTH3.hooks.session._getCached(this._identityProviderUri); + return OAUTH3.hooks.session.get(this._identityProviderUri).then(function (session) { + opts.api = api; + opts.session = session; - return OAUTH3.api(this._resourceProviderDirectives.api, opts); + return OAUTH3.api(this._resourceProviderDirectives.api, opts); + }); } , pkg: function (pkgname) { var me = this; @@ -1450,17 +1464,19 @@ return OAUTH3.PromiseA.reject(new Error("No Package for '" + pkgname + "'")); } - pkg = OAUTH3._pkgs[pkgname]; - Object.keys(pkg).forEach(function (key) { - result[key] = function (opts) { - opts = opts || {}; - opts.session = OAUTH3.hooks.session._getCached(issuer); - opts.audience = audience; - return pkg[key](opts); - }; - }); + return OAUTH3.hooks.session.get(issuer).then(function (session) { + pkg = OAUTH3._pkgs[pkgname]; + Object.keys(pkg).forEach(function (key) { + result[key] = function (opts) { + opts = opts || {}; + opts.session = session; + opts.audience = audience; + return pkg[key](opts); + }; + }); - return result; + return result; + }); } }; result.setIssuer = result.setIdentityProvider;