|
| 1 | +import { promises as fs } from 'node:fs' |
| 2 | +import { join } from 'node:path' |
| 3 | + |
| 4 | +import test from 'ava' |
| 5 | +import { decode as nodeJwtDecode } from 'jsonwebtoken' |
| 6 | + |
| 7 | +import { Algorithm, sign, signSync, verifySync, verify } from '../index.js' |
| 8 | + |
| 9 | +const getUtcTimestamp = () => Math.floor(new Date().getTime() / 1000) |
| 10 | +const oneDayInSeconds = 86400 |
| 11 | + |
| 12 | +test('signSync and sign (async) should produce the same result', async (t) => { |
| 13 | + const claims = { |
| 14 | + data: { |
| 15 | + id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 16 | + pr: 33, |
| 17 | + isM: true, |
| 18 | + set: ['KL', 'TV', 'JI'], |
| 19 | + nest: { id: 'poly' }, |
| 20 | + }, |
| 21 | + exp: getUtcTimestamp() + oneDayInSeconds, |
| 22 | + } |
| 23 | + const secretKey = 'secret' |
| 24 | + const resSync = signSync(claims, secretKey) |
| 25 | + const resAsync = await sign(claims, secretKey) |
| 26 | + |
| 27 | + t.is(resSync, resAsync) |
| 28 | + t.truthy(nodeJwtDecode(resAsync)) |
| 29 | +}) |
| 30 | + |
| 31 | +test('verify should return the decoded claims', async (t) => { |
| 32 | + const data = { |
| 33 | + id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 34 | + pr: 33, |
| 35 | + isM: true, |
| 36 | + set: ['KL', 'TV', 'JI'], |
| 37 | + nest: { id: 'poly' }, |
| 38 | + } |
| 39 | + const claims = { data, exp: getUtcTimestamp() + oneDayInSeconds } |
| 40 | + const secretKey = 'secret' |
| 41 | + const headers = { algorithm: Algorithm.HS384 } |
| 42 | + |
| 43 | + const token = await sign(claims, secretKey, headers) |
| 44 | + |
| 45 | + const validation = { algorithms: [Algorithm.HS384] } |
| 46 | + const decodedToken = await verify(token, secretKey, validation) |
| 47 | + |
| 48 | + t.like(decodedToken, claims) |
| 49 | +}) |
| 50 | + |
| 51 | +test('verify sync should return the decoded claims', async (t) => { |
| 52 | + const data = { |
| 53 | + id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 54 | + pr: 33, |
| 55 | + isM: true, |
| 56 | + set: ['KL', 'TV', 'JI'], |
| 57 | + nest: { id: 'poly' }, |
| 58 | + } |
| 59 | + const claims = { data, exp: getUtcTimestamp() + oneDayInSeconds } |
| 60 | + const publicKeyPem = |
| 61 | + '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzq7L/V03tpy3QTYOP51CT0fY2Sp5spejcja9brkEZoLYFcvLSeNnsXtPg/Sr7PwbykiXoY++xo7+6o2VfPnbiEFV8fNap+4tWDmxeZfPifmCEA58BFncnK8z5luxR+syeRuI/9IUHllGxsKoQAbFECZoNCR+I5H/ynqhm9rvk89iNsh5EGxknOq2GmMaKRZ3nHZtVuwUj3BlwgsmP28ZAofMN/xM8bugHS1nNNHmRh6Ubg0Od3r2FH0+3df86ZzJ013M/LG1189aGNPXDOH4guBh7TPficw9nUnhIghiEFrxhAvIOQjClbhFud931T+UqD5BsF/ZarJ1VkaUa3UjxwIDAQAB-----END PUBLIC KEY-----' |
| 62 | + const privateKeyPem = |
| 63 | + '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAzq7L/V03tpy3QTYOP51CT0fY2Sp5spejcja9brkEZoLYFcvLSeNnsXtPg/Sr7PwbykiXoY++xo7+6o2VfPnbiEFV8fNap+4tWDmxeZfPifmCEA58BFncnK8z5luxR+syeRuI/9IUHllGxsKoQAbFECZoNCR+I5H/ynqhm9rvk89iNsh5EGxknOq2GmMaKRZ3nHZtVuwUj3BlwgsmP28ZAofMN/xM8bugHS1nNNHmRh6Ubg0Od3r2FH0+3df86ZzJ013M/LG1189aGNPXDOH4guBh7TPficw9nUnhIghiEFrxhAvIOQjClbhFud931T+UqD5BsF/ZarJ1VkaUa3UjxwIDAQABAoIBAQDGYRB7B9ZJ+PIMLY5PkOnsntGM4DAfM102a0Q32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u14698S4VHnhMu/671TwJXS6NpCTCGjrUJoKymuaBGYvgFRqcqjVtHzyz+YMkFQISvi/DurN5CN4C1Yiv7EDFQC+69fcOo4tP9S9EFya189IvJsJ-----END RSA PRIVATE KEY-----' |
| 64 | + const headers = { algorithm: Algorithm.RS256 } |
| 65 | + |
| 66 | + const token = await sign(claims, privateKeyPem, headers) |
| 67 | + |
| 68 | + const validation = { algorithms: [Algorithm.RS256] } |
| 69 | + const decodedToken = verifySync(token, publicKeyPem, validation) |
| 70 | + |
| 71 | + t.like(decodedToken, claims) |
| 72 | +}) |
| 73 | + |
| 74 | +test('verify should throw in case the token is expired', async (t) => { |
| 75 | + const data = { |
| 76 | + id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 77 | + pr: 33, |
| 78 | + isM: true, |
| 79 | + set: ['KL', 'TV', 'JI'], |
| 80 | + nest: { id: 'poly' }, |
| 81 | + } |
| 82 | + const claims = { data, iat: getUtcTimestamp() - oneDayInSeconds * 2, exp: getUtcTimestamp() - oneDayInSeconds } |
| 83 | + const secretKey = 'secret' |
| 84 | + const headers = { algorithm: Algorithm.HS256 } |
| 85 | + |
| 86 | + const token = await sign(claims, secretKey, headers) |
| 87 | + |
| 88 | + t.throws(() => verifySync(token, secretKey)) |
| 89 | + await t.throwsAsync(() => verify(token, secretKey)) |
| 90 | +}) |
| 91 | + |
| 92 | +test('should allow to use a buffer as sign/verify key', async (t) => { |
| 93 | + const [publicKeyPem, privateKeyPem] = await Promise.all([ |
| 94 | + fs.readFile(join(__dirname, 'public-key.pem')), |
| 95 | + fs.readFile(join(__dirname, 'private-key.pem')), |
| 96 | + ]) |
| 97 | + |
| 98 | + const data = { |
| 99 | + id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 100 | + pr: 33, |
| 101 | + isM: true, |
| 102 | + set: ['KL', 'TV', 'JI'], |
| 103 | + nest: { id: 'poly' }, |
| 104 | + } |
| 105 | + const claims = { data, exp: getUtcTimestamp() + oneDayInSeconds } |
| 106 | + const headers = { algorithm: Algorithm.PS256 } |
| 107 | + |
| 108 | + const token = await sign(claims, privateKeyPem.toString(), headers) |
| 109 | + |
| 110 | + const validation = { algorithms: [Algorithm.PS256] } |
| 111 | + const decodedToken = await verify(token, publicKeyPem.toString(), validation) |
| 112 | + |
| 113 | + t.like(decodedToken, claims) |
| 114 | +}) |
0 commit comments