From 39c18ab1842bdf4579d5a5a7cec6ef515a62d869 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Fri, 28 Jul 2017 16:27:52 -0600 Subject: [PATCH] added hooks to store key pairs in localStorage --- oauth3.issuer.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/oauth3.issuer.js b/oauth3.issuer.js index d45c1e1..19deb22 100644 --- a/oauth3.issuer.js +++ b/oauth3.issuer.js @@ -495,6 +495,7 @@ OAUTH3.requests.accounts.create = function (directive, session, account) { , data: data }); }; + OAUTH3.hooks.grants = { // Provider Only set: function (clientUri, newGrants) { @@ -516,6 +517,96 @@ OAUTH3.hooks.grants = { 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; }'); + 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; }'); + 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; }'); + 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 () { try {