Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Jan 1, 2024
1 parent dba41ee commit eea63cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
32 changes: 10 additions & 22 deletions src/vendors/jwks/jwks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);
});
Expand Down
5 changes: 2 additions & 3 deletions src/vendors/jwks/jwks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
37 changes: 19 additions & 18 deletions src/vendors/jwt/jwt-verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
verifyHSTokenWithSecretString,
checkJwtFields,
parseJwt,
checkTokenValidness,
checkTokenValidness
// checkTokenValidness
} from "./jwt-verify";
import {
Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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(
{
Expand Down Expand Up @@ -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);
});
});

0 comments on commit eea63cd

Please sign in to comment.