Skip to content

Commit

Permalink
Merge branch 'main' into dev/openwallet-foundation#53
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjhan authored Feb 7, 2024
2 parents 57fa2c4 + 3b017ef commit 07ae120
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/test/jwt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ describe('JWT', () => {
expect(jwt.payload).toEqual({ foo: 'bar' });
});

test('returns decoded JWT when correct JWT string is provided', () => {
// Two objects are created separately, the first: { alg: 'HS256', typ: 'JWT' } represents a JWT Header and the second: { sub: '1234567890', name: 'John Doe' } represents a JWT Payload.
// These objects are turned into strings with JSON.stringify. The resulting strings are encoded with base64 encoding using Buffer.from(string).toString('base64').
// These base64 encoded strings are concatenated with a period (.) between them, following the structure of a JWT, which is composed of three Base64-URL strings separated by dots (header.payload.signature).
// A 'signature' string is added at the end to represent a JWT signature.
// So, the jwt variable ends up being a string with the format of a base64Url encoded Header, a period, a base64Url encoded Payload, another period, and a 'signature' string.
// It's important to note that the 'signature' here is just a placeholder string and not an actual cryptographic signature generated from the header and payload data.
const jwt = `${Buffer.from(JSON.stringify({ alg: 'HS256', typ: 'JWT' })).toString('base64')}.${Buffer.from(JSON.stringify({ sub: '1234567890', name: 'John Doe' })).toString('base64')}.signature`;
const result = Jwt.decodeJWT(jwt);
expect(result).toEqual({
header: { alg: 'HS256', typ: 'JWT' },
payload: { sub: '1234567890', name: 'John Doe' },
signature: 'signature',
});
});

test('throws an error when JWT string is not correctly formed', () => {
const jwt = 'abc.def';
expect(() => Jwt.decodeJWT(jwt)).toThrow('Invalid JWT as input');
});

test('throws an error when JWT parts are missing', () => {
const jwt = `${Buffer.from(JSON.stringify({ alg: 'HS256', typ: 'JWT' })).toString('base64')}`;
expect(() => Jwt.decodeJWT(jwt)).toThrow('Invalid JWT as input');
});

test('set', async () => {
const jwt = new Jwt();
jwt.setHeader({ alg: 'EdDSA' });
Expand Down

0 comments on commit 07ae120

Please sign in to comment.