-
Notifications
You must be signed in to change notification settings - Fork 2
Add webhook verification support #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/schematic/webhook/WebhookSignatureException.java
This file contains hidden or 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,26 @@ | ||
| package com.schematic.webhook; | ||
|
|
||
| /** | ||
| * Exception thrown when webhook signature verification fails. | ||
| */ | ||
| public class WebhookSignatureException extends RuntimeException { | ||
|
|
||
| /** | ||
| * Constructs a new WebhookSignatureException with the specified detail message. | ||
| * | ||
| * @param message the detail message | ||
| */ | ||
| public WebhookSignatureException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new WebhookSignatureException with the specified detail message and cause. | ||
| * | ||
| * @param message the detail message | ||
| * @param cause the cause | ||
| */ | ||
| public WebhookSignatureException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
179 changes: 179 additions & 0 deletions
179
src/main/java/com/schematic/webhook/WebhookVerifier.java
This file contains hidden or 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,179 @@ | ||
| package com.schematic.webhook; | ||
|
|
||
| import javax.crypto.Mac; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.InvalidKeyException; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Utilities for verifying the signatures of Schematic webhooks. | ||
| * <p> | ||
| * Schematic signs webhook payloads using HMAC-SHA256. This class provides methods | ||
| * to verify these signatures. | ||
| */ | ||
| public class WebhookVerifier { | ||
|
|
||
| /** | ||
| * Header containing the webhook signature. | ||
| */ | ||
| public static final String WEBHOOK_SIGNATURE_HEADER = "X-Schematic-Webhook-Signature"; | ||
|
|
||
| /** | ||
| * Header containing the webhook timestamp. | ||
| */ | ||
| public static final String WEBHOOK_TIMESTAMP_HEADER = "X-Schematic-Webhook-Timestamp"; | ||
|
|
||
| private static final String HMAC_SHA256 = "HmacSHA256"; | ||
|
|
||
| /** | ||
| * Verifies the signature of a webhook request. | ||
| * | ||
| * @param body The request body as a string | ||
| * @param headers Map of HTTP headers | ||
| * @param secret The webhook secret | ||
| * @throws WebhookSignatureException if the signature is invalid | ||
| */ | ||
| public static void verifyWebhookSignature(String body, Map<String, String> headers, String secret) | ||
| throws WebhookSignatureException { | ||
|
|
||
| // Extract signature and timestamp headers | ||
| String signature = headers.get(WEBHOOK_SIGNATURE_HEADER); | ||
| String timestamp = headers.get(WEBHOOK_TIMESTAMP_HEADER); | ||
|
|
||
| // Verify signature | ||
| verifySignature(body, signature, timestamp, secret); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies the signature of a webhook payload. | ||
| * | ||
| * @param body The webhook payload | ||
| * @param signature The signature header value | ||
| * @param timestamp The timestamp header value | ||
| * @param secret The webhook secret | ||
| * @throws WebhookSignatureException if the signature is invalid | ||
| */ | ||
| public static void verifySignature(String body, String signature, String timestamp, String secret) | ||
| throws WebhookSignatureException { | ||
|
|
||
| if (signature == null || signature.isEmpty()) { | ||
| throw new WebhookSignatureException("Missing webhook signature"); | ||
| } | ||
|
|
||
| if (timestamp == null || timestamp.isEmpty()) { | ||
| throw new WebhookSignatureException("Missing webhook timestamp"); | ||
| } | ||
|
|
||
| // Compute expected signature | ||
| String expectedSignature = computeHexSignature(body, timestamp, secret); | ||
|
|
||
| // Compare signatures using constant-time comparison | ||
| if (!constantTimeEquals(hexToBytes(expectedSignature), hexToBytes(signature))) { | ||
| throw new WebhookSignatureException("Invalid signature"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Computes the hex-encoded HMAC-SHA256 signature for a webhook payload. | ||
| * | ||
| * @param body The webhook payload | ||
| * @param timestamp The timestamp | ||
| * @param secret The webhook secret | ||
| * @return The hex-encoded signature | ||
| * @throws WebhookSignatureException if an error occurs during signature computation | ||
| */ | ||
| public static String computeHexSignature(String body, String timestamp, String secret) | ||
| throws WebhookSignatureException { | ||
|
|
||
| byte[] signature = computeSignature(body, timestamp, secret); | ||
| return bytesToHex(signature); | ||
| } | ||
|
|
||
| /** | ||
| * Computes the HMAC-SHA256 signature for a webhook payload. | ||
| * | ||
| * @param body The webhook payload | ||
| * @param timestamp The timestamp | ||
| * @param secret The webhook secret | ||
| * @return The signature bytes | ||
| * @throws WebhookSignatureException if an error occurs during signature computation | ||
| */ | ||
| public static byte[] computeSignature(String body, String timestamp, String secret) | ||
| throws WebhookSignatureException { | ||
|
|
||
| try { | ||
| // Create message by concatenating body and timestamp | ||
| String message = body + "+" + timestamp; | ||
| byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| // Create HMAC-SHA256 instance | ||
| SecretKeySpec keySpec = new SecretKeySpec( | ||
| secret.getBytes(StandardCharsets.UTF_8), | ||
| HMAC_SHA256 | ||
| ); | ||
| Mac mac = Mac.getInstance(HMAC_SHA256); | ||
| mac.init(keySpec); | ||
|
|
||
| // Compute and return signature | ||
| return mac.doFinal(messageBytes); | ||
| } catch (NoSuchAlgorithmException | InvalidKeyException e) { | ||
| throw new WebhookSignatureException("Error computing signature", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a byte array to a hex string. | ||
| * | ||
| * @param bytes The byte array | ||
| * @return The hex string | ||
| */ | ||
| private static String bytesToHex(byte[] bytes) { | ||
| StringBuilder result = new StringBuilder(); | ||
| for (byte b : bytes) { | ||
| result.append(String.format("%02x", b)); | ||
| } | ||
| return result.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a hex string to a byte array. | ||
| * | ||
| * @param hex The hex string | ||
| * @return The byte array | ||
| * @throws WebhookSignatureException if the hex string is invalid | ||
| */ | ||
| private static byte[] hexToBytes(String hex) throws WebhookSignatureException { | ||
| try { | ||
| int len = hex.length(); | ||
| byte[] data = new byte[len / 2]; | ||
| for (int i = 0; i < len; i += 2) { | ||
| data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) | ||
| + Character.digit(hex.charAt(i + 1), 16)); | ||
| } | ||
| return data; | ||
| } catch (Exception e) { | ||
| throw new WebhookSignatureException("Invalid signature format", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compares two byte arrays in constant time to prevent timing attacks. | ||
| * | ||
| * @param a First byte array | ||
| * @param b Second byte array | ||
| * @return true if the arrays are equal, false otherwise | ||
| */ | ||
| private static boolean constantTimeEquals(byte[] a, byte[] b) { | ||
| if (a.length != b.length) { | ||
| return false; | ||
| } | ||
|
|
||
| int result = 0; | ||
| for (int i = 0; i < a.length; i++) { | ||
| result |= a[i] ^ b[i]; | ||
| } | ||
| return result == 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be beneficial to validate that the hex string has an even length before processing, as an odd-length string would indicate an invalid format.