From fbce24e14acc2dca67e2ee5c55dc13cdbb6c5d54 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 10 Feb 2017 20:23:57 -0700 Subject: [PATCH] bugfixes --- oauth3.browser.js | 33 +++++++++++++++++++-------------- oauth3.core.js | 9 +++++---- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/oauth3.browser.js b/oauth3.browser.js index 7cc00f1..77f8a2b 100644 --- a/oauth3.browser.js +++ b/oauth3.browser.js @@ -5,7 +5,7 @@ var OAUTH3_CORE = exports.OAUTH3_CORE; function getDefaultAppUrl() { - console.warn('[deprecated] using window.location.{protocol, host, pathname} when opts.appUrl should be used'); + console.warn('[deprecated] using window.location.{protocol, host, pathname} when opts.client_id should be used'); return window.location.protocol + '//' + window.location.host + (window.location.pathname).replace(/\/?$/, '') @@ -23,8 +23,9 @@ opts = opts || {}; opts.debug = true; providerUri = OAUTH3_CORE.normalizeUrl(providerUri); - var discObj = OAUTH3_CORE.urls.discover(providerUri, { appUrl: (opts.appUrl || getDefaultAppUrl()), debug: opts.debug }); + var discObj = OAUTH3_CORE.urls.discover(providerUri, { client_id: (opts.client_id || opts.client_uri || getDefaultAppUrl()), debug: opts.debug }); + // TODO ability to reuse iframe instead of closing return browser.insertIframe(discObj.url, discObj.state, opts).then(function (params) { if (params.error) { return OAUTH3_CORE.formatError(providerUri, params.error); @@ -108,7 +109,6 @@ , openWindow: function (url, state, opts) { var promise = new OAUTH3.PromiseA(function (resolve, reject) { - var winref; var tok; function cleanup() { @@ -117,10 +117,13 @@ tok = null; // this is last in case the window self-closes synchronously // (should never happen, but that's a negotiable implementation detail) - //winref.close(); + if (!opts.reuseWindow) { + promise.child.close(); + } } window['--oauth3-callback-' + state] = function (params) { + console.log('YOLO!!'); resolve(params); cleanup(); }; @@ -132,18 +135,20 @@ cleanup(); }, opts.timeout || 3 * 60 * 1000); - // TODO allow size changes (via directive even) - winref = window.open( - url - , 'oauth3-login-' + state - , 'height=' + (opts.height || 720) + ',width=' + (opts.width || 620) - ); - if (!winref) { - reject("TODO: open the iframe first and discover oauth3 directives before popup"); - cleanup(); - } + setTimeout(function () { + if (!promise.child) { + reject("TODO: open the iframe first and discover oauth3 directives before popup"); + cleanup(); + } + }, 0); }); + // TODO allow size changes (via directive even) + promise.child = window.open( + url + , 'oauth3-login-' + (opts.reuseWindow || state) + , 'height=' + (opts.height || 720) + ',width=' + (opts.width || 620) + ); // TODO periodically garbage collect expired handlers from window object return promise; } diff --git a/oauth3.core.js b/oauth3.core.js index 9845a21..39b96fd 100644 --- a/oauth3.core.js +++ b/oauth3.core.js @@ -210,14 +210,14 @@ if (!providerUri) { throw new Error("cannot discover without providerUri"); } - if (!opts.appUrl) { - throw new Error("cannot discover without opts.appUrl"); + if (!opts.client_id) { + throw new Error("cannot discover without options.client_id"); } var params = { action: 'directives' , state: core.utils.randomState() - , redirect_uri: opts.appUrl + (opts.appCallbackPath || '/.well-known/oauth3/callback.html') + , redirect_uri: opts.client_id + (opts.client_callback_path || '/.well-known/oauth3/callback.html') , response_type: 'rpc' , _method: 'GET' , _pathname: '.well-known/oauth3/directives.json' @@ -336,7 +336,8 @@ var redirectUri = opts.redirectUri; var scope = opts.scope || directive.authn_scope; - var clientId = opts.appId || opts.clientId || opts.clientUri; + var clientId = core.normalizeUri(opts.client_id || opts.client_uri + || opts.appId || opts.clientId || opts.clientUri); var args = directive[type]; var uri = args.url; var state = core.utils.randomState();