-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support mTLS certificate upload (#835)
* feat: Add mTLS and custom HMAC support to webhoks * remove resetNonce * use env vars for client id/secret * update tests * debug steps * remove async * remove unneeded changes
- Loading branch information
Showing
18 changed files
with
310 additions
and
86 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
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
3 changes: 3 additions & 0 deletions
3
src/prisma/migrations/20241031010103_add_mtls_configuration/migration.sql
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,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "configuration" ADD COLUMN "mtlsCertificateEncrypted" TEXT, | ||
ADD COLUMN "mtlsPrivateKeyEncrypted" TEXT; |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import crypto from "crypto"; | ||
import CryptoJS from "crypto-js"; | ||
import crypto from "node:crypto"; | ||
import { env } from "./env"; | ||
|
||
export const encrypt = (data: string): string => { | ||
export function encrypt(data: string): string { | ||
return CryptoJS.AES.encrypt(data, env.ENCRYPTION_PASSWORD).toString(); | ||
}; | ||
} | ||
|
||
export const decrypt = (data: string, password: string) => { | ||
export function decrypt(data: string, password: string) { | ||
return CryptoJS.AES.decrypt(data, password).toString(CryptoJS.enc.Utf8); | ||
}; | ||
} | ||
|
||
export const isWellFormedPublicKey = (key: string) => { | ||
export function isWellFormedPublicKey(key: string) { | ||
try { | ||
crypto.createPublicKey(key); | ||
return true; | ||
} catch (_e) { | ||
return false; | ||
} | ||
}; | ||
} |
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,63 @@ | ||
import { createHmac } from "node:crypto"; | ||
|
||
/** | ||
* Generates an HMAC-256 secret to set in the "Authorization" header. | ||
* | ||
* @param webhookUrl - The URL to call. | ||
* @param body - The request body. | ||
* @param timestamp - The request timestamp. | ||
* @param nonce - A unique string for this request. Should not be re-used. | ||
* @param clientId - Your application's client id. | ||
* @param clientSecret - Your application's client secret. | ||
* @returns | ||
*/ | ||
export const generateSecretHmac256 = (args: { | ||
webhookUrl: string; | ||
body: Record<string, unknown>; | ||
timestamp: Date; | ||
nonce: string; | ||
clientId: string; | ||
clientSecret: string; | ||
}): string => { | ||
const { webhookUrl, body, timestamp, nonce, clientId, clientSecret } = args; | ||
|
||
// Create the body hash by hashing the payload. | ||
const bodyHash = createHmac("sha256", clientSecret) | ||
.update(JSON.stringify(body), "utf8") | ||
.digest("base64"); | ||
|
||
// Create the signature hash by hashing the signature. | ||
const ts = timestamp.getTime(); // timestamp expected in milliseconds | ||
const httpMethod = "POST"; | ||
const url = new URL(webhookUrl); | ||
const resourcePath = url.pathname; | ||
const host = url.hostname; | ||
const port = url.port | ||
? Number.parseInt(url.port) | ||
: url.protocol === "https:" | ||
? 443 | ||
: 80; | ||
|
||
const signature = [ | ||
ts, | ||
nonce, | ||
httpMethod, | ||
resourcePath, | ||
host, | ||
port, | ||
bodyHash, | ||
"", // to insert a newline at the end | ||
].join("\n"); | ||
|
||
const signatureHash = createHmac("sha256", clientSecret) | ||
.update(signature, "utf8") | ||
.digest("base64"); | ||
|
||
return [ | ||
`MAC id="${clientId}"`, | ||
`ts="${ts}"`, | ||
`nonce="${nonce}"`, | ||
`bodyhash="${bodyHash}"`, | ||
`mac="${signatureHash}"`, | ||
].join(","); | ||
}; |
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
Oops, something went wrong.