Skip to content

Commit d4ea268

Browse files
committed
docs: use bytes[] input for Webhook signature validation, use better syntax
1 parent 938e1fd commit d4ea268

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,23 @@ This SDK provides utility method for verifying the HMAC signature of the incomin
242242
class WebhookController {
243243

244244
@PostMapping("/api/webhook")
245-
@ResponseBody
246-
public ResponseEntity<String> webhookHandler(@RequestBody String webhook, @RequestHeader HttpHeaders headers) {
245+
public ResponseEntity<String> webhookHandler(@RequestBody byte[] webhook, @RequestHeader HttpHeaders headers) {
247246
final String secret = System.getenv("WEBHOOK_SIGNATURE_SECRET");
248247
if (secret == null || secret.isEmpty()) {
249-
return new ResponseEntity<String>("Secret key is not configured", HttpStatus.INTERNAL_SERVER_ERROR);
248+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Secret key is not configured");
250249
}
251250

252251
final String signature = headers.getFirst("fpjs-event-signature");
253252
if (signature == null || signature.isEmpty()) {
254-
return new ResponseEntity<String>("Missing fpjs-event-signature header", HttpStatus.BAD_REQUEST);
253+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing fpjs-event-signature header");
255254
}
256255

257-
final boolean isValidSignature = Webhook.isValidWebhookSignature(signature, webhook.getBytes(StandardCharsets.UTF_8), secret);
256+
final boolean isValidSignature = Webhook.isValidWebhookSignature(signature, webhook, secret);
258257
if (!isValidSignature) {
259-
return new ResponseEntity<String>("Webhook signature is not valid", HttpStatus.BAD_REQUEST);
258+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Webhook signature is not valid");
260259
}
261260

262-
return new ResponseEntity<String>("Webhook received", HttpStatus.OK);
261+
return ResponseEntity.ok("Webhook received");
263262
}
264263
}
265264
```

template/README.mustache

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,24 +269,23 @@ This SDK provides utility method for verifying the HMAC signature of the incomin
269269
class WebhookController {
270270
271271
@PostMapping("/api/webhook")
272-
@ResponseBody
273-
public ResponseEntity<String> webhookHandler(@RequestBody String webhook, @RequestHeader HttpHeaders headers) {
272+
public ResponseEntity<String> webhookHandler(@RequestBody byte[] webhook, @RequestHeader HttpHeaders headers) {
274273
final String secret = System.getenv("WEBHOOK_SIGNATURE_SECRET");
275274
if (secret == null || secret.isEmpty()) {
276-
return new ResponseEntity<String>("Secret key is not configured", HttpStatus.INTERNAL_SERVER_ERROR);
275+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Secret key is not configured");
277276
}
278277
279278
final String signature = headers.getFirst("fpjs-event-signature");
280279
if (signature == null || signature.isEmpty()) {
281-
return new ResponseEntity<String>("Missing fpjs-event-signature header", HttpStatus.BAD_REQUEST);
280+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing fpjs-event-signature header");
282281
}
283282
284-
final boolean isValidSignature = Webhook.isValidWebhookSignature(signature, webhook.getBytes(StandardCharsets.UTF_8), secret);
283+
final boolean isValidSignature = Webhook.isValidWebhookSignature(signature, webhook, secret);
285284
if (!isValidSignature) {
286-
return new ResponseEntity<String>("Webhook signature is not valid", HttpStatus.BAD_REQUEST);
285+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Webhook signature is not valid");
287286
}
288287
289-
return new ResponseEntity<String>("Webhook received", HttpStatus.OK);
288+
return ResponseEntity.ok("Webhook received");
290289
}
291290
}
292291
```

0 commit comments

Comments
 (0)