Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
[#IOCIT-373] refactor based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Mar 15, 2023
1 parent 51b9eec commit b2a775e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
36 changes: 17 additions & 19 deletions utils/httpSignature.verifiers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import * as crypto from "crypto";
import { JsonWebKey } from "crypto";
import {
AlgorithmTypes,
verifyEcdsaSha256
} from "@mattrglobal/http-signatures";
import { algMap } from "@mattrglobal/http-signatures";

// ----------------------
// Custom Verifiers
// ----------------------

export const verifyRsaPssSha256 = (key: JsonWebKey) => async (
/**
* Builder for `rsa-pss-sha256` signature verifier.
* It's based on the`rsa-pss-sha512` one, defined in @mattrglobal/http-signatures library.
* See https://github.com/mattrglobal/http-signatures/blob/master/src/common/cryptoPrimatives.ts#L97
*
* @param key the public key
* @returns a function that takes the data and the signature
* and return the comparison between them, based on the algorithm and the public key
*/
export const getVerifyRsaPssSha256 = (key: JsonWebKey) => async (
data: Uint8Array,
signature: Uint8Array
): Promise<boolean> => {
Expand All @@ -27,34 +33,26 @@ export const verifyRsaPssSha256 = (key: JsonWebKey) => async (
);
};

export type SupportedAlgTypes = keyof typeof myAlgMap;
export type SupportedAlgTypes = keyof typeof extendedAlgMap;

const isSupportedAlg = (alg: string): alg is SupportedAlgTypes =>
alg === "ecdsa-p256-sha256" || alg === "rsa-pss-sha256";

export const myAlgMap = {
["ecdsa-p256-sha256"]: {
verify: verifyEcdsaSha256
},
export const extendedAlgMap = {
...algMap,
["rsa-pss-sha256"]: {
verify: verifyRsaPssSha256
verify: getVerifyRsaPssSha256
}
};

export const customVerify = (keyMap: {
readonly [keyid: string]: { readonly key: JsonWebKey };
}) => async (
signatureParams: { readonly keyid: string; readonly alg: AlgorithmTypes },
signatureParams: { readonly keyid: string; readonly alg: SupportedAlgTypes },
data: Uint8Array,
signature: Uint8Array
): Promise<boolean> => {
if (!isSupportedAlg(signatureParams.alg)) {
throw new Error("Unsupported algorithm");
}
if (keyMap[signatureParams.keyid] === undefined) {
return Promise.resolve(false);
}
return myAlgMap[signatureParams.alg].verify(
return extendedAlgMap[signatureParams.alg].verify(
keyMap[signatureParams.keyid].key
)(data, signature);
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ describe("HttpMessageSignatureMiddleware - Success", () => {

const res = await HttpMessageSignatureMiddleware()(mockReq);

console.log(res);

expect(res).toMatchObject(E.right(true));
});

Expand Down

0 comments on commit b2a775e

Please sign in to comment.