|
| 1 | +package com.cosium.standard_webhooks_consumer; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | + |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.security.InvalidKeyException; |
| 7 | +import java.security.NoSuchAlgorithmException; |
| 8 | +import java.util.Base64; |
| 9 | +import java.util.Optional; |
| 10 | +import javax.crypto.Mac; |
| 11 | +import javax.crypto.spec.SecretKeySpec; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author Réda Housni Alaoui |
| 15 | + */ |
| 16 | +class SecretKey implements VerificationKey { |
| 17 | + |
| 18 | + private static final String SERIALIZATION_PREFIX = "whsec_"; |
| 19 | + private static final SignatureSchemeId SCHEME_ID = new SignatureSchemeId("v1"); |
| 20 | + private static final String ALGORITHM = "HmacSHA256"; |
| 21 | + |
| 22 | + private final byte[] value; |
| 23 | + |
| 24 | + private SecretKey(byte[] value) { |
| 25 | + this.value = requireNonNull(value); |
| 26 | + } |
| 27 | + |
| 28 | + public static Optional<SecretKey> parseKey(String serializedVerificationKey) { |
| 29 | + if (!serializedVerificationKey.startsWith(SERIALIZATION_PREFIX)) { |
| 30 | + return Optional.empty(); |
| 31 | + } |
| 32 | + return Optional.of( |
| 33 | + new SecretKey( |
| 34 | + Base64.getDecoder() |
| 35 | + .decode(serializedVerificationKey.substring(SERIALIZATION_PREFIX.length())))); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean supports(SignatureSchemeId signatureSchemeId) { |
| 40 | + return SCHEME_ID.equals(signatureSchemeId); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void verify(String messageId, long timestamp, String payload, Signature signatureToVerify) |
| 45 | + throws WebhookSignatureVerificationException { |
| 46 | + |
| 47 | + try { |
| 48 | + doVerify(messageId, timestamp, payload, signatureToVerify); |
| 49 | + } catch (RuntimeException e) { |
| 50 | + throw new WebhookSignatureVerificationException(e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void doVerify( |
| 55 | + String messageId, long timestamp, String payload, Signature signatureToVerify) |
| 56 | + throws WebhookSignatureVerificationException { |
| 57 | + String expectedBase64EncodedSignatureContent; |
| 58 | + try { |
| 59 | + expectedBase64EncodedSignatureContent = sign(messageId, timestamp, payload); |
| 60 | + } catch (NoSuchAlgorithmException | InvalidKeyException e) { |
| 61 | + throw new WebhookSignatureVerificationException(e); |
| 62 | + } |
| 63 | + |
| 64 | + if (expectedBase64EncodedSignatureContent.equals(signatureToVerify.base64EncodedValue())) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + throw new WebhookSignatureVerificationException("%s is not valid".formatted(signatureToVerify)); |
| 69 | + } |
| 70 | + |
| 71 | + private String sign(String messageId, long timestamp, String payload) |
| 72 | + throws NoSuchAlgorithmException, InvalidKeyException { |
| 73 | + String contentToSign = "%s.%s.%s".formatted(messageId, timestamp, payload); |
| 74 | + Mac sha512Hmac = Mac.getInstance(ALGORITHM); |
| 75 | + SecretKeySpec keySpec = new SecretKeySpec(value, ALGORITHM); |
| 76 | + sha512Hmac.init(keySpec); |
| 77 | + byte[] macData = sha512Hmac.doFinal(contentToSign.getBytes(StandardCharsets.UTF_8)); |
| 78 | + return Base64.getEncoder().encodeToString(macData); |
| 79 | + } |
| 80 | +} |
0 commit comments