changed default storage for grants and key pairs as well

This commit is contained in:
tigerbot 2017-07-31 19:39:52 -06:00
parent 197c0fdcb2
commit e42079d856
1 changed files with 88 additions and 83 deletions

View File

@ -541,28 +541,45 @@ OAUTH3.requests.accounts.create = function (directive, session, account) {
}; };
OAUTH3.hooks.grants = { OAUTH3.hooks.grants = {
// Provider Only get: function (clientUri) {
set: function (clientUri, newGrants) { OAUTH3.hooks._checkStorage('grants', 'get');
clientUri = OAUTH3.uri.normalize(clientUri);
console.warn('[oauth3.hooks.setGrants] PLEASE IMPLEMENT -- Your Fault'); if (!clientUri) {
console.warn(newGrants); throw new Error("clientUri is not set");
if (!this._cache) { this._cache = {}; } }
console.log('clientUri, newGrants'); return OAUTH3.PromiseA.resolve(OAUTH3._hooks.grants.get(OAUTH3.uri.normalize(clientUri)));
console.log(clientUri, newGrants);
this._cache[clientUri] = newGrants;
return newGrants;
} }
, get: function (clientUri) { , set: function (clientUri, grants) {
clientUri = OAUTH3.uri.normalize(clientUri); OAUTH3.hooks._checkStorage('grants', 'set');
console.warn('[oauth3.hooks.getGrants] PLEASE IMPLEMENT -- Your Fault');
if (!this._cache) { this._cache = {}; } if (!clientUri) {
console.log('clientUri, existingGrants'); throw new Error("clientUri is not set");
console.log(clientUri, this._cache[clientUri]); }
return this._cache[clientUri]; return OAUTH3.PromiseA.resolve(OAUTH3._hooks.grants.set(OAUTH3.uri.normalize(clientUri), grants));
}
, all: function () {
OAUTH3.hooks._checkStorage('grants', 'all');
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.grants.all());
}
, clear: function () {
OAUTH3.hooks._checkStorage('grants', 'clear');
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.grants.clear());
} }
}; };
OAUTH3.hooks.keyPairs = { OAUTH3.hooks.keyPairs = {
set: function (id, keyPair) { get: function (id) {
OAUTH3.hooks._checkStorage('keyPairs', 'get');
if (!id) {
throw new Error("id is not set");
}
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.keyPairs.get(id));
}
, set: function (id, keyPair) {
OAUTH3.hooks._checkStorage('keyPairs', 'set');
if (!keyPair && id.privateKey && id.publicKey && id.sub) { if (!keyPair && id.privateKey && id.publicKey && id.sub) {
keyPair = id; keyPair = id;
id = keyPair.sub; id = keyPair.sub;
@ -570,82 +587,70 @@ OAUTH3.hooks.keyPairs = {
if (!keyPair) { if (!keyPair) {
return OAUTH3.PromiseA.reject(new Error("no key pair provided to save")); return OAUTH3.PromiseA.reject(new Error("no key pair provided to save"));
} }
if (!id) {
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.set) { throw new Error("id is not set");
return OAUTH3._hooks.keyPairs.set(id, keyPair);
} }
keyPair.sub = keyPair.sub || id;
if (!OAUTH3.hooks.keyPairs._warned) { OAUTH3.hooks.keyPairs._warned = {}; } return OAUTH3.PromiseA.resolve(OAUTH3._hooks.keyPairs.set(id, keyPair));
if (!OAUTH3.hooks.keyPairs._warned.set) {
console.warn('[Warn] Please implement OAUTH3._hooks.keyPairs.set = function (id, keyPair) { return PromiseA<keyPair>; }');
OAUTH3.hooks.keyPairs._warned.set = true;
}
window.localStorage.setItem('key_pair-'+id, JSON.stringify(keyPair));
return OAUTH3.PromiseA.resolve(keyPair);
}
, get: function (id) {
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.get) {
return OAUTH3._hooks.keyPairs.get(id);
}
if (!OAUTH3.hooks.keyPairs._warned) { OAUTH3.hooks.keyPairs._warned = {}; }
if (!OAUTH3.hooks.keyPairs._warned.get) {
console.warn('[Warn] Please implement OAUTH3._hooks.keyPairs.get = function (id) { return PromiseA<keyPair>; }');
OAUTH3.hooks.keyPairs._warned.get = true;
}
return OAUTH3.PromiseA.resolve().then(function () {
return window.localStorage.getItem('key_pair-'+id) || 'null';
}).then(JSON.parse);
}
, _get_all_keys: function () {
var pattern = /^key_pair-/;
var matching = [];
var ind, key;
for (ind = 0; ind < window.localStorage.length; ind++) {
key = window.localStorage.key(ind);
if (pattern.test(key)) {
matching.push(key);
}
}
return matching;
} }
, all: function () { , all: function () {
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.all) { OAUTH3.hooks._checkStorage('keyPairs', 'all');
return OAUTH3._hooks.keyPairs.all();
}
if (!OAUTH3.hooks.keyPairs._warned) { OAUTH3.hooks.keyPairs._warned = {}; } return OAUTH3.PromiseA.resolve(OAUTH3._hooks.keyPairs.all());
if (!OAUTH3.hooks.keyPairs._warned.all) { }
console.warn('[Warn] Please implement OAUTH3._hooks.keyPairs.all = function (id) { return PromiseA<keyPair>; }'); , clear: function () {
OAUTH3.hooks.keyPairs._warned.all = true; OAUTH3.hooks._checkStorage('keyPairs', 'clear');
}
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.keyPairs.clear());
}
};
OAUTH3._defaultStorage.grants = {
prefix: 'grants-'
, get: function (clientUri) {
var result = JSON.parse(window.localStorage.getItem(this.prefix + clientUri) || 'null');
return OAUTH3.PromiseA.resolve(result);
}
, set: function (clientUri, grants) {
window.localStorage.setItem(this.prefix + clientUri, JSON.stringify(grants));
return this.get(clientUri);
}
, all: function () {
var prefix = this.prefix;
var result = {}; var result = {};
OAUTH3.hooks.keyPairs._get_all_keys().forEach(function (key) { OAUTH3._defaultStorage._getStorageKeys(prefix, window.localStorage).forEach(function (key) {
var id = key.replace('key_pair-', ''); result[key.replace(prefix, '')] = JSON.parse(window.localStorage.getItem(key) || 'null');
try {
result[id] = JSON.parse(window.localStorage.getItem(key));
} catch (err) {
console.error('failed to parse key', key, err);
window.localStorage.removeItem(key);
}
}); });
return OAUTH3.PromiseA.resolve(result); return OAUTH3.PromiseA.resolve(result);
} }
, clear: function () { , clear: function () {
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.clear) { OAUTH3._defaultStorage._getStorageKeys(this.prefix, window.localStorage).forEach(function (key) {
return OAUTH3._hooks.keyPairs.clear(); window.localStorage.removeItem(key);
} });
return OAUTH3.PromiseA.resolve();
if (!OAUTH3.hooks.keyPairs._warned) { OAUTH3.hooks.keyPairs._warned = {}; } }
if (!OAUTH3.hooks.keyPairs._warned.clear) { };
console.warn('[Warn] Please implement OAUTH3._hooks.keyPairs.clear = function (id) { return PromiseA<>; }'); OAUTH3._defaultStorage.keyPairs = {
OAUTH3.hooks.keyPairs._warned.clear = true; prefix: 'key_pairs-'
} , get: function (id) {
var result = JSON.parse(window.localStorage.getItem(this.prefix + id) || 'null');
OAUTH3.hooks.keyPairs._get_all_keys().forEach(function (key) { return OAUTH3.PromiseA.resolve(result);
}
, set: function (id, keyPair) {
window.localStorage.setItem(this.prefix + id, JSON.stringify(keyPair));
return this.get(id);
}
, all: function () {
var prefix = this.prefix;
var result = {};
OAUTH3._defaultStorage._getStorageKeys(prefix, window.localStorage).forEach(function (key) {
result[key.replace(prefix, '')] = JSON.parse(window.localStorage.getItem(key) || 'null');
});
return OAUTH3.PromiseA.resolve(result);
}
, clear: function () {
OAUTH3._defaultStorage._getStorageKeys(this.prefix, window.localStorage).forEach(function (key) {
window.localStorage.removeItem(key); window.localStorage.removeItem(key);
}); });
return OAUTH3.PromiseA.resolve(); return OAUTH3.PromiseA.resolve();