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 = {
// Provider Only
set: function (clientUri, newGrants) {
clientUri = OAUTH3.uri.normalize(clientUri);
console.warn('[oauth3.hooks.setGrants] PLEASE IMPLEMENT -- Your Fault');
console.warn(newGrants);
if (!this._cache) { this._cache = {}; }
console.log('clientUri, newGrants');
console.log(clientUri, newGrants);
this._cache[clientUri] = newGrants;
return newGrants;
get: function (clientUri) {
OAUTH3.hooks._checkStorage('grants', 'get');
if (!clientUri) {
throw new Error("clientUri is not set");
}
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.grants.get(OAUTH3.uri.normalize(clientUri)));
}
, get: function (clientUri) {
clientUri = OAUTH3.uri.normalize(clientUri);
console.warn('[oauth3.hooks.getGrants] PLEASE IMPLEMENT -- Your Fault');
if (!this._cache) { this._cache = {}; }
console.log('clientUri, existingGrants');
console.log(clientUri, this._cache[clientUri]);
return this._cache[clientUri];
, set: function (clientUri, grants) {
OAUTH3.hooks._checkStorage('grants', 'set');
if (!clientUri) {
throw new Error("clientUri is not set");
}
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 = {
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) {
keyPair = id;
id = keyPair.sub;
@ -570,82 +587,70 @@ OAUTH3.hooks.keyPairs = {
if (!keyPair) {
return OAUTH3.PromiseA.reject(new Error("no key pair provided to save"));
}
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.set) {
return OAUTH3._hooks.keyPairs.set(id, keyPair);
if (!id) {
throw new Error("id is not set");
}
keyPair.sub = keyPair.sub || id;
if (!OAUTH3.hooks.keyPairs._warned) { OAUTH3.hooks.keyPairs._warned = {}; }
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;
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.keyPairs.set(id, keyPair));
}
, all: function () {
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.all) {
return OAUTH3._hooks.keyPairs.all();
}
OAUTH3.hooks._checkStorage('keyPairs', 'all');
if (!OAUTH3.hooks.keyPairs._warned) { OAUTH3.hooks.keyPairs._warned = {}; }
if (!OAUTH3.hooks.keyPairs._warned.all) {
console.warn('[Warn] Please implement OAUTH3._hooks.keyPairs.all = function (id) { return PromiseA<keyPair>; }');
OAUTH3.hooks.keyPairs._warned.all = true;
}
return OAUTH3.PromiseA.resolve(OAUTH3._hooks.keyPairs.all());
}
, clear: function () {
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 = {};
OAUTH3.hooks.keyPairs._get_all_keys().forEach(function (key) {
var id = key.replace('key_pair-', '');
try {
result[id] = JSON.parse(window.localStorage.getItem(key));
} catch (err) {
console.error('failed to parse key', key, err);
window.localStorage.removeItem(key);
}
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 () {
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.clear) {
return OAUTH3._hooks.keyPairs.clear();
}
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.hooks.keyPairs._warned.clear = true;
}
OAUTH3.hooks.keyPairs._get_all_keys().forEach(function (key) {
OAUTH3._defaultStorage._getStorageKeys(this.prefix, window.localStorage).forEach(function (key) {
window.localStorage.removeItem(key);
});
return OAUTH3.PromiseA.resolve();
}
};
OAUTH3._defaultStorage.keyPairs = {
prefix: 'key_pairs-'
, get: function (id) {
var result = JSON.parse(window.localStorage.getItem(this.prefix + id) || 'null');
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);
});
return OAUTH3.PromiseA.resolve();