aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/jsonwebtoken/jwt.asymmetric_signing.test.js
blob: 848dadb1aeb49ad2d2b6a9649c8a8b790353de8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const PS_SUPPORTED = true;
import jwt from "jsonwebtoken";
import { expect, describe, it } from "bun:test";
import fs from "fs";
import path from "path";

function loadKey(filename) {
  return fs.readFileSync(path.join(__dirname, filename));
}

const algorithms = {
  RS256: {
    pub_key: loadKey("pub.pem"),
    priv_key: loadKey("priv.pem"),
    invalid_pub_key: loadKey("invalid_pub.pem"),
  },
  ES256: {
    // openssl ecparam -name secp256r1 -genkey -param_enc explicit -out ecdsa-private.pem
    priv_key: loadKey("ecdsa-private.pem"),
    // openssl ec -in ecdsa-private.pem -pubout -out ecdsa-public.pem
    pub_key: loadKey("ecdsa-public.pem"),
    invalid_pub_key: loadKey("ecdsa-public-invalid.pem"),
  },
};

if (PS_SUPPORTED) {
  algorithms.PS256 = {
    pub_key: loadKey("pub.pem"),
    priv_key: loadKey("priv.pem"),
    invalid_pub_key: loadKey("invalid_pub.pem"),
  };
}

describe("Asymmetric Algorithms", function () {
  Object.keys(algorithms).forEach(function (algorithm) {
    describe(algorithm, function () {
      const pub = algorithms[algorithm].pub_key;
      const priv = algorithms[algorithm].priv_key;

      // "invalid" means it is not the public key for the loaded "priv" key
      const invalid_pub = algorithms[algorithm].invalid_pub_key;

      describe("when signing a token", function () {
        const token = jwt.sign({ foo: "bar" }, priv, { algorithm: algorithm });

        it("should be syntactically valid", function () {
          expect(typeof token).toBe("string");
          expect(token.split(".")).toHaveLength(3);
        });

        describe("asynchronous", function () {
          (algorithm === "ES256" ? it.todo : it)("should validate with public key", function (done) {
            jwt.verify(token, pub, function (err, decoded) {
              if (err) return done(err);
              expect(decoded).toBeDefined();
              expect(decoded.foo).toBeTruthy();
              expect("bar").toBe(decoded.foo);
              done();
            });
          });

          it("should throw with invalid public key", function (done) {
            jwt.verify(token, invalid_pub, function (err, decoded) {
              expect(decoded).toBeUndefined();
              expect(err).toBeTruthy();
              done();
            });
          });
        });

        describe("synchronous", function () {
          (algorithm === "ES256" ? it.todo : it)("should validate with public key", function () {
            const decoded = jwt.verify(token, pub);
            expect(decoded).toBeDefined();
            expect(decoded.foo).toBeTruthy();
            expect("bar").toBe(decoded.foo);
          });

          it("should throw with invalid public key", function () {
            const jwtVerify = jwt.verify.bind(null, token, invalid_pub);
            expect(jwtVerify).toThrow();
          });
        });
      });

      describe("when signing a token with expiration", function () {
        (algorithm === "ES256" ? it.todo : it)("should be valid expiration", function (done) {
          const token = jwt.sign({ foo: "bar" }, priv, { algorithm: algorithm, expiresIn: "10m" });
          jwt.verify(token, pub, function (err, decoded) {
            if (err) return done(err);
            expect(decoded).toBeTruthy();
            expect(err).toBeNull();
            done();
          });
        });

        (algorithm === "ES256" ? it.todo : it)("should be invalid", function (done) {
          // expired token
          const token = jwt.sign({ foo: "bar" }, priv, { algorithm: algorithm, expiresIn: -1 * (10 * 60 * 1000) });
          jwt.verify(token, pub, function (err, decoded) {
            expect(decoded).toBeUndefined();
            expect(err).toBeDefined();
            expect(err.name).toBe("TokenExpiredError");
            expect(err.expiredAt).toBeInstanceOf(Date);
            expect(err).toBeInstanceOf(jwt.TokenExpiredError);
            done();
          });
        });

        (algorithm === "ES256" ? it.todo : it)("should NOT be invalid", function (done) {
          // expired token
          const token = jwt.sign({ foo: "bar" }, priv, { algorithm: algorithm, expiresIn: -1 * (10 * 60 * 1000) });

          jwt.verify(token, pub, { ignoreExpiration: true }, function (err, decoded) {
            expect(decoded).toBeDefined();
            expect(decoded.foo).toBeDefined();
            expect("bar").toBe(decoded.foo);
            done();
          });
        });
      });

      describe("when verifying a malformed token", function () {
        it("should throw", function (done) {
          jwt.verify("fruit.fruit.fruit", pub, function (err, decoded) {
            expect(decoded).toBeUndefined();
            expect(err).toBeDefined();
            expect(err.name).toBe("JsonWebTokenError");
            done();
          });
        });
      });

      describe("when decoding a jwt token with additional parts", function () {
        const token = jwt.sign({ foo: "bar" }, priv, { algorithm: algorithm });

        it("should throw", function (done) {
          jwt.verify(token + ".foo", pub, function (err, decoded) {
            expect(decoded).toBeUndefined();
            expect(err).toBeDefined();
            done();
          });
        });
      });

      describe("when decoding a invalid jwt token", function () {
        it("should return null", function (done) {
          const payload = jwt.decode("whatever.token");
          expect(payload).toBeNull();
          done();
        });
      });

      describe("when decoding a valid jwt token", function () {
        it("should return the payload", function (done) {
          const obj = { foo: "bar" };
          const token = jwt.sign(obj, priv, { algorithm: algorithm });
          const payload = jwt.decode(token);
          expect(payload.foo).toEqual(obj.foo);
          done();
        });
        it("should return the header and payload and signature if complete option is set", function (done) {
          const obj = { foo: "bar" };
          const token = jwt.sign(obj, priv, { algorithm: algorithm });
          const decoded = jwt.decode(token, { complete: true });
          expect(decoded.payload.foo).toEqual(obj.foo);
          expect(decoded.header).toStrictEqual({ typ: "JWT", alg: algorithm });
          expect(typeof decoded.signature).toBe("string");
          done();
        });
      });
    });
  });

  describe("when signing a token with an unsupported private key type", function () {
    it.todo("should throw an error", function () {
      const obj = { foo: "bar" };
      const key = loadKey("dsa-private.pem");
      const algorithm = "RS256";

      expect(function () {
        jwt.sign(obj, key, { algorithm });
      }).toThrow('Unknown key type "dsa".');
    });
  });

  describe("when signing a token with an incorrect private key type", function () {
    it("should throw a validation error if key validation is enabled", function () {
      const obj = { foo: "bar" };
      const key = loadKey("rsa-private.pem");
      const algorithm = "ES256";

      expect(function () {
        jwt.sign(obj, key, { algorithm });
      }).toThrow(/"alg" parameter for "rsa" key type must be one of:/);
    });

    it("should throw an unknown error if key validation is disabled", function () {
      const obj = { foo: "bar" };
      const key = loadKey("rsa-private.pem");
      const algorithm = "ES256";

      expect(function () {
        jwt.sign(obj, key, { algorithm, allowInvalidAsymmetricKeyTypes: true });
      }).not.toThrow(/"alg" parameter for "rsa" key type must be one of:/);
    });
  });
});