|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { HmacSHA1 } from '../../vendor/crypto-es'; |
| 4 | +import { assertTokenSignature } from '../utils'; |
| 5 | + |
| 6 | +describe('assertTokenSignature(token, key, signature)', () => { |
| 7 | + const token = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyLWlkIn0.0u5CllULtDVD9DUUmUMdJLbBCSNcnv4j3hCaPz4dNr8'; |
| 8 | + const key = 'sk_test_mock'; |
| 9 | + const validSignature = HmacSHA1(token, key).toString(); |
| 10 | + |
| 11 | + it('passes when the signature matches', () => { |
| 12 | + expect(() => assertTokenSignature(token, key, validSignature)).not.toThrow(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('throws when the signature is missing', () => { |
| 16 | + expect(() => assertTokenSignature(token, key, undefined)).toThrowError(); |
| 17 | + expect(() => assertTokenSignature(token, key, null)).toThrowError(); |
| 18 | + expect(() => assertTokenSignature(token, key, '')).toThrowError(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('throws when the signature differs at the last character', () => { |
| 22 | + const tampered = validSignature.slice(0, -1) + (validSignature.endsWith('0') ? '1' : '0'); |
| 23 | + expect(() => assertTokenSignature(token, key, tampered)).toThrowError(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('throws when the signature differs in length', () => { |
| 27 | + expect(() => assertTokenSignature(token, key, validSignature.slice(0, -1))).toThrowError(); |
| 28 | + expect(() => assertTokenSignature(token, key, validSignature + '0')).toThrowError(); |
| 29 | + }); |
| 30 | +}); |
0 commit comments