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