From 9e8ce962c7d0d2bd5ee715a8b5e33615086c987b Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 25 May 2021 20:29:23 +0200 Subject: [PATCH] refactor: getValueAndPayload signature --- lib/models/base_model.js | 4 +-- lib/models/base_token.js | 50 ------------------------------------ lib/models/formats/jwt.js | 8 +++--- lib/models/formats/opaque.js | 3 +-- lib/models/formats/paseto.js | 4 +-- 5 files changed, 9 insertions(+), 60 deletions(-) diff --git a/lib/models/base_model.js b/lib/models/base_model.js index f7f0abd69..dd01c9eb0 100644 --- a/lib/models/base_model.js +++ b/lib/models/base_model.js @@ -56,7 +56,7 @@ module.exports = function getBaseToken(provider) { this.exp = epochTime() + ttl; } - const [token, payload] = await this.getValueAndPayload(); + const { value, payload } = await this.getValueAndPayload(); if (payload) { await this.adapter.upsert(this.jti, payload, ttl); @@ -65,7 +65,7 @@ module.exports = function getBaseToken(provider) { this.emit('issued'); } - return token; + return value; } async destroy() { diff --git a/lib/models/base_token.js b/lib/models/base_token.js index c9f9648c2..67b414fa1 100644 --- a/lib/models/base_token.js +++ b/lib/models/base_token.js @@ -66,14 +66,6 @@ module.exports = function getBaseToken(provider) { ]; } - /** - * @name expiration - * @api public - * - * Return a Number (value of seconds) for this tokens TTL. Always return this.expiresIn when - * set, otherwise return the desired TTL in seconds. - * - */ get expiration() { if (!this.expiresIn) { this.expiresIn = this.constructor.expiresIn(ctxRef.get(this), this, this.#client); @@ -91,47 +83,5 @@ module.exports = function getBaseToken(provider) { } } - /** - * @name generateTokenId - * @api public - * - * Return a randomly generated Token instance ID (string) - * - * BaseToken.prototype.generateTokenId - * see implementations for each format in lib/models/formats - */ - - /** - * @name getValueAndPayload - * @api public - * - * Return an Array instance with the first member being a string representation of the token as - * it should be returned to the client. Second member being an Object that should be passed to - * the adapter for storage. - * - * BaseToken.prototype.getValueAndPayload - * see implementations for each format in lib/models/formats - */ - - /** - * @name verify - * @static - * @api public - * - * A verify function that asserts that the presented token is valid, not expired, - * or otherwise manipulated with. - * - * @param token - the presented token string value - * @param stored - data returned from the adapter token lookup - * @param options - options object - * @param options.ignoreExpiration - Boolean indicating whether expired but still stored - * tokens should pass this verification. - * - * BaseToken.prototype.constructor.verify - * see implementations for each format in lib/models/formats - */ - - // class BaseToken extends hasFormat(provider, 'default', Class) {} - return BaseToken; }; diff --git a/lib/models/formats/jwt.js b/lib/models/formats/jwt.js index 568070a5a..120845615 100644 --- a/lib/models/formats/jwt.js +++ b/lib/models/formats/jwt.js @@ -97,7 +97,7 @@ module.exports = (provider, { opaque }) => { return nanoid(); }, async getValueAndPayload() { - const [, payload] = await opaque.getValueAndPayload.call(this); + const { payload } = await opaque.getValueAndPayload.call(this); const { aud, jti, iat, exp, scope, clientId, 'x5t#S256': x5t, jkt, extra, } = payload; @@ -170,11 +170,11 @@ module.exports = (provider, { opaque }) => { alg: config.encrypt.alg, }); - return [encrypted]; + return { value: encrypted }; } if (config.sign) { - return [signed]; + return { value: signed }; } if (config.encrypt) { @@ -191,7 +191,7 @@ module.exports = (provider, { opaque }) => { alg: config.encrypt.alg, }); - return [encrypted]; + return { value: encrypted }; } throw new Error('invalid Resource Server jwt configuration'); diff --git a/lib/models/formats/opaque.js b/lib/models/formats/opaque.js index cad3480d9..668d6f7ad 100644 --- a/lib/models/formats/opaque.js +++ b/lib/models/formats/opaque.js @@ -27,7 +27,6 @@ module.exports = (provider) => ({ async getValueAndPayload() { const now = epochTime(); const exp = this.exp || now + this.expiration; - const value = this.jti; const payload = { iat: this.iat || epochTime(), ...(exp ? { exp } : undefined), @@ -41,7 +40,7 @@ module.exports = (provider) => ({ payload.extra = await instance(provider).configuration('extraTokenClaims')(ctxRef.get(this), this); } - return [value, payload]; + return { value: this.jti, payload }; }, async verify(stored, { ignoreExpiration } = {}) { // checks that legacy tokens aren't accepted as opaque when their jti is passed diff --git a/lib/models/formats/paseto.js b/lib/models/formats/paseto.js index c947133ef..538614ac9 100644 --- a/lib/models/formats/paseto.js +++ b/lib/models/formats/paseto.js @@ -63,7 +63,7 @@ module.exports = (provider, { opaque }) => { return nanoid(); }, async getValueAndPayload() { - const [, payload] = await opaque.getValueAndPayload.call(this); + const { payload } = await opaque.getValueAndPayload.call(this); const { aud, jti, iat, exp, scope, clientId, 'x5t#S256': x5t, jkt, extra, } = payload; @@ -146,7 +146,7 @@ module.exports = (provider, { opaque }) => { }, ); - return [token]; + return { value: token }; }, }; };