|
| 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.KeyFactory; |
| 8 | +import java.security.NoSuchAlgorithmException; |
| 9 | +import java.security.SignatureException; |
| 10 | +import java.security.spec.InvalidKeySpecException; |
| 11 | +import java.security.spec.X509EncodedKeySpec; |
| 12 | +import java.util.Base64; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Réda Housni Alaoui |
| 17 | + */ |
| 18 | +class PublicKey implements VerificationKey { |
| 19 | + |
| 20 | + private static final String SERIALIZATION_PREFIX = "whpk_"; |
| 21 | + private static final SignatureSchemeId SCHEME_ID = new SignatureSchemeId("v1a"); |
| 22 | + private static final String ALGORITHM = "Ed25519"; |
| 23 | + |
| 24 | + private final byte[] value; |
| 25 | + |
| 26 | + private PublicKey(byte[] value) { |
| 27 | + this.value = requireNonNull(value); |
| 28 | + } |
| 29 | + |
| 30 | + public static Optional<PublicKey> parseKey(String serializedVerificationKey) { |
| 31 | + if (!serializedVerificationKey.startsWith(SERIALIZATION_PREFIX)) { |
| 32 | + return Optional.empty(); |
| 33 | + } |
| 34 | + return Optional.of( |
| 35 | + new PublicKey( |
| 36 | + Base64.getDecoder() |
| 37 | + .decode(serializedVerificationKey.substring(SERIALIZATION_PREFIX.length())))); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean supports(SignatureSchemeId signatureSchemeId) { |
| 42 | + return SCHEME_ID.equals(signatureSchemeId); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void verify(String messageId, long timestamp, String payload, Signature signatureToVerify) |
| 47 | + throws WebhookSignatureVerificationException { |
| 48 | + try { |
| 49 | + doVerify(messageId, timestamp, payload, signatureToVerify); |
| 50 | + } catch (NoSuchAlgorithmException |
| 51 | + | InvalidKeyException |
| 52 | + | SignatureException |
| 53 | + | InvalidKeySpecException e) { |
| 54 | + throw new WebhookSignatureVerificationException(e); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void doVerify( |
| 59 | + String messageId, long timestamp, String payload, Signature signatureToVerify) |
| 60 | + throws NoSuchAlgorithmException, |
| 61 | + InvalidKeyException, |
| 62 | + SignatureException, |
| 63 | + InvalidKeySpecException, |
| 64 | + WebhookSignatureVerificationException { |
| 65 | + |
| 66 | + java.security.Signature signature = java.security.Signature.getInstance(ALGORITHM); |
| 67 | + |
| 68 | + java.security.PublicKey publicKey = |
| 69 | + KeyFactory.getInstance(ALGORITHM) |
| 70 | + .generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(value), ALGORITHM)); |
| 71 | + |
| 72 | + String signedContent = "%s.%s.%s".formatted(messageId, timestamp, payload); |
| 73 | + signature.initVerify(publicKey); |
| 74 | + signature.update(signedContent.getBytes(StandardCharsets.UTF_8)); |
| 75 | + |
| 76 | + if (signature.verify(signatureToVerify.decode())) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + throw new WebhookSignatureVerificationException("%s is not valid".formatted(signatureToVerify)); |
| 81 | + } |
| 82 | +} |
0 commit comments