|
| 1 | +[](https://github.com/Cosium/standard-webhooks-consumer/actions/workflows/ci.yml) |
| 2 | + |
| 3 | + |
1 | 4 | # standard-webhooks-consumer
|
2 |
| -https://www.standardwebhooks.com/ consumer side java library |
| 5 | + |
| 6 | +https://www.standardwebhooks.com/ consumer side java library. |
| 7 | + |
| 8 | +# Maven dependency |
| 9 | + |
| 10 | +```xml |
| 11 | +<dependency> |
| 12 | + <groupId>com.cosium.standard_webhooks_consumer</groupId> |
| 13 | + <artifactId>standard-webhooks-consumer</artifactId> |
| 14 | + <version>${standard-webhooks-consumer.version}</version> |
| 15 | +</dependency> |
| 16 | +``` |
| 17 | + |
| 18 | +# Signature verification |
| 19 | + |
| 20 | +```java |
| 21 | +public class App { |
| 22 | + |
| 23 | + public void verifySymmetricSignature() throws WebhookSignatureVerificationException { |
| 24 | + |
| 25 | + WebhookSignatureVerifier verifier = |
| 26 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 27 | + .build(); |
| 28 | + HttpHeaders httpHeaders = |
| 29 | + createHttpHeaders( |
| 30 | + Map.of( |
| 31 | + "webhook-id", |
| 32 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 33 | + "webhook-timestamp", |
| 34 | + String.valueOf(1737987215), |
| 35 | + "webhook-signature", |
| 36 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 37 | + verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}"); |
| 38 | + } |
| 39 | + |
| 40 | + public void verifyAsymmetricSignature() throws WebhookSignatureVerificationException { |
| 41 | + |
| 42 | + WebhookSignatureVerifier verifier = |
| 43 | + WebhookSignatureVerifier.builder("whpk_MCowBQYDK2VwAyEAkp3dScDPIzT1CwUFUMdzyPbWOAQaCF9z4ucuKuZD7Io=") |
| 44 | + .build(); |
| 45 | + |
| 46 | + HttpHeaders httpHeaders = |
| 47 | + createHttpHeaders( |
| 48 | + Map.of( |
| 49 | + "webhook-id", |
| 50 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 51 | + "webhook-timestamp", |
| 52 | + String.valueOf(1737987215), |
| 53 | + "webhook-signature", |
| 54 | + "v1a,XVbiOe+IzCKsXBuhb52iHLroqxFJofJNMQRL80I2kWO0+kXu2gcqgXAzontxDDgpMDw6SMh4sjzr+67EmUUzDg==")); |
| 55 | + |
| 56 | + verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}"); |
| 57 | + } |
| 58 | + |
| 59 | + private HttpHeaders createHttpHeaders(Map<String, String> headers) { |
| 60 | + return HttpHeaders.of( |
| 61 | + headers.entrySet().stream() |
| 62 | + .map(entry -> Map.entry(entry.getKey(), List.of(entry.getValue()))) |
| 63 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), |
| 64 | + (s, s2) -> true); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
0 commit comments