diff --git a/java/use-webhooks/webhooks_vnext_validate_signature.java b/java/use-webhooks/webhooks_vnext_validate_signature.java new file mode 100644 index 00000000..0a08454f --- /dev/null +++ b/java/use-webhooks/webhooks_vnext_validate_signature.java @@ -0,0 +1,13 @@ +// Tip: Find more about Java SDK at https://kontent.ai/learn/java +import javax.crypto; +import javax.crypto.spec; +import javax.xml.bind; + +// Generates a payload hash to compare with the 'X-Kontent-ai-Signature' header value +public static String generateHash(String message, String secret) throws Exception { + Mac sha256Hmac = Mac.getInstance("HmacSHA256"); + SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); + sha256Hmac.init(secretKeySpec); + + return Base64.getEncoder().encodeToString(sha256Hmac.doFinal(message.getBytes(StandardCharsets.UTF_8))); +} diff --git a/js/using-webhooks/webhooks_vnext_validate_signature.js b/js/using-webhooks/webhooks_vnext_validate_signature.js new file mode 100644 index 00000000..fd177b4e --- /dev/null +++ b/js/using-webhooks/webhooks_vnext_validate_signature.js @@ -0,0 +1,11 @@ +// Tip: Find more about JS/TS SDKs at https://kontent.ai/learn/javascript +const signatureHelper = require('@kontent-ai/webhook-helper'); + +// Generates a payload hash and compares it to the payload signature +const isValidSignature = (req, secret) => { + return signatureHelper.isValidSignatureFromString( + req.body, // Use raw body data from the request, i.e., by using body-parser + secret, + req.headers['x-kontent-ai-signature'] + ); +}; diff --git a/net/using-webhooks/webhooks_vnext_validate_signature.cs b/net/using-webhooks/webhooks_vnext_validate_signature.cs new file mode 100644 index 00000000..e236d9a4 --- /dev/null +++ b/net/using-webhooks/webhooks_vnext_validate_signature.cs @@ -0,0 +1,18 @@ +// Tip: Find more about .NET SDKs at https://kontent.ai/learn/net +using System; +using System.Security.Cryptography; +using System.Text; + +// Generates a payload hash to compare with the 'X-Kontent-ai-Signature' header value +private static string GenerateHash(string message, string secret) +{ + secret = secret ?? ""; + UTF8Encoding SafeUTF8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); + byte[] keyBytes = SafeUTF8.GetBytes(secret); + byte[] messageBytes = SafeUTF8.GetBytes(message); + using (HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes)) + { + byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); + return Convert.ToBase64String(hashmessage); + } +} diff --git a/php/use-webhooks/webhooks_vnext_validate_signature.php b/php/use-webhooks/webhooks_vnext_validate_signature.php new file mode 100644 index 00000000..5ebfc671 --- /dev/null +++ b/php/use-webhooks/webhooks_vnext_validate_signature.php @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/ts/use-webhooks/webhooks_vnext_validate_signature.ts b/ts/use-webhooks/webhooks_vnext_validate_signature.ts new file mode 100644 index 00000000..c7793658 --- /dev/null +++ b/ts/use-webhooks/webhooks_vnext_validate_signature.ts @@ -0,0 +1,11 @@ +// Tip: Find more about JS/TS SDKs at https://kontent.ai/learn/javascript +import { signatureHelper } from '@kontent-ai/webhook-helper'; + +// Example of generating the hash to verify the notification +const isValidSignature = (req, secret) => { + return signatureHelper.isValidSignatureFromString( + req.body, // Use raw body data from the request, i.e., by using body-parser + secret, + req.headers['x-kontent-ai-signature'] + ); +};