Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend types for server side token #415

Merged
merged 3 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/signing.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import jwt, { SignOptions } from 'jsonwebtoken';

// for a claim in jwt
function joinClaimValue(items: string | string[]): string {
const values = Array.isArray(items) ? items : [items];
const claims = [];
for (let i = 0; i < values.length; i += 1) {
const s = values[i].trim();
if (s === '*') return s;
claims.push(s);
}
return claims.join(',');
}

/**
* Creates the JWT token for feedId, resource and action using the apiSecret
* @method JWTScopeToken
* @memberof signing
* @private
* @param {string} apiSecret - API Secret key
* @param {string} resource - JWT payload resource
* @param {string} action - JWT payload action
* @param {string | string[]} resource - JWT payload resource
* @param {string | string[]} action - JWT payload action
* @param {object} [options] - Optional additional options
* @param {string} [options.feedId] - JWT payload feed identifier
* @param {string | string[]} [options.feedId] - JWT payload feed identifier
* @param {string} [options.userId] - JWT payload user identifier
* @param {boolean} [options.expireTokens] - JWT noTimestamp
* @return {string} JWT Token
*/
export function JWTScopeToken(
apiSecret: string,
resource: string,
action: string,
options: { expireTokens?: boolean; feedId?: string; userId?: string } = {},
resource: string | string[],
action: string | string[],
options: { expireTokens?: boolean; feedId?: string | string[]; userId?: string } = {},
) {
const noTimestamp = options.expireTokens ? !options.expireTokens : true;
const payload: { action: string; resource: string; feed_id?: string; user_id?: string } = { resource, action };
if (options.feedId) payload.feed_id = options.feedId;
const payload: { action: string; resource: string; feed_id?: string; user_id?: string } = {
resource: joinClaimValue(resource),
action: joinClaimValue(action),
};
if (options.feedId) payload.feed_id = joinClaimValue(options.feedId);
if (options.userId) payload.user_id = options.userId;

return jwt.sign(payload, apiSecret, { algorithm: 'HS256', noTimestamp });
Expand Down
9 changes: 9 additions & 0 deletions test/unit/node/token_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ describe('[UNIT] Creating tokens', function () {

expect(token).to.be(expected);
});

it('#multiClaimToken', () => {
const token = JWTScopeToken('abcdefghijklmnop', ['personalization', 'collections'], ['read', 'delete'], {
feedId: ['timeline:me', 'timeline:you'],
});
expect(token).to.be(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXNvdXJjZSI6InBlcnNvbmFsaXphdGlvbixjb2xsZWN0aW9ucyIsImFjdGlvbiI6InJlYWQsZGVsZXRlIiwiZmVlZF9pZCI6InRpbWVsaW5lOm1lLHRpbWVsaW5lOnlvdSJ9.zWwZIdeWl2HCFvFPC9aMK4h832bmoQuifmUlyPq9knE',
);
});
});