Skip to content

Commit

Permalink
refactor: getValueAndPayload signature
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed May 26, 2021
1 parent 153ac52 commit 9e8ce96
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/models/base_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -65,7 +65,7 @@ module.exports = function getBaseToken(provider) {
this.emit('issued');
}

return token;
return value;
}

async destroy() {
Expand Down
50 changes: 0 additions & 50 deletions lib/models/base_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};
8 changes: 4 additions & 4 deletions lib/models/formats/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions lib/models/formats/opaque.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/models/formats/paseto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,7 +146,7 @@ module.exports = (provider, { opaque }) => {
},
);

return [token];
return { value: token };
},
};
};

0 comments on commit 9e8ce96

Please sign in to comment.