|
| 1 | +import { JwtError, JwtErrorCode } from "../src/errors" |
| 2 | +import { DecodedHeader, DecodedPayload, RS256Token } from "../src/jwt-decoder" |
| 3 | +import { genTime, genIss, signJWT, TestingKeyFetcher, encodeObjectBase64Url } from "./jwk-utils" |
| 4 | + |
| 5 | +describe("TokenDecoder", () => { |
| 6 | + const kid = "kid123456" |
| 7 | + const projectId = "projectId1234" |
| 8 | + const currentTimestamp = genTime(Date.now()) |
| 9 | + const payload: DecodedPayload = { |
| 10 | + aud: projectId, |
| 11 | + exp: currentTimestamp, // now (for border test) |
| 12 | + iat: currentTimestamp - 10000, // -10s |
| 13 | + iss: genIss(projectId), |
| 14 | + sub: "userId12345", |
| 15 | + } |
| 16 | + |
| 17 | + it("invalid token", () => { |
| 18 | + expect(() => RS256Token.decode("invalid", currentTimestamp)).toThrowError( |
| 19 | + new JwtError( |
| 20 | + JwtErrorCode.INVALID_ARGUMENT, |
| 21 | + "token must consist of 3 parts" |
| 22 | + ) |
| 23 | + ) |
| 24 | + }) |
| 25 | + |
| 26 | + describe("header", () => { |
| 27 | + const validHeader: DecodedHeader = { |
| 28 | + kid, |
| 29 | + alg: "RS256", |
| 30 | + } |
| 31 | + |
| 32 | + it("invalid kid", () => { |
| 33 | + const headerPart = encodeObjectBase64Url({ |
| 34 | + ...validHeader, |
| 35 | + kid: undefined |
| 36 | + }) |
| 37 | + const jwt = `${headerPart}.payload.signature` |
| 38 | + expect(() => RS256Token.decode(jwt, currentTimestamp)).toThrowError( |
| 39 | + new JwtError( |
| 40 | + JwtErrorCode.NO_KID_IN_HEADER, |
| 41 | + `kid must be a string but got ${undefined}` |
| 42 | + ) |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it("invalid alg", () => { |
| 47 | + const headerPart = encodeObjectBase64Url({ |
| 48 | + ...validHeader, |
| 49 | + alg: "HS256" |
| 50 | + }) |
| 51 | + const jwt = `${headerPart}.payload.signature` |
| 52 | + expect(() => RS256Token.decode(jwt, currentTimestamp)).toThrowError( |
| 53 | + new JwtError( |
| 54 | + JwtErrorCode.INVALID_ARGUMENT, |
| 55 | + `algorithm must be RS256 but got ${"HS256"}` |
| 56 | + ) |
| 57 | + ) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + describe("payload", () => { |
| 62 | + test.each([ |
| 63 | + [ |
| 64 | + "aud", |
| 65 | + { |
| 66 | + ...payload, |
| 67 | + aud: "", |
| 68 | + }, |
| 69 | + new JwtError( |
| 70 | + JwtErrorCode.INVALID_ARGUMENT, |
| 71 | + `"aud" claim must be a string but got ""` |
| 72 | + ) |
| 73 | + ], |
| 74 | + [ |
| 75 | + "sub", |
| 76 | + { |
| 77 | + ...payload, |
| 78 | + sub: "", |
| 79 | + }, |
| 80 | + new JwtError( |
| 81 | + JwtErrorCode.INVALID_ARGUMENT, |
| 82 | + `"sub" claim must be a string but got ""` |
| 83 | + ) |
| 84 | + ], |
| 85 | + [ |
| 86 | + "iss", |
| 87 | + { |
| 88 | + ...payload, |
| 89 | + iss: "", |
| 90 | + }, |
| 91 | + new JwtError( |
| 92 | + JwtErrorCode.INVALID_ARGUMENT, |
| 93 | + `"iss" claim must be a string but got ""` |
| 94 | + ) |
| 95 | + ], |
| 96 | + [ |
| 97 | + "iat is in future", |
| 98 | + { |
| 99 | + ...payload, |
| 100 | + iat: currentTimestamp + 10000, // +10s |
| 101 | + }, |
| 102 | + new JwtError( |
| 103 | + JwtErrorCode.INVALID_ARGUMENT, |
| 104 | + `Incorrect "iat" claim must be a older than "${currentTimestamp}" (iat: "${currentTimestamp + 10000}")` |
| 105 | + ) |
| 106 | + ], |
| 107 | + [ |
| 108 | + "iat is now", |
| 109 | + { |
| 110 | + ...payload, |
| 111 | + iat: currentTimestamp, |
| 112 | + }, |
| 113 | + new JwtError( |
| 114 | + JwtErrorCode.INVALID_ARGUMENT, |
| 115 | + `Incorrect "iat" claim must be a older than "${currentTimestamp}" (iat: "${currentTimestamp}")` |
| 116 | + ) |
| 117 | + ], |
| 118 | + [ |
| 119 | + "exp is in past", |
| 120 | + { |
| 121 | + ...payload, |
| 122 | + exp: currentTimestamp - 10000, // -10s |
| 123 | + }, |
| 124 | + new JwtError( |
| 125 | + JwtErrorCode.INVALID_ARGUMENT, |
| 126 | + `Incorrect "exp" (expiration time) claim must be a newer than "${currentTimestamp}" (exp: "${currentTimestamp - 10000}")` |
| 127 | + ) |
| 128 | + ], |
| 129 | + ])("invalid %s", async (_, payload, wantErr) => { |
| 130 | + const testingKeyFetcher = await TestingKeyFetcher.withKeyPairGeneration("mismachKid") |
| 131 | + const jwt = await signJWT(kid, payload, testingKeyFetcher.getPrivateKey()) |
| 132 | + expect(() => RS256Token.decode(jwt, currentTimestamp)).toThrowError(wantErr) |
| 133 | + }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments