From f3f9c73e6178c8056ce2cc4cfb8a2c0cdd584f2c Mon Sep 17 00:00:00 2001 From: pavan Date: Fri, 22 Nov 2024 14:30:15 +0800 Subject: [PATCH] Autowiring HmacValidator instead of creating a new instance everytime --- .../src/main/java/com/adyen/checkout/Config.java | 9 ++++++++- .../com/adyen/checkout/api/WebhookResource.java | 12 ++++-------- .../src/main/java/com/adyen/checkout/Config.java | 8 ++++++++ .../com/adyen/checkout/api/WebhookResource.java | 12 ++++-------- .../src/main/java/com/adyen/giftcard/Config.java | 8 ++++++++ .../com/adyen/giftcard/api/WebhookResource.java | 12 ++++-------- .../src/main/java/com/adyen/giving/Config.java | 8 ++++++++ .../com/adyen/giving/api/WebhookResource.java | 12 ++++-------- .../src/main/java/com/adyen/ipp/Config.java | 8 ++++++++ .../java/com/adyen/ipp/api/WebhookController.java | 10 +++------- .../src/main/java/com/adyen/paybylink/Config.java | 14 ++++++++++++++ .../adyen/paybylink/api/WebhookController.java | 12 ++++-------- .../src/main/java/com/adyen/checkout/Config.java | 9 +++++++++ .../com/adyen/checkout/api/WebhookResource.java | 15 +++++---------- 14 files changed, 91 insertions(+), 58 deletions(-) create mode 100644 paybylink-example/src/main/java/com/adyen/paybylink/Config.java diff --git a/checkout-example-advanced/src/main/java/com/adyen/checkout/Config.java b/checkout-example-advanced/src/main/java/com/adyen/checkout/Config.java index 7c373ac6..9e2bd205 100644 --- a/checkout-example-advanced/src/main/java/com/adyen/checkout/Config.java +++ b/checkout-example-advanced/src/main/java/com/adyen/checkout/Config.java @@ -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(); + } } diff --git a/checkout-example-advanced/src/main/java/com/adyen/checkout/api/WebhookResource.java b/checkout-example-advanced/src/main/java/com/adyen/checkout/api/WebhookResource.java index cf2a6578..e82ef4e2 100644 --- a/checkout-example-advanced/src/main/java/com/adyen/checkout/api/WebhookResource.java +++ b/checkout-example-advanced/src/main/java/com/adyen/checkout/api/WebhookResource.java @@ -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; @@ -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; } /** @@ -59,7 +60,7 @@ public ResponseEntity 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: {} @@ -102,9 +103,4 @@ void consumeEvent(NotificationRequestItem item) { // producer.close(); } - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } } diff --git a/checkout-example/src/main/java/com/adyen/checkout/Config.java b/checkout-example/src/main/java/com/adyen/checkout/Config.java index cdb5b03b..cd5f842e 100644 --- a/checkout-example/src/main/java/com/adyen/checkout/Config.java +++ b/checkout-example/src/main/java/com/adyen/checkout/Config.java @@ -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(); + } } diff --git a/checkout-example/src/main/java/com/adyen/checkout/api/WebhookResource.java b/checkout-example/src/main/java/com/adyen/checkout/api/WebhookResource.java index 5b18046f..b8ead4cd 100644 --- a/checkout-example/src/main/java/com/adyen/checkout/api/WebhookResource.java +++ b/checkout-example/src/main/java/com/adyen/checkout/api/WebhookResource.java @@ -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; @@ -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; } /** @@ -59,7 +60,7 @@ public ResponseEntity 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" + @@ -101,9 +102,4 @@ void consumeEvent(NotificationRequestItem item) { // producer.close(); } - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } } diff --git a/giftcard-example/src/main/java/com/adyen/giftcard/Config.java b/giftcard-example/src/main/java/com/adyen/giftcard/Config.java index ee952847..f516d729 100644 --- a/giftcard-example/src/main/java/com/adyen/giftcard/Config.java +++ b/giftcard-example/src/main/java/com/adyen/giftcard/Config.java @@ -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(); + } } diff --git a/giftcard-example/src/main/java/com/adyen/giftcard/api/WebhookResource.java b/giftcard-example/src/main/java/com/adyen/giftcard/api/WebhookResource.java index 3abedfaf..2aee5549 100644 --- a/giftcard-example/src/main/java/com/adyen/giftcard/api/WebhookResource.java +++ b/giftcard-example/src/main/java/com/adyen/giftcard/api/WebhookResource.java @@ -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; @@ -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; } /** @@ -59,7 +60,7 @@ public ResponseEntity 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"); @@ -132,9 +133,4 @@ void consumeEvent(NotificationRequestItem item) { // producer.close(); } - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } } diff --git a/giving-example/src/main/java/com/adyen/giving/Config.java b/giving-example/src/main/java/com/adyen/giving/Config.java index caaea05f..4a6e1290 100644 --- a/giving-example/src/main/java/com/adyen/giving/Config.java +++ b/giving-example/src/main/java/com/adyen/giving/Config.java @@ -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(); + } } diff --git a/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java b/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java index 5ceb4b76..20c773ad 100644 --- a/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java +++ b/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java @@ -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; @@ -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; } /** @@ -59,7 +60,7 @@ public ResponseEntity 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: {} @@ -102,9 +103,4 @@ void consumeEvent(NotificationRequestItem item) { // producer.close(); } - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } } diff --git a/in-person-payments-example/src/main/java/com/adyen/ipp/Config.java b/in-person-payments-example/src/main/java/com/adyen/ipp/Config.java index f7d68ba8..82016d87 100644 --- a/in-person-payments-example/src/main/java/com/adyen/ipp/Config.java +++ b/in-person-payments-example/src/main/java/com/adyen/ipp/Config.java @@ -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(); + } } diff --git a/in-person-payments-example/src/main/java/com/adyen/ipp/api/WebhookController.java b/in-person-payments-example/src/main/java/com/adyen/ipp/api/WebhookController.java index 32b21003..a184dc26 100644 --- a/in-person-payments-example/src/main/java/com/adyen/ipp/api/WebhookController.java +++ b/in-person-payments-example/src/main/java/com/adyen/ipp/api/WebhookController.java @@ -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.*; @@ -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) { @@ -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; } /** @@ -54,7 +55,7 @@ public ResponseEntity 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"); @@ -96,9 +97,4 @@ public ResponseEntity webhooks(@RequestBody String json) throws IOExcept // Acknowledge event has been consumed return ResponseEntity.status(HttpStatus.ACCEPTED).build(); } - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } } diff --git a/paybylink-example/src/main/java/com/adyen/paybylink/Config.java b/paybylink-example/src/main/java/com/adyen/paybylink/Config.java new file mode 100644 index 00000000..1d9a0361 --- /dev/null +++ b/paybylink-example/src/main/java/com/adyen/paybylink/Config.java @@ -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(); + } +} diff --git a/paybylink-example/src/main/java/com/adyen/paybylink/api/WebhookController.java b/paybylink-example/src/main/java/com/adyen/paybylink/api/WebhookController.java index 9ea6faa8..f0e2830b 100644 --- a/paybylink-example/src/main/java/com/adyen/paybylink/api/WebhookController.java +++ b/paybylink-example/src/main/java/com/adyen/paybylink/api/WebhookController.java @@ -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; @@ -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, @@ -59,7 +60,7 @@ public ResponseEntity 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: {} @@ -105,9 +106,4 @@ void consumeEvent(NotificationRequestItem item) { } } - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } } diff --git a/subscription-example/src/main/java/com/adyen/checkout/Config.java b/subscription-example/src/main/java/com/adyen/checkout/Config.java index cdb5b03b..75315b0a 100644 --- a/subscription-example/src/main/java/com/adyen/checkout/Config.java +++ b/subscription-example/src/main/java/com/adyen/checkout/Config.java @@ -1,12 +1,21 @@ 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(); + } } diff --git a/subscription-example/src/main/java/com/adyen/checkout/api/WebhookResource.java b/subscription-example/src/main/java/com/adyen/checkout/api/WebhookResource.java index b9becdca..0abe0b4c 100644 --- a/subscription-example/src/main/java/com/adyen/checkout/api/WebhookResource.java +++ b/subscription-example/src/main/java/com/adyen/checkout/api/WebhookResource.java @@ -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.*; @@ -24,16 +23,18 @@ public class WebhookResource { private final Logger log = LoggerFactory.getLogger(WebhookResource.class); - private ApplicationProperty applicationProperty; + 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; } /** @@ -57,7 +58,7 @@ public ResponseEntity 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"); @@ -97,10 +98,4 @@ public ResponseEntity webhooks(@RequestBody String json) throws IOExcept // Acknowledge event has been consumed return ResponseEntity.status(HttpStatus.ACCEPTED).build(); } - - - @Bean - public HMACValidator getHmacValidator() { - return new HMACValidator(); - } }