This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
generated from pagopa/io-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from pagopa/IOCIT-373--add-custom-verifiers
[#IOCIT-373] Add custom verifiers to signature middleware
- Loading branch information
Showing
3 changed files
with
151 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as crypto from "crypto"; | ||
import { JsonWebKey } from "crypto"; | ||
import { algMap } from "@mattrglobal/http-signatures"; | ||
|
||
// ---------------------- | ||
// Custom Verifiers | ||
// ---------------------- | ||
|
||
/** | ||
* 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/v4.0.1/src/common/cryptoPrimatives.ts | ||
* | ||
* @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> => { | ||
const keyObject = crypto.createPublicKey({ format: "jwk", key }); | ||
return crypto | ||
.createVerify("SHA256") | ||
.update(data) | ||
.verify( | ||
{ | ||
dsaEncoding: "ieee-p1363", | ||
key: keyObject, | ||
padding: crypto.constants.RSA_PKCS1_PSS_PADDING | ||
}, | ||
signature | ||
); | ||
}; | ||
|
||
export type SupportedAlgTypes = keyof typeof extendedAlgMap; | ||
|
||
export const extendedAlgMap = { | ||
["rsa-pss-sha256"]: { | ||
verify: getVerifyRsaPssSha256 | ||
}, | ||
...algMap | ||
}; | ||
|
||
export const customVerify = (keyMap: { | ||
readonly [keyid: string]: { readonly key: JsonWebKey }; | ||
}) => async ( | ||
signatureParams: { readonly keyid: string; readonly alg: SupportedAlgTypes }, | ||
data: Uint8Array, | ||
signature: Uint8Array | ||
): Promise<boolean> => { | ||
if (keyMap[signatureParams.keyid] === undefined) { | ||
return Promise.resolve(false); | ||
} | ||
return extendedAlgMap[signatureParams.alg].verify( | ||
keyMap[signatureParams.keyid].key | ||
)(data, signature); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters