This commit is contained in:
AJ ONeal 2017-02-21 14:18:09 -07:00
parent c356d61598
commit ae97b30b6f
1 changed files with 40 additions and 32 deletions

View File

@ -4,7 +4,7 @@
var OAUTH3 = exports.OAUTH3 = { var OAUTH3 = exports.OAUTH3 = {
clientUri: function (location) { clientUri: function (location) {
return OAUTH3.utils.uri.normalize(location.host + location.pathname); return OAUTH3.uri.normalize(location.host + location.pathname);
} }
, error: { , error: {
parse: function (providerUri, params) { parse: function (providerUri, params) {
@ -83,7 +83,7 @@
} }
if ('scope' === key) { if ('scope' === key) {
params[key] = OAUTH3.utils.scope.stringify(params[key]); params[key] = OAUTH3.scope.stringify(params[key]);
} }
qs.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key])); qs.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
@ -171,8 +171,8 @@
if (!opts.client_id) { if (!opts.client_id) {
throw new Error("cannot discover without options.client_id"); throw new Error("cannot discover without options.client_id");
} }
var clientId = OAUTH3.utils.url.normalize(opts.client_id || opts.client_uri); var clientId = OAUTH3.url.normalize(opts.client_id || opts.client_uri);
providerUri = OAUTH3.utils.url.normalize(providerUri); providerUri = OAUTH3.url.normalize(providerUri);
var params = { var params = {
action: 'directives' action: 'directives'
@ -185,7 +185,7 @@
}; };
var result = { var result = {
url: providerUri + '/.well-known/oauth3/#/?' + OAUTH3.utils.query.stringify(params) url: providerUri + '/.well-known/oauth3/#/?' + OAUTH3.query.stringify(params)
, state: params.state , state: params.state
, method: 'GET' , method: 'GET'
, query: params , query: params
@ -226,20 +226,20 @@
params.response_type = responseType; params.response_type = responseType;
if (scope) { if (scope) {
params.scope = OAUTH3.utils.scope.stringify(scope); params.scope = OAUTH3.scope.stringify(scope);
} }
if (!opts.redirect_uri) { if (!opts.redirect_uri) {
// TODO consider making this optional // TODO consider making this optional
//console.warn("auto-generating redirect_uri from hard-coded callback.html" //console.warn("auto-generating redirect_uri from hard-coded callback.html"
// + " (should be configurable... but then redirect_uri could just be manually-generated)"); // + " (should be configurable... but then redirect_uri could just be manually-generated)");
opts.redirect_uri = OAUTH3.utils.url.resolve( opts.redirect_uri = OAUTH3.url.resolve(
OAUTH3.utils.url.normalize(params.client_uri) OAUTH3.url.normalize(params.client_uri)
, '.well-known/oauth3/callback.html' , '.well-known/oauth3/callback.html'
); );
} }
params.redirect_uri = opts.redirect_uri; params.redirect_uri = opts.redirect_uri;
uri += '?' + OAUTH3.utils.query.stringify(params); uri += '?' + OAUTH3.query.stringify(params);
result = { result = {
url: uri url: uri
@ -287,11 +287,11 @@
} }
if (scope) { if (scope) {
params.scope = OAUTH3.utils.scope.stringify(scope); params.scope = OAUTH3.scope.stringify(scope);
} }
if ('GET' === args.method.toUpperCase()) { if ('GET' === args.method.toUpperCase()) {
uri += '?' + OAUTH3.utils.query.stringify(params); uri += '?' + OAUTH3.query.stringify(params);
} else { } else {
body = params; body = params;
} }
@ -326,8 +326,8 @@
, client_id: opts.client_id || opts.client_uri , client_id: opts.client_id || opts.client_uri
, client_uri: opts.client_uri || opts.client_id , client_uri: opts.client_uri || opts.client_id
, state: state , state: state
, redirect_uri: opts.redirect_uri = OAUTH3.utils.url.resolve( , redirect_uri: opts.redirect_uri = OAUTH3.url.resolve(
OAUTH3.utils.url.normalize(opts.client_uri || opts.client_id) OAUTH3.url.normalize(opts.client_uri || opts.client_id)
, '.well-known/oauth3/callback.html' , '.well-known/oauth3/callback.html'
) )
, debug: opts.debug , debug: opts.debug
@ -336,13 +336,13 @@
var body; var body;
if ('GET' === args.method.toUpperCase()) { if ('GET' === args.method.toUpperCase()) {
uri += '?' + OAUTH3.utils.query.stringify(params); uri += '?' + OAUTH3.query.stringify(params);
} else { } else {
body = params; body = params;
} }
return { return {
url: OAUTH3.utils.url.resolve(directive.issuer, uri) url: OAUTH3.url.resolve(directive.issuer, uri)
, method: args.method , method: args.method
, state: state , state: state
, data: body , data: body
@ -352,7 +352,7 @@
, hooks: { , hooks: {
directives: { directives: {
_get: function (providerUri) { _get: function (providerUri) {
providerUri = OAUTH3.utils.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; } if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; }
return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives._cache[providerUri] return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives._cache[providerUri]
|| OAUTH3.hooks.directives.get(providerUri)) || OAUTH3.hooks.directives.get(providerUri))
@ -363,7 +363,7 @@
}); });
} }
, _getCached: function (providerUri) { , _getCached: function (providerUri) {
providerUri = OAUTH3.utils.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
return OAUTH3.hooks.directives._cache[providerUri]; return OAUTH3.hooks.directives._cache[providerUri];
} }
, get: function (providerUri) { , get: function (providerUri) {
@ -371,7 +371,7 @@
return JSON.parse(window.localStorage.getItem('directives-' + providerUri) || '{}'); return JSON.parse(window.localStorage.getItem('directives-' + providerUri) || '{}');
} }
, _set: function (providerUri, directives) { , _set: function (providerUri, directives) {
providerUri = OAUTH3.utils.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; } if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; }
OAUTH3.hooks.directives._cache[providerUri] = directives; OAUTH3.hooks.directives._cache[providerUri] = directives;
return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives.set(providerUri, directives)); return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives.set(providerUri, directives));
@ -469,14 +469,18 @@
console.error(new Error('no providerUri').stack); console.error(new Error('no providerUri').stack);
throw new Error("providerUri is not set"); throw new Error("providerUri is not set");
} }
providerUri = OAUTH3.utils.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
console.warn('[Warn] Please implement OAUTH3.hooks.session.set = function (providerUri, newSession) { return PromiseA<newSession>; }'); console.warn('[Warn] Please implement OAUTH3.hooks.session.set = function (providerUri, newSession) { return PromiseA<newSession>; }');
if (!OAUTH3.hooks.session._sessions) { OAUTH3.hooks.session._sessions = {}; } if (!OAUTH3.hooks.session._sessions) { OAUTH3.hooks.session._sessions = {}; }
OAUTH3.hooks.session._sessions[providerUri] = newSession; OAUTH3.hooks.session._sessions[providerUri] = newSession;
return OAUTH3.PromiseA.resolve(newSession); return OAUTH3.PromiseA.resolve(newSession);
} }
, _getCached: function (providerUri) {
providerUri = OAUTH3.uri.normalize(providerUri);
return OAUTH3.hooks.session._sessions[providerUri];
}
, get: function (providerUri) { , get: function (providerUri) {
providerUri = OAUTH3.utils.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
if (!providerUri) { if (!providerUri) {
throw new Error("providerUri is not set"); throw new Error("providerUri is not set");
} }
@ -496,7 +500,8 @@
return directives; return directives;
} }
return OAUTH3._discoverHelper(providerUri, opts).then(function (directives) { return OAUTH3._discoverHelper(providerUri, opts).then(function (directives) {
directives.issuer = directives.issuer || OAUTH3.utils.url.normalize(providerUri); directives.azp = directives.azp || OAUTH3.url.normalize(providerUri);
directives.issuer = directives.issuer || OAUTH3.url.normalize(providerUri);
// OAUTH3.PromiseA.resolve() is taken care of because this is wrapped // OAUTH3.PromiseA.resolve() is taken care of because this is wrapped
return OAUTH3.hooks.directives._set(providerUri, directives); return OAUTH3.hooks.directives._set(providerUri, directives);
}); });
@ -517,6 +522,8 @@
return OAUTH3._requestHelper(preq, opts); return OAUTH3._requestHelper(preq, opts);
} }
OAUTH3.url.resolve(preq.providerUri || preq.provider_uri || preq.directives && preq.directives.issuer, preq.url);
if (!preq.session) { if (!preq.session) {
return fetch(); return fetch();
} }
@ -526,8 +533,9 @@
, _requestHelper: function (preq, opts) { , _requestHelper: function (preq, opts) {
return OAUTH3._browser.request(preq, opts); return OAUTH3._browser.request(preq, opts);
} }
, implicitGrant: function(providerUri, opts) { , implicitGrant: function(directives, opts) {
var promise; var promise;
var providerUri = directives.azp || directives.issuer || directives;
if (opts.broker) { if (opts.broker) {
// Discovery can happen in-flow because we know that this is // Discovery can happen in-flow because we know that this is
@ -583,7 +591,7 @@
} }
return OAUTH3._browser.frameRequest( return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(directives.issuer, authReq.url) OAUTH3.url.resolve(directives.issuer, authReq.url)
, authReq.state // state should recycle params , authReq.state // state should recycle params
, { windowType: opts.windowType , { windowType: opts.windowType
, reuseWindow: opts.broker && '-broker' , reuseWindow: opts.broker && '-broker'
@ -592,7 +600,7 @@
).then(function (tokens) { ).then(function (tokens) {
if (tokens.error) { if (tokens.error) {
// TODO directives.audience // TODO directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(directives.issuer /*providerUri*/, tokens)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(directives.issuer /*providerUri*/, tokens));
} }
return tokens; return tokens;
@ -606,7 +614,7 @@
var data = req.data; var data = req.data;
data.provider_uri = providerUri; data.provider_uri = providerUri;
if (data.error) { if (data.error) {
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(providerUri, data)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, data));
} }
return OAUTH3.hooks.session.refresh(opts, data); return OAUTH3.hooks.session.refresh(opts, data);
}); });
@ -627,7 +635,7 @@
); );
return OAUTH3._browser.frameRequest( return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(directives.issuer, logoutReq.url) OAUTH3.url.resolve(directives.issuer, logoutReq.url)
, logoutReq.state // state should recycle params , logoutReq.state // state should recycle params
, { windowType: 'popup' , { windowType: 'popup'
, reuseWindow: opts.broker && '-broker' , reuseWindow: opts.broker && '-broker'
@ -638,7 +646,7 @@
if (params.error) { if (params.error) {
// TODO directives.audience // TODO directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(directives.issuer /*providerUri*/, params)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(directives.issuer /*providerUri*/, params));
} }
return params; return params;
@ -654,7 +662,7 @@
// TODO we don't need to include this if we're using jQuery or angular // TODO we don't need to include this if we're using jQuery or angular
, discover: function(providerUri, opts) { , discover: function(providerUri, opts) {
opts = opts || {}; opts = opts || {};
providerUri = OAUTH3.utils.url.normalize(providerUri); providerUri = OAUTH3.url.normalize(providerUri);
if (providerUri.match(OAUTH3._browser.window.location.hostname)) { if (providerUri.match(OAUTH3._browser.window.location.hostname)) {
console.warn("It looks like you're a provider checking for your own directive," console.warn("It looks like you're a provider checking for your own directive,"
@ -662,7 +670,7 @@
+ " OAUTH3.request({ method: 'GET', url: '.well-known/oauth3/directive.json' })"); + " OAUTH3.request({ method: 'GET', url: '.well-known/oauth3/directive.json' })");
return OAUTH3.request({ return OAUTH3.request({
method: 'GET' method: 'GET'
, url: OAUTH3.utils.url.normalize(providerUri) + '/.well-known/oauth3/directives.json' , url: OAUTH3.url.normalize(providerUri) + '/.well-known/oauth3/directives.json'
}).then(function (resp) { }).then(function (resp) {
return resp.data; return resp.data;
}); });
@ -695,7 +703,7 @@
opts._windowType = opts.windowType; opts._windowType = opts.windowType;
opts.windowType = opts.windowType || 'background'; opts.windowType = opts.windowType || 'background';
return OAUTH3._browser.frameRequest( return OAUTH3._browser.frameRequest(
OAUTH3.utils.url.resolve(providerUri, discReq.url) OAUTH3.url.resolve(providerUri, discReq.url)
, discReq.state , discReq.state
// why not just pass opts whole? // why not just pass opts whole?
, { windowType: opts.windowType , { windowType: opts.windowType
@ -708,7 +716,7 @@
// caller will call OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || params.debug }); // caller will call OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || params.debug });
if (params.error) { if (params.error) {
// TODO directives.issuer || directives.audience // TODO directives.issuer || directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(providerUri, params)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, params));
} }
// TODO params should have response_type indicating json, binary, etc // TODO params should have response_type indicating json, binary, etc
@ -842,7 +850,7 @@
}).then(function (params) { }).then(function (params) {
if (params.error) { if (params.error) {
// TODO directives.issuer || directives.audience // TODO directives.issuer || directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError('https://oauth3.org', params)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse('https://oauth3.org', params));
} }
return params; return params;
}); });