Java Requires Chilkat v11.5.0+
Java
Box: Verify a Captured V2 Webhook Signature
See more Box Examples
Verify raw payload bytes and captured headers using the configured primary and secondary signing keys. The example rejects stale/future timestamps and compares randomized HMACs of the signatures to avoid exposing a signature-prefix timing comparison. It does not host an HTTP listener. The receiver must persist/deduplicate the verified event and acknowledge promptly.
See the Box API guide for setup and the complete workflow.
Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false;
// Save the exact HTTP body bytes to this file, without JSON reserialization.
CkBinData rawBody = new CkBinData();
success = rawBody.LoadFile("qa_data/box_webhook_body.bin");
if (success != true) {
System.out.println(rawBody.lastErrorText());
return;
}
// Read these fields case-insensitively from the incoming request headers.
// For this offline example, enter the captured header values.
String timestamp = "BOX_DELIVERY_TIMESTAMP";
String primarySignature = "BOX_SIGNATURE_PRIMARY";
String secondarySignature = "BOX_SIGNATURE_SECONDARY";
String signatureVersion = "1";
String signatureAlgorithm = "HmacSHA256";
// Retrieve configured keys from protected application configuration.
String primaryKey = "BOX_PRIMARY_SIGNATURE_KEY";
String secondaryKey = "BOX_SECONDARY_SIGNATURE_KEY";
if ((signatureVersion != "1") || (signatureAlgorithm != "HmacSHA256")) {
System.out.println("Unsupported signature version or algorithm.");
return;
}
CkDateTime delivered = new CkDateTime();
success = delivered.SetFromTimestamp(timestamp);
if (success != true) {
System.out.println(delivered.lastErrorText());
return;
}
CkDateTime now = new CkDateTime();
success = now.SetFromCurrentSystemTime();
if (success != true) {
System.out.println(now.lastErrorText());
return;
}
int age = Integer.toString(now.GetAsUnixTime(false)) - Integer.toString(delivered.GetAsUnixTime(false));
// Box recommends rejecting deliveries older than ten minutes.
// Allow at most 60 seconds of future clock skew; keep the server clock synchronized.
if ((age > 600) || (age < -60)) {
System.out.println("Delivery timestamp is outside the accepted window.");
return;
}
CkBinData signedBytes = new CkBinData();
success = signedBytes.AppendBd(rawBody);
if (success != true) {
System.out.println(signedBytes.lastErrorText());
return;
}
success = signedBytes.AppendString(timestamp,"utf-8");
if (success != true) {
System.out.println(signedBytes.lastErrorText());
return;
}
CkCrypt2 mac = new CkCrypt2();
mac.put_MacAlgorithm("hmac");
mac.put_HashAlgorithm("sha256");
mac.put_EncodingMode("base64");
success = mac.SetMacKeyString(primaryKey);
if (success != true) {
System.out.println(mac.lastErrorText());
return;
}
String expectedPrimary = mac.macBdENC(signedBytes);
if (mac.get_LastMethodSuccess() != true) {
System.out.println(mac.lastErrorText());
return;
}
success = mac.SetMacKeyString(secondaryKey);
if (success != true) {
System.out.println(mac.lastErrorText());
return;
}
String expectedSecondary = mac.macBdENC(signedBytes);
if (mac.get_LastMethodSuccess() != true) {
System.out.println(mac.lastErrorText());
return;
}
// Blind both sides with a fresh secret random key before equality comparison.
// Never compare the original HMAC strings using a prefix-short-circuit comparison.
// A host-language receiver can instead use its standard constant-time byte comparison.
CkPrng random = new CkPrng();
String comparisonKey = random.genRandom(32,"base64");
if (random.get_LastMethodSuccess() != true) {
System.out.println(random.lastErrorText());
return;
}
success = mac.SetMacKeyEncoded(comparisonKey,"base64");
if (success != true) {
System.out.println(mac.lastErrorText());
return;
}
String pExpected = mac.macStringENC(expectedPrimary);
if (mac.get_LastMethodSuccess() != true) {
System.out.println(mac.lastErrorText());
return;
}
String pReceived = mac.macStringENC(primarySignature);
if (mac.get_LastMethodSuccess() != true) {
System.out.println(mac.lastErrorText());
return;
}
String sExpected = mac.macStringENC(expectedSecondary);
if (mac.get_LastMethodSuccess() != true) {
System.out.println(mac.lastErrorText());
return;
}
String sReceived = mac.macStringENC(secondarySignature);
if (mac.get_LastMethodSuccess() != true) {
System.out.println(mac.lastErrorText());
return;
}
boolean validPrimary = (primaryKey != "") && (primarySignature != "") && (pExpected == pReceived);
boolean validSecondary = (secondaryKey != "") && (secondarySignature != "") && (sExpected == sReceived);
if ((validPrimary == false) && (validSecondary == false)) {
System.out.println("Signature verification failed.");
return;
}
// Parse JSON only after signature verification.
CkJsonObject webhookEvent = new CkJsonObject();
success = webhookEvent.Load(rawBody.getString("utf-8"));
if (success != true) {
System.out.println(webhookEvent.lastErrorText());
return;
}
System.out.println("Verified event ID: " + webhookEvent.stringOf("id"));
System.out.println("Trigger: " + webhookEvent.stringOf("trigger"));
// Deduplicate using event.id, not BOX-DELIVERY-ID, which changes on retries.
// Verify the target belongs to your configured webhook before dispatching work.
}
}