cache directives, just a little bit

This commit is contained in:
AJ ONeal 2017-02-11 00:19:17 -05:00
parent 7de254d597
commit a2b52b7619
2 changed files with 31 additions and 3 deletions

View File

@ -20,8 +20,19 @@
if (!providerUri) { if (!providerUri) {
throw new Error('oauth3.discover(providerUri, opts) received providerUri as ' + providerUri); throw new Error('oauth3.discover(providerUri, opts) received providerUri as ' + providerUri);
} }
var directives = OAUTH3.hooks.getDirectives(providerUri);
if (directives && directives.issuer) {
return OAUTH3.PromiseA.resolve(directives);
}
return browser._discoverHelper(providerUri, opts).then(function (directives) {
directives.issuer = directives.issuer || OAUTH3_CORE.normalizeUrl(providerUri);
console.log('discoverHelper', directives);
return OAUTH3.hooks.setDirectives(providerUri, directives);
});
}
, _discoverHelper: function (providerUri, opts) {
opts = opts || {}; opts = opts || {};
opts.debug = true; //opts.debug = true;
providerUri = OAUTH3_CORE.normalizeUrl(providerUri); providerUri = OAUTH3_CORE.normalizeUrl(providerUri);
if (window.location.hostname.match(providerUri)) { if (window.location.hostname.match(providerUri)) {
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,"
@ -44,7 +55,6 @@
return OAUTH3_CORE.formatError(providerUri, params.error); return OAUTH3_CORE.formatError(providerUri, params.error);
} }
var directives = JSON.parse(atob(OAUTH3_CORE.utils.urlSafeBase64ToBase64(params.result || params.directives))); var directives = JSON.parse(atob(OAUTH3_CORE.utils.urlSafeBase64ToBase64(params.result || params.directives)));
directives.issuer = directives.issuer || OAUTH3_CORE.normalizeUrl(providerUri);
return directives; return directives;
}, function (err) { }, function (err) {
return OAUTH3.PromiseA.reject(err); return OAUTH3.PromiseA.reject(err);

View File

@ -136,6 +136,9 @@
return oauth3.PromiseA.resolve(oauth3.hooks.setSession(providerUri, oldSession)); return oauth3.PromiseA.resolve(oauth3.hooks.setSession(providerUri, oldSession));
} }
, setSession: function (providerUri, newSession) { , setSession: function (providerUri, newSession) {
if (!providerUri) {
console.error(new Error('no providerUri').stack);
}
providerUri = oauth3.core.normalizeUri(providerUri); providerUri = oauth3.core.normalizeUri(providerUri);
console.warn('[oauth3.hooks.setSession] PLEASE IMPLEMENT -- Your Fault'); console.warn('[oauth3.hooks.setSession] PLEASE IMPLEMENT -- Your Fault');
console.warn(newSession); console.warn(newSession);
@ -149,6 +152,22 @@
if (!oauth3.hooks._sessions) { oauth3.hooks._sessions = {}; } if (!oauth3.hooks._sessions) { oauth3.hooks._sessions = {}; }
return oauth3.hooks._sessions[providerUri]; return oauth3.hooks._sessions[providerUri];
} }
, setDirectives: function (providerUri, directives) {
providerUri = oauth3.core.normalizeUri(providerUri);
console.warn('[oauth3.hooks.setDirectives] PLEASE IMPLEMENT -- Your Fault');
console.warn(directives);
if (!oauth3.hooks._directives) { oauth3.hooks._directives = {}; }
window.localStorage.setItem('directives-' + providerUri, JSON.stringify(directives));
oauth3.hooks._directives[providerUri] = directives;
return directives;
}
, getDirectives: function (providerUri) {
providerUri = oauth3.core.normalizeUri(providerUri);
console.warn('[oauth3.hooks.getDirectives] PLEASE IMPLEMENT -- Your Fault');
if (!oauth3.hooks._directives) { oauth3.hooks._directives = {}; }
return JSON.parse(window.localStorage.getItem('directives-' + providerUri) || '{}');
//return oauth3.hooks._directives[providerUri];
}
// Provider Only // Provider Only
, setGrants: function (clientUri, newGrants) { , setGrants: function (clientUri, newGrants) {
@ -243,7 +262,6 @@
client_id: providerUri client_id: providerUri
, debug: opts.debug , debug: opts.debug
}).then(function (directive) { }).then(function (directive) {
console.log('core.urls.grants(directive, opts)', core.urls.grants(directive, opts));
return oauth3.request(core.urls.grants(directive, opts)).then(function (grantsResult) { return oauth3.request(core.urls.grants(directive, opts)).then(function (grantsResult) {
if ('POST' === opts.method) { if ('POST' === opts.method) {
// TODO this is clientToken // TODO this is clientToken