From b1ddf92e764325fbe049e97722fd58a1b4a4c128 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 21 Feb 2017 14:18:41 -0700 Subject: [PATCH] add helpers --- oauth3.implicit.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/oauth3.implicit.js b/oauth3.implicit.js index 58253de..bc4ad35 100644 --- a/oauth3.implicit.js +++ b/oauth3.implicit.js @@ -930,4 +930,79 @@ OAUTH3.PromiseA = Promise; } + // this is not necessary, but it's relatively small + // and gives people the 3-line examples they love so much + OAUTH3.create = function (location/*, opts*/) { + if (!location) { + location = OAUTH3._browser.window.location; + } + + return { + _clientUri: OAUTH3.clientUri(location) + , _providerUri: null + , init: function (location) { + var me = this; + var p = OAUTH3.PromiseA.resolve(); + + if (location) { + this._clientUri = OAUTH3.clientUri(location); + } + if (this._providerUri) { + p = OAUTH3.discover(this._providerUri, { client_id: this._clientUri }).then(function (/*directives*/) { + $('.js-signin').removeAttr('disabled'); + }); + } + + return OAUTH3.discover(this._clientUri, { client_id: this._clientUri }).then(function (clientDirectives) { + me._clientDirectives = clientDirectives; + return p.then(function () { + return clientDirectives; + }); + }); + } + , setProvider: function (providerUri) { + var me = this; + me._providerUri = providerUri; + return me.init().then(function () { + // this should be synchronous the second time around + return OAUTH3.discover(me._providerUri, { client_id: me._clientUri }).then(function (directives) { + console.log("setProvider", directives); + me._providerDirectives = directives; + return directives; + }); + }); + } + , login: function (opts) { + var me = this; + opts = opts || {}; + opts.client_uri = me._clientUri; + + console.log('login', me._providerDirectives); + return OAUTH3.implicitGrant(me._providerDirectives, opts).then(function (session) { + me._session = true; + return session; + }); + } + , session: function () { + return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._providerUri))); + } + , request: function (preq) { + preq.client_uri = this._clientUri; + preq.client_id = this._clientUri; + if (this._session) { + preq.session = preq.session || OAUTH3.hooks.session._getCached(this._providerUri); + } + return OAUTH3.request(preq); + } + , logout: function (opts) { + opts = opts || {}; + opts.client_uri = this._clientUri; + opts.client_id = this._clientUri; + opts.session = OAUTH3.hooks.session._getCached(this._providerUri); + + return OAUTH3.logout(this._providerUri, opts); + } + }; + }; + }('undefined' !== typeof exports ? exports : window));