Skip to content

Commit

Permalink
feat: rename IsValidWebhookSignature to isSignatureValid in `WebhookV…
Browse files Browse the repository at this point in the history
…alidation
  • Loading branch information
ilfa committed Nov 22, 2024
1 parent 817b444 commit f49c1c2
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-pigs-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fingerprint-pro-server-api-java-sdk": major
---

Rename `IsValidWebhookSignature` to `isSignatureValid` in `WebhookValidation`
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class WebhookController {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing fpjs-event-signature header");
}

final boolean isValidSignature = WebhookValidation.isValidWebhookSignature(signature, webhook, secret);
final boolean isValidSignature = WebhookValidation.isSignatureValid(signature, webhook, secret);
if (!isValidSignature) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Webhook signature is not valid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void main(String... args) {
final byte[] data = "data".getBytes(StandardCharsets.UTF_8);

try {
boolean isSignatureValid = WebhookValidation.isValidWebhookSignature(header, data, secret);
boolean isSignatureValid = WebhookValidation.isSignatureValid(header, data, secret);
if (isSignatureValid) {
System.out.println("Webhook signature is valid");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class WebhookValidation {
* @throws NoSuchAlgorithmException in case HMAC-SHA-256 isn't available
* @throws IllegalArgumentException if an invalid key is provided
*/
public static boolean isValidWebhookSignature(String header, byte[] data, String secret) throws NoSuchAlgorithmException {
public static boolean isSignatureValid(String header, byte[] data, String secret) throws NoSuchAlgorithmException {
String[] signatures = header.split(",");
for (String signature : signatures) {
String[] parts = signature.split("=");
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/test/java/com/fingerprint/WebhookValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@ public class WebhookValidationTest {

@Test
public void validHeaderTest() throws NoSuchAlgorithmException {
boolean result = WebhookValidation.isValidWebhookSignature(validHeader, data, secret);
boolean result = WebhookValidation.isSignatureValid(validHeader, data, secret);
Assertions.assertTrue(result);
}

@Test
public void invalidHeaderTest() throws NoSuchAlgorithmException {
boolean result = WebhookValidation.isValidWebhookSignature("v2=wrong", data, secret);
boolean result = WebhookValidation.isSignatureValid("v2=wrong", data, secret);
assert !result;
}

@Test
public void headerWithoutVersionTest() throws NoSuchAlgorithmException {
boolean result = WebhookValidation.isValidWebhookSignature("secretonly", data, secret);
boolean result = WebhookValidation.isSignatureValid("secretonly", data, secret);
assert !result;
}

@Test
public void emptySecretTest() throws NoSuchAlgorithmException {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
WebhookValidation.isValidWebhookSignature("v1=value", data, "");
WebhookValidation.isSignatureValid("v1=value", data, "");
});
}

@Test
public void emptyDataTest() throws NoSuchAlgorithmException {
boolean result = WebhookValidation.isValidWebhookSignature(validHeader, "".getBytes(), secret);
boolean result = WebhookValidation.isSignatureValid(validHeader, "".getBytes(), secret);
assert !result;
}

Expand Down
2 changes: 1 addition & 1 deletion template/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class WebhookController {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing fpjs-event-signature header");
}
final boolean isValidSignature = WebhookValidation.isValidWebhookSignature(signature, webhook, secret);
final boolean isValidSignature = WebhookValidation.isSignatureValid(signature, webhook, secret);
if (!isValidSignature) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Webhook signature is not valid");
}
Expand Down

0 comments on commit f49c1c2

Please sign in to comment.