From eea63cd76d1e03e3dfef89d5bdc0d40c779e29f5 Mon Sep 17 00:00:00 2001 From: David Barrat Date: Mon, 1 Jan 2024 04:15:21 +0100 Subject: [PATCH] cleanup --- src/vendors/jwks/jwks.test.ts | 32 ++++++++------------------ src/vendors/jwks/jwks.ts | 5 ++-- src/vendors/jwt/jwt-verify.test.ts | 37 +++++++++++++++--------------- 3 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/vendors/jwks/jwks.test.ts b/src/vendors/jwks/jwks.test.ts index ad517e8..551a784 100644 --- a/src/vendors/jwks/jwks.test.ts +++ b/src/vendors/jwks/jwks.test.ts @@ -13,8 +13,8 @@ import fetch, { Headers } from "node-fetch"; // https://stackoverflow.com/a/75956506/8483084 if (!globalThis.fetch) { - globalThis.fetch = fetch - globalThis.Headers = Headers + globalThis.fetch = fetch; + globalThis.Headers = Headers; } import * as c from "../../constants"; @@ -462,7 +462,6 @@ it("signs with Ed25519 key pair", async () => { expect(verifiedPayload?.payload).toMatchObject(payload); expect(verifiedPayload?.protectedHeader).toMatchObject(protectedHeaders); - }); it("verifies Ed448 Key pair", async () => { const crypto = require("crypto"); @@ -493,13 +492,7 @@ it("verifies Ed448 Key pair", async () => { expect(verifiedPayload?.protectedHeader).toMatchObject(protectedHeaders); }); - - - it("verifies correctly token with public uri", async () => { - - - const tenantUuid2 = "d84ddef4-81dd-4ce6-9594-03ac52cac367"; const applicationUuid2 = "b867db48-4e11-4cae-bb03-086dc97c8ddd"; const keyPairES512 = await getKeyPair({ @@ -535,20 +528,15 @@ it("verifies correctly token with public uri", async () => { let verified: ITokenExtractedWithPubKey | undefined; - verified = await verifyTokenWithPublicKey(signedPayloadEs512, null, { - jwksUri - }); - - - expect(verified.protectedHeader).toEqual({ alg: "ES512", type: "jwt" }); - expect(verified.payload).toEqual({ - urn: "urn:test:test", - kid: keyPairES512?.kid - }); - - if (verified) { + verified = await verifyTokenWithPublicKey(signedPayloadEs512, null, { + jwksUri + }); - } + expect(verified.protectedHeader).toEqual({ alg: "ES512", type: "jwt" }); + expect(verified.payload).toEqual({ + urn: "urn:test:test", + kid: keyPairES512?.kid + }); scopeNock.persist(false); }); diff --git a/src/vendors/jwks/jwks.ts b/src/vendors/jwks/jwks.ts index 833d1ac..91b3e32 100644 --- a/src/vendors/jwks/jwks.ts +++ b/src/vendors/jwks/jwks.ts @@ -96,8 +96,7 @@ export const verifyTokenWithPublicKey = async ( decoded = await jwtVerify(token, keyLike, { issuer: opts?.requiredIssuer, - audience: opts?.requiredAudiences, - + audience: opts?.requiredAudiences }); return decoded; } else if (!!publicKey) { @@ -113,7 +112,7 @@ export const verifyTokenWithPublicKey = async ( JWKS = createRemoteJWKSet(new URL(opts?.jwksUri), { headers: { "Content-Type": "application/json", - "User-Agent": "authdog-jwks-rsa", + "User-Agent": "authdog-jwks-rsa" } }); } else { diff --git a/src/vendors/jwt/jwt-verify.test.ts b/src/vendors/jwt/jwt-verify.test.ts index b026e48..dacb8f5 100644 --- a/src/vendors/jwt/jwt-verify.test.ts +++ b/src/vendors/jwt/jwt-verify.test.ts @@ -3,7 +3,7 @@ import { verifyHSTokenWithSecretString, checkJwtFields, parseJwt, - checkTokenValidness, + checkTokenValidness // checkTokenValidness } from "./jwt-verify"; import { @@ -20,11 +20,10 @@ import fetch, { Headers } from "node-fetch"; // https://stackoverflow.com/a/75956506/8483084 if (!globalThis.fetch) { - globalThis.fetch = fetch - globalThis.Headers = Headers + globalThis.fetch = fetch; + globalThis.Headers = Headers; } - const DUMMY_HS256_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; const DUMMY_NON_JWT_TOKEN = "hello-i-am-not-a-jwt"; @@ -264,7 +263,6 @@ it("parses token (payload and header)", async () => { }); it("verifies a token with checkTokenValidness signed with ES512 key - jwk", async () => { - const keyPairES512 = await getKeyPair({ algorithmIdentifier: Algs.ES512, keySize: 4096 @@ -278,18 +276,21 @@ it("verifies a token with checkTokenValidness signed with ES512 key - jwk", asyn const jwks = { keys: [ - { - crv: 'P-256', - x: 'fqCXPnWs3sSfwztvwYU9SthmRdoT4WCXxS8eD8icF6U', - y: 'nP6GIc42c61hoKqPcZqkvzhzIJkBV3Jw3g8sGG7UeP8', - kty: 'EC', - kid: 'one', - }, - ...keys - ], - } - - const scopeNock = nock('https://as.example.com').get('/jwks').once().reply(200, jwks) + { + crv: "P-256", + x: "fqCXPnWs3sSfwztvwYU9SthmRdoT4WCXxS8eD8icF6U", + y: "nP6GIc42c61hoKqPcZqkvzhzIJkBV3Jw3g8sGG7UeP8", + kty: "EC", + kid: "one" + }, + ...keys + ] + }; + + const scopeNock = nock("https://as.example.com") + .get("/jwks") + .once() + .reply(200, jwks); const signedPayloadEs512 = await signJwtWithPrivateKey( { @@ -364,4 +365,4 @@ it("throws an error while verifying token with public uri whose key is missing f ).rejects.toThrow(c.JWK_NO_APPLICABLE_KEY); scopeNock.persist(false); -}); \ No newline at end of file +});