Class WebhookSignatures
Mailtrap signs every outbound webhook by computing
HMAC-SHA256(signing_secret, raw_request_body) and sending the
lowercase hex digest in the Mailtrap-Signature HTTP header. To
authenticate a webhook on the receiver side, compute the same digest using
the signing_secret returned when the webhook was created and compare
it to the value of the header in constant time.
The comparison is performed with MessageDigest.isEqual(byte[], byte[])
to avoid timing side-channels.
The method never throws on inputs that could plausibly arrive over the
wire (empty strings, wrong-length signatures, non-hex characters, missing
secret) — it simply returns false. This makes it safe to call
directly from a request handler without wrapping in try/catch.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHex-encoded HMAC-SHA256 signature length (SHA-256 produces 32 bytes / 64 hex chars). -
Method Summary
-
Field Details
-
SIGNATURE_HEX_LENGTH
public static final int SIGNATURE_HEX_LENGTHHex-encoded HMAC-SHA256 signature length (SHA-256 produces 32 bytes / 64 hex chars).- See Also:
-
-
Method Details
-
verify
Verifies the HMAC-SHA256 signature of a Mailtrap webhook payload.- Parameters:
payload- the raw request body, exactly as received. Do not parse and re-serialize the JSON — re-encoding may reorder keys or alter whitespace and invalidate the signature. With Spring use@RequestBody byte[]or read the body directly fromHttpServletRequest.getInputStream()on the webhook route so the body is preserved verbatim.signature- the value of theMailtrap-SignatureHTTP header (lowercase hex string).signingSecret- the webhook'ssigning_secret, returned by the Webhooks API on webhook creation.- Returns:
trueif the signature is valid for the given payload and secret,falseotherwise (including anynull/empty input, wrong-length or non-hex signatures).
-