Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook signature verification not implemented #44

Open
prj opened this issue May 26, 2023 · 1 comment
Open

Webhook signature verification not implemented #44

prj opened this issue May 26, 2023 · 1 comment

Comments

@prj
Copy link

prj commented May 26, 2023

Seems like the signature verification is not implemented for webhooks.
Only the json deserialization.

Would be useful if you did implement it, because everyone has to implement it themselves otherwise.

@prj
Copy link
Author

prj commented Jun 16, 2023

Here is my implementation, which is in production now, maybe useful for someone else in the future:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	log.info("Received webhook from kevin...");
	
	String payload = IOUtils.toString(req.getInputStream(), req.getCharacterEncoding());

	String kevinTimestamp = req.getHeader("X-Kevin-Timestamp");
	String kevinSignature = req.getHeader("X-Kevin-Signature");
	
	String forSignature = "POST" + vehicalProperties.getKevinWebhookUrl() + kevinTimestamp + payload;
	
	Mac sha256_HMAC = null;
	try {
		sha256_HMAC = Mac.getInstance("HmacSHA256");
		SecretKeySpec secret_key = new SecretKeySpec(vehicalProperties.getKevinEndpointSecret().getBytes("UTF-8"), "HmacSHA256");
		sha256_HMAC.init(secret_key);
	} catch (Exception e) {
		log.log(Level.SEVERE, "Error initializing HMAC algorithm.", e);
		resp.setStatus(400);
		return;
	}
	
	String computedSignature = Hex.encodeHexString(sha256_HMAC.doFinal(forSignature.getBytes("UTF-8")));
	
	if (!kevinSignature.equals(computedSignature)) {
		log.severe("Webhook signature verification failed.");
		resp.setStatus(400);
		return;
	}
	
	Parser parser = new Parser();
	PaymentWebhookPayload parsedHook = parser.parsePaymentWebhookRequest(payload);
	
	...

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant