WIP implicit grant complete

This commit is contained in:
AJ ONeal 2017-02-14 12:53:03 -07:00
parent 4dd07c9f80
commit ab4dfdce58
1 changed files with 38 additions and 82 deletions

View File

@ -171,7 +171,6 @@
var args = directive[type];
var uri = args.url;
var state = opts.state || OAUTH3.utils.randomState();
console.log('implicit grant opts.state', opts.state);
var params = {
debug: opts.debug || undefined
, client_uri: opts.client_uri || opts.clientUri || undefined
@ -247,7 +246,7 @@
if (directives && directives.issuer) {
return directives;
}
return OAUTH3._discover(providerUri, opts).then(function (directives) {
return OAUTH3._discoverHelper(providerUri, opts).then(function (directives) {
directives.issuer = directives.issuer || OAUTH3.utils.url.normalize(providerUri);
// OAUTH3.PromiseA.resolve() is taken care of because this is wrapped
return OAUTH3.hooks.directives.set(providerUri, directives);
@ -285,19 +284,21 @@
}
, _discoverThenImplicitGrant: function(providerUri, opts) {
opts.windowType = opts.windowType || 'popup';
return OAUTH3._discover(providerUri, opts).then(function (directives) {
console.info('discover complete');
console.log(directives);
console.log('DISCOVER COMPLETE opts._state', opts._state);
return OAUTH3.discover(providerUri, opts).then(function (directives) {
console.info('Discover complete');
return OAUTH3._implicitGrant(directives, opts).then(function (tokens) {
console.info('implicit grant complete', tokens);
console.info('Implicit Grant complete', tokens);
OAUTH3._browser.closeFrame(tokens.state || opts._state);
//opts._state = undefined;
return tokens;
});
});
}
, _discoverHelper: function(providerUri, opts) {
return OAUTH3._discover(providerUri, opts);
}
, _discover: function(providerUri, opts) {
opts = opts || {};
providerUri = OAUTH3.utils.url.normalize(providerUri);
if (providerUri.match(OAUTH3._browser.window.location.hostname)) {
@ -334,18 +335,29 @@
// TODO allow postMessage from providerUri in addition to callback
// TODO allow node to open a desktop browser window
opts._windowType = opts.windowType;
opts.windowType = opts.windowType || 'background';
return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(providerUri, discReq.url)
, discReq.state
// why not just pass opts whole?
, { windowType: opts.windowType
, reuseWindow: opts.broker && '-broker'
, debug: opts.debug
}
).then(function (params) {
// discWin.child.close()
opts.windowType = opts._windowType;
// caller will call OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || params.debug });
if (params.error) {
// TODO directives.issuer || directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(providerUri, params));
}
// TODO params should have response_type indicating json, binary, etc
var directives = JSON.parse(OAUTH3.utils.atob(OAUTH3.utils._urlSafeBase64ToBase64(params.result || params.directives)));
return OAUTH3.hooks.directives.set(providerUri, directives);
// caller will call OAUTH3.hooks.directives.set(providerUri, directives);
return directives;
});
}
, _implicitGrant: function(directives, opts) {
@ -365,32 +377,24 @@
window.alert("DEBUG MODE: Pausing so you can look at logs and whatnot :) Fire at will!");
}
if (opts._state) {
console.log('equal states authReq?', authReq.state === opts._state);
console.log(opts._state);
console.log(authReq.state);
}
console.log("framing request for implicit grant");
return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(directives.issuer, authReq.url)
, authReq.state // state should recycle params
, { windowType: opts.windowType
, reuseWindow: opts.broker && '-broker'
, debug: opts.debug
}
).then(function (tokens) {
console.log("completed implicit grant");
if (tokens.error) {
// TODO directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(directives.issuer /*providerUri*/, tokens));
}
return new OAUTH3.PromiseA(function (resolve, reject) {
console.log("framing request for implicit grant");
return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(directives.issuer, authReq.url)
, authReq.state // state should recycle params
, { windowType: opts.windowType
, reuseWindow: opts.broker && '-broker'
, debug: opts.debug
}
).then(function (tokens) {
console.log("completed implicit grant");
if (tokens.error) {
// TODO directives.audience
return reject(OAUTH3.utils._formatError(directives.issuer /*providerUri*/, tokens));
}
OAUTH3._browser.closeFrame(authReq.state, { debug: opts.debug || tokens.debug });
OAUTH3._browser.closeFrame(authReq.state, { debug: opts.debug || tokens.debug });
return tokens;
});
return tokens;
});
}
@ -441,55 +445,6 @@
xhr.send();
});
}
, discover: function (providerUri, opts) {
opts = opts || {};
//opts.debug = true;
providerUri = OAUTH3.utils.url.normalize(providerUri);
if (providerUri.match(OAUTH3._browser.window.location.hostname)) {
console.warn("It looks like you're a provider checking for your own directive,"
+ " so we we're just gonna use OAUTH3.request({ method: 'GET', url: '.well-known/oauth3/directive.json' })");
return OAUTH3.request({
method: 'GET'
, url: OAUTH3.utils.url.normalize(providerUri) + '/.well-known/oauth3/directives.json'
});
}
if (!(opts.client_id || opts.client_uri).match(OAUTH3._browser.window.location.hostname)) {
console.warn("It looks like your client_id doesn't match your current window... this probably won't end well");
console.warn(opts.client_id || opts.client_uri, OAUTH3._browser.window.location.hostname);
}
var discReq = OAUTH3.urls.discover(
providerUri
, { client_id: (opts.client_id || opts.client_uri || OAUTH3.clientUri(OAUTH3._browser.window.location))
, state: opts._state || undefined
, debug: opts.debug }
);
if (opts._state) {
console.log('equal states discObj?', discReq.state === opts._state);
}
// TODO ability to reuse iframe instead of closing
opts._windowType = opts.windowType;
opts.windowType = opts.windowType || 'background';
return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(providerUri, discReq.url)
, discReq.state
, opts
).then(function (params) {
opts.windowType = opts._windowType;
OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || params.debug });
if (params.error) {
// TODO directives.issuer || directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(providerUri, params));
}
var directives = JSON.parse(OAUTH3.utils.atob(OAUTH3.utils._urlSafeBase64ToBase64(params.result || params.directives)));
return directives;
}, function (err) {
OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || err.debug });
return OAUTH3.PromiseA.reject(err);
});
}
, frameRequest: function (url, state, opts) {
console.log('frameRequest state', state);
opts = opts || {};
@ -592,6 +547,7 @@
});
}
, closeFrame: function (state, opts) {
opts = opts || {};
function close() {
console.log("Attempting to close... ", OAUTH3._browser._frames[state]);
try {