Skip to content

Commit

Permalink
Autowiring HmacValidator instead of creating a new instance everytime
Browse files Browse the repository at this point in the history
  • Loading branch information
pavan committed Nov 22, 2024
1 parent d1198c5 commit f3f9c73
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.adyen.checkout;

import com.adyen.util.HMACValidator;
import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class Config {
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,15 +26,17 @@ public class WebhookResource {
private final Logger log = LoggerFactory.getLogger(WebhookResource.class);

private final ApplicationProperty applicationProperty;
private final HMACValidator hmacValidator;

@Autowired
public WebhookResource(ApplicationProperty applicationProperty) {
public WebhookResource(ApplicationProperty applicationProperty, HMACValidator hmacValidator) {
this.applicationProperty = applicationProperty;

if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
//throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
}
this.hmacValidator = hmacValidator;
}

/**
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
var item = notificationRequestItem.get();

try {
if (getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
if (hmacValidator.validateHMAC(item, this.applicationProperty.getHmacKey())) {
log.info("""
Received webhook with event {} :\s
Merchant Reference: {}
Expand Down Expand Up @@ -102,9 +103,4 @@ void consumeEvent(NotificationRequestItem item) {
// producer.close();

}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
8 changes: 8 additions & 0 deletions checkout-example/src/main/java/com/adyen/checkout/Config.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.adyen.checkout;

import com.adyen.util.HMACValidator;
import org.springframework.context.annotation.Bean;

import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,15 +26,17 @@ public class WebhookResource {
private final Logger log = LoggerFactory.getLogger(WebhookResource.class);

private final ApplicationProperty applicationProperty;
private final HMACValidator hmacValidator;

@Autowired
public WebhookResource(ApplicationProperty applicationProperty) {
public WebhookResource(ApplicationProperty applicationProperty, HMACValidator hmacValidator) {
this.applicationProperty = applicationProperty;

if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
//throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
}
this.hmacValidator = hmacValidator;
}

/**
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
var item = notificationRequestItem.get();

try {
if (getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
if (hmacValidator.validateHMAC(item, this.applicationProperty.getHmacKey())) {
log.info("Received webhook with event {} : \n" +
"Merchant Reference: {}\n" +
"Alias : {}\n" +
Expand Down Expand Up @@ -101,9 +102,4 @@ void consumeEvent(NotificationRequestItem item) {
// producer.close();

}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
8 changes: 8 additions & 0 deletions giftcard-example/src/main/java/com/adyen/giftcard/Config.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.adyen.giftcard;

import com.adyen.util.HMACValidator;
import org.springframework.context.annotation.Bean;

import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,15 +26,17 @@ public class WebhookResource {
private final Logger log = LoggerFactory.getLogger(WebhookResource.class);

private ApplicationProperty applicationProperty;
private HMACValidator hmacValidator;

@Autowired
public WebhookResource(ApplicationProperty applicationProperty) {
public WebhookResource(ApplicationProperty applicationProperty, HMACValidator hmacValidator) {
this.applicationProperty = applicationProperty;

if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
}
this.hmacValidator = hmacValidator;
}

/**
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
var item = notificationRequestItem.get();

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
if (!hmacValidator.validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
Expand Down Expand Up @@ -132,9 +133,4 @@ void consumeEvent(NotificationRequestItem item) {
// producer.close();

}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
8 changes: 8 additions & 0 deletions giving-example/src/main/java/com/adyen/giving/Config.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.adyen.giving;

import com.adyen.util.HMACValidator;
import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class Config {
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,15 +26,17 @@ public class WebhookResource {
private final Logger log = LoggerFactory.getLogger(WebhookResource.class);

private final ApplicationProperty applicationProperty;
private final HMACValidator hmacValidator;

@Autowired
public WebhookResource(ApplicationProperty applicationProperty) {
public WebhookResource(ApplicationProperty applicationProperty, HMACValidator hmacValidator) {
this.applicationProperty = applicationProperty;

if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
//throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
}
this.hmacValidator = hmacValidator;
}

/**
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
var item = notificationRequestItem.get();

try {
if (getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
if (hmacValidator.validateHMAC(item, this.applicationProperty.getHmacKey())) {
log.info("""
Received webhook with event {} :\s
Merchant Reference: {}
Expand Down Expand Up @@ -102,9 +103,4 @@ void consumeEvent(NotificationRequestItem item) {
// producer.close();

}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.adyen.ipp;

import com.adyen.util.HMACValidator;
import org.springframework.context.annotation.Bean;

import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -23,6 +22,7 @@ public class WebhookController {
private final Logger log = LoggerFactory.getLogger(WebhookController.class);

private ApplicationProperty applicationProperty;
private HMACValidator hmacValidator;

@Autowired
public WebhookController(ApplicationProperty applicationProperty) {
Expand All @@ -31,6 +31,7 @@ public WebhookController(ApplicationProperty applicationProperty) {
if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
}
this.hmacValidator = hmacValidator;
}

/**
Expand All @@ -54,7 +55,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
var item = notificationRequestItem.get();

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
if (!hmacValidator.validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
Expand Down Expand Up @@ -96,9 +97,4 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
// Acknowledge event has been consumed
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
14 changes: 14 additions & 0 deletions paybylink-example/src/main/java/com/adyen/paybylink/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.adyen.paybylink;

import com.adyen.util.HMACValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -28,15 +27,17 @@ public class WebhookController {
private PaymentLinkService paymentLinkService;

private final ApplicationProperty applicationProperty;
private final HMACValidator hmacValidator;

@Autowired
public WebhookController(ApplicationProperty applicationProperty) {
public WebhookController(ApplicationProperty applicationProperty, HMACValidator hmacValidator) {
this.applicationProperty = applicationProperty;

if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
//throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
}
this.hmacValidator = hmacValidator;
}

/** Process incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
var item = notificationRequestItem.get();

try {
if (getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
if (hmacValidator.validateHMAC(item, this.applicationProperty.getHmacKey())) {
log.info("""
Received webhook with event {} :\s
Merchant Reference: {}
Expand Down Expand Up @@ -105,9 +106,4 @@ void consumeEvent(NotificationRequestItem item) {
}

}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
Loading

0 comments on commit f3f9c73

Please sign in to comment.