;(function (exports) { 'use strict'; var OAUTH3 = exports.OAUTH3; var OAUTH3_CORE = exports.OAUTH3_CORE; function getDefaultAppUrl() { 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(/\/?$/, '') ; } var browser = exports.OAUTH3_BROWSER = { clientUri: function (location) { return OAUTH3_CORE.normalizeUri(location.host + location.pathname); } , discover: function (providerUri, opts) { if (!providerUri) { throw new Error('oauth3.discover(providerUri, opts) received providerUri as ' + providerUri); } opts = opts || {}; opts.debug = true; providerUri = OAUTH3_CORE.normalizeUrl(providerUri); 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); } 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); }); } , frameRequest: function (url, state, opts) { var promise; if ('background' === opts.type) { promise = browser.insertIframe(url, state, opts); } else if ('popup' === opts.type) { promise = browser.openWindow(url, state, opts); } else { throw new Error("login framing method not specified or not type yet implemented"); } return promise.then(function (params) { var err; if (params.error || params.error_description) { err = new Error(params.error_description || "Unknown response error"); err.code = params.error || "E_UKNOWN_ERROR"; err.params = params; return OAUTH3.PromiseA.reject(err); } return params; }); } , insertIframe: function (url, state, opts) { opts = opts || {}; var promise = new OAUTH3.PromiseA(function (resolve, reject) { var tok; var iframeDiv; function cleanup() { delete window['--oauth3-callback-' + state]; iframeDiv.remove(); clearTimeout(tok); tok = null; } window['--oauth3-callback-' + state] = function (params) { resolve(params); cleanup(); }; tok = setTimeout(function () { var err = new Error("the iframe request did not complete within 15 seconds"); err.code = "E_TIMEOUT"; reject(err); cleanup(); }, opts.timeout || 15000); // TODO hidden / non-hidden (via directive even) var framesrc = '