Skip to content

Commit

Permalink
feat: added verifySignature to jwt package
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Aug 21, 2023
1 parent f974dd6 commit 9c2ac56
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
50 changes: 41 additions & 9 deletions packages/jwt/__tests__/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { tokenGenerate } from '../lib';
import { verify } from 'jsonwebtoken';
import { tokenGenerate, verifySignature } from '../lib';
import { verify, sign } from 'jsonwebtoken';
import { readFileSync } from 'fs';
import {
MissingApplicationIdError,
Expand Down Expand Up @@ -32,29 +32,27 @@ const acl = {
describe('Token Generator', () => {
test('Will throw when missing applicationId', () => {
// eslint-disable-next-line
// @ts-ignore
// @ts-ignore
expect(() => tokenGenerate()).toThrow(new MissingApplicationIdError());
});

test('Will throw when missing privateKey', () => {
// eslint-disable-next-line
// @ts-ignore
expect(() => tokenGenerate('app-id')).toThrow(
new MissingPrivateKeyError(),
);
// @ts-ignore
expect(() => tokenGenerate('app-id')).toThrow(new MissingPrivateKeyError());
});

test('Will throw when applicationId not a string', () => {
// eslint-disable-next-line
// @ts-ignore
// @ts-ignore
expect(() => tokenGenerate(12345, privateKey)).toThrow(
new InvalidApplicationIdError(),
);
});

test('Will throw when privateKey not a string or buffer', () => {
// eslint-disable-next-line
// @ts-ignore
// @ts-ignore
expect(() => tokenGenerate(applicationId, 56789)).toThrow(
new InvalidPrivateKeyError(),
);
Expand Down Expand Up @@ -92,4 +90,38 @@ describe('Token Generator', () => {
expect(decoded).not.toHaveProperty('ttl');
expect(decoded.acl).toMatchObject(acl);
});

test('Can Verify signature', () => {
const token = tokenGenerate(applicationId, privateKey, {
ttl,
subject,
acl,
});

// eslint-disable-next-line
expect(verifySignature(token, privateKey)).toEqual(true);
});

test('Will not validate with invalid key', () => {
const token = tokenGenerate(applicationId, privateKey, {
ttl,
subject,
acl,
});

// eslint-disable-next-line
expect(verifySignature(token, 'fizz-buzz')).toEqual(false);
});

test('Will not validate with invalid JWT token', () => {
const token = sign({}, 'fizzBuzz');

// eslint-disable-next-line
expect(verifySignature(token, privateKey)).toEqual(false);
});

test('Will not validate with non JWT token', () => {
// eslint-disable-next-line
expect(verifySignature('fizz-buzz', privateKey)).toEqual(false);
});
});
4 changes: 4 additions & 0 deletions packages/jwt/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export function tokenGenerate(
) {
return instance.tokenGenerate(applicationId, privateKey, opts);
}

export function verifySignature(token: string, privateKey: string | Buffer) {
return instance.verifySignature(token, privateKey);
}
17 changes: 15 additions & 2 deletions packages/jwt/lib/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JWTInterface, GeneratorOptions, Claims } from './common';
import { sign } from 'jsonwebtoken';
import { sign, verify } from 'jsonwebtoken';
import { v4 as uuidv4 } from 'uuid';
import {
MissingApplicationIdError,
Expand Down Expand Up @@ -45,6 +45,19 @@ export class JWT implements JWTInterface {
});
}

verifySignature(jwt: string, privateKey: string | Buffer): boolean {
try {
verify(jwt, privateKey, {
algorithms: ['RS256'],
});
return true;
} catch (error) {
log('Error when verifying token', error);
}

return false;
}

private validateOptions(opts?: GeneratorOptions): Claims {
const now = parseInt((Date.now() / 1000).toString(), 10);

Expand All @@ -70,7 +83,7 @@ export class JWT implements JWTInterface {

for (const property in opts) {
// eslint-disable-next-line
if (opts.hasOwnProperty(property)) {
if (opts.hasOwnProperty(property)) {
claims[property] = opts[property];
}
}
Expand Down

0 comments on commit 9c2ac56

Please sign in to comment.