added hooks to store key pairs in localStorage
This commit is contained in:
parent
5a5488f504
commit
39c18ab184
|
@ -495,6 +495,7 @@ OAUTH3.requests.accounts.create = function (directive, session, account) {
|
||||||
, data: data
|
, data: data
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
OAUTH3.hooks.grants = {
|
OAUTH3.hooks.grants = {
|
||||||
// Provider Only
|
// Provider Only
|
||||||
set: function (clientUri, newGrants) {
|
set: function (clientUri, newGrants) {
|
||||||
|
@ -516,6 +517,96 @@ OAUTH3.hooks.grants = {
|
||||||
return this._cache[clientUri];
|
return this._cache[clientUri];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
OAUTH3.hooks.keyPairs = {
|
||||||
|
set: function (id, keyPair) {
|
||||||
|
if (!keyPair && id.privateKey && id.publicKey && id.sub) {
|
||||||
|
keyPair = id;
|
||||||
|
id = keyPair.sub;
|
||||||
|
}
|
||||||
|
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 (!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;
|
||||||
|
}
|
||||||
|
, all: function () {
|
||||||
|
if (OAUTH3._hooks && OAUTH3._hooks.keyPairs && OAUTH3._hooks.keyPairs.all) {
|
||||||
|
return OAUTH3._hooks.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
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) {
|
||||||
|
window.localStorage.removeItem(key);
|
||||||
|
});
|
||||||
|
return OAUTH3.PromiseA.resolve();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
OAUTH3._browser.isIframe = function isIframe () {
|
OAUTH3._browser.isIframe = function isIframe () {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue