From c483058fa842adea07e0d0fb5fa432387881d8ea Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 10 Feb 2017 20:47:32 -0700 Subject: [PATCH] shortcut discover + authorization_dialog --- oauth3.browser.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/oauth3.browser.js b/oauth3.browser.js index 4875588..d589900 100644 --- a/oauth3.browser.js +++ b/oauth3.browser.js @@ -38,6 +38,63 @@ }); } + , discoverAuthorizationDialog: function(providerUri, opts) { + var discObj = OAUTH3.core.discover(providerUri, opts); + + // hmm... we're gonna need a broker for this since switching windows is distracting, + // popups are obnoxious, iframes are sometimes blocked, and most servers don't implement CORS + // eventually it should be the browser (and postMessage may be a viable option now), but whatever... + + // TODO allow postMessage from providerUri in addition to callback + var discWin = OAUTH3.openWindow(discObj.url, discObj.state, { reuseWindow: 'conquerer' }); + return discWin.then(function (params) { + console.log('discwin params'); + console.log(params); + // discWin.child + // TODO params should have response_type indicating json, binary, etc + var directives = JSON.parse(atob(OAUTH3.core.utils.urlSafeBase64ToBase64(params.result || params.directives))); + console.log('directives'); + console.log(directives); + + // Do some stuff + var authObj = OAUTH3.core.implicitGrant( + directives + , { redirect_uri: opts.redirect_uri + , debug: opts.debug + , client_id: opts.client_id || opts.client_uri + , client_uri: opts.client_uri || opts.client_id + } + ); + + if (params.debug) { + window.alert("DEBUG MODE: Pausing so you can look at logs and whatnot :) Fire at will!"); + } + + return new OAUTH3.PromiseA(function (resolve, reject) { + // TODO check if authObj.url is relative or full + discWin.child.location = OAUTH3.core.urls.resolve(providerUri, authObj.url); + + if (params.debug) { + discWin.child.focus(); + } + + window['--oauth3-callback-' + authObj.state] = function (tokens) { + if (tokens.error) { + return reject(OAUTH3.core.formatError(tokens.error)); + } + + if (params.debug || tokens.debug) { + if (window.confirm("DEBUG MODE: okay to close oauth3 window?")) { + discWin.child.close(); + } + } + + resolve(tokens); + }; + }); + }); + } + , frameRequest: function (url, state, opts) { var promise;