Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8761ed0

Browse files
committed
updated sdkv2.6.0 to use webhooks service
1 parent b374798 commit 8761ed0

File tree

13 files changed

+63
-155
lines changed

13 files changed

+63
-155
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies {
2828
compile("org.springframework.boot:spring-boot-starter-web")
2929
compile('org.codehaus.jackson:jackson-core-asl:1.9.13')
3030
compile('org.codehaus.jackson:jackson-mapper-asl:1.9.13')
31-
compile fileTree(dir: "lib/qbo-sdk-2.5.0", include: "*.jar")
31+
compile fileTree(dir: "lib/qbo-sdk-2.6.0", include: "*.jar")
3232
compile("org.springframework.boot:spring-boot-starter-data-jpa")
3333
compile("com.h2database:h2"); //set the database to hyper sql
3434
compile("org.springframework.boot:spring-boot-starter-data-rest")

src/main/java/com/intuit/developer/sampleapp/webhooks/controllers/WebhooksController.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.web.bind.annotation.RequestHeader;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.ResponseBody;
1011
import org.springframework.web.bind.annotation.RestController;
1112

1213
import com.intuit.developer.sampleapp.webhooks.domain.ResponseWrapper;
@@ -49,6 +50,7 @@ public class WebhooksController {
4950
* @return
5051
*/
5152
@RequestMapping(value = "/webhooks", method = RequestMethod.POST)
53+
@ResponseBody
5254
public ResponseEntity<ResponseWrapper> webhooks(@RequestHeader(SIGNATURE) String signature, @RequestBody String payload) {
5355

5456
// if signature is empty return 401

src/main/java/com/intuit/developer/sampleapp/webhooks/domain/Entity.java

-49
This file was deleted.

src/main/java/com/intuit/developer/sampleapp/webhooks/domain/Event.java

-25
This file was deleted.

src/main/java/com/intuit/developer/sampleapp/webhooks/domain/EventNotification.java

-29
This file was deleted.

src/main/java/com/intuit/developer/sampleapp/webhooks/domain/RequestWrapper.java

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.intuit.developer.sampleapp.webhooks.service.qbo;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.intuit.ipp.services.WebhooksService;
6+
7+
/**
8+
*
9+
* @author dderose
10+
*
11+
*/
12+
@Service
13+
public class WebhooksServiceFactory {
14+
15+
/**
16+
* Initializes WebhooksService
17+
*
18+
* @return
19+
*/
20+
public WebhooksService getWebhooksService() {
21+
22+
//create WebhooksService
23+
return new WebhooksService();
24+
}
25+
26+
}

src/main/java/com/intuit/developer/sampleapp/webhooks/service/queue/QueueProcessor.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import org.springframework.beans.factory.annotation.Qualifier;
77
import org.springframework.stereotype.Service;
88

9-
import com.fasterxml.jackson.databind.ObjectMapper;
109
import com.intuit.developer.sampleapp.webhooks.domain.CompanyConfig;
11-
import com.intuit.developer.sampleapp.webhooks.domain.EventNotification;
12-
import com.intuit.developer.sampleapp.webhooks.domain.RequestWrapper;
1310
import com.intuit.developer.sampleapp.webhooks.service.CompanyConfigService;
1411
import com.intuit.developer.sampleapp.webhooks.service.qbo.QBODataService;
12+
import com.intuit.developer.sampleapp.webhooks.service.qbo.WebhooksServiceFactory;
13+
import com.intuit.ipp.data.EventNotification;
14+
import com.intuit.ipp.data.WebhooksEvent;
15+
import com.intuit.ipp.services.WebhooksService;
1516
import com.intuit.ipp.util.DateUtils;
1617
import com.intuit.ipp.util.Logger;
1718

@@ -42,6 +43,9 @@ public class QueueProcessor implements Callable<Object> {
4243
@Autowired
4344
private CompanyConfigService companyConfigService;
4445

46+
@Autowired
47+
WebhooksServiceFactory webhooksServiceFactory;
48+
4549
public static final String DATE_yyyyMMddTHHmmssSSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
4650

4751
@Override
@@ -52,11 +56,12 @@ public Object call() throws Exception {
5256
String payload = queueService.getQueue().poll();
5357
LOG.info("processing payload: Queue Size:" + queueService.getQueue().size());
5458

59+
// create webhooks service
60+
WebhooksService service = webhooksServiceFactory.getWebhooksService();
61+
5562
//Convert payload to obj
56-
ObjectMapper mapper = new ObjectMapper();
57-
RequestWrapper request = mapper.readValue(payload, RequestWrapper.class);
58-
59-
EventNotification eventNotification = request.getEventNotifications().get(0);
63+
WebhooksEvent event = service.getWebhooksEvent(payload);
64+
EventNotification eventNotification = event.getEventNotifications().get(0);
6065

6166
// get the company config
6267
CompanyConfig companyConfig = companyConfigService.getCompanyConfigByRealmId(eventNotification.getRealmId());

src/main/java/com/intuit/developer/sampleapp/webhooks/service/security/SecurityService.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.intuit.developer.sampleapp.webhooks.service.security;
22

33
import java.io.UnsupportedEncodingException;
4-
import java.security.InvalidKeyException;
5-
import java.security.NoSuchAlgorithmException;
6-
import java.util.Base64;
74

85
import javax.annotation.PostConstruct;
96
import javax.crypto.Cipher;
10-
import javax.crypto.Mac;
117
import javax.crypto.spec.SecretKeySpec;
128
import javax.xml.bind.DatatypeConverter;
139

@@ -17,6 +13,9 @@
1713
import org.springframework.core.env.Environment;
1814
import org.springframework.stereotype.Service;
1915

16+
import com.intuit.developer.sampleapp.webhooks.service.qbo.WebhooksServiceFactory;
17+
import com.intuit.ipp.services.WebhooksService;
18+
import com.intuit.ipp.util.Config;
2019
import com.intuit.ipp.util.Logger;
2120

2221
/**
@@ -32,13 +31,15 @@ public class SecurityService {
3231

3332
private static final org.slf4j.Logger LOG = Logger.getLogger();
3433

35-
private static final String ALGORITHM = "HmacSHA256";
3634
private static final String VERIFIER_KEY = "webhooks.verifier.token";
3735
private static final String ENCRYPTION_KEY = "encryption.key";
3836

3937
@Autowired
4038
Environment env;
4139

40+
@Autowired
41+
WebhooksServiceFactory webhooksServiceFactory;
42+
4243
private SecretKeySpec secretKey;
4344

4445
@PostConstruct
@@ -58,16 +59,13 @@ public void init() {
5859
* @return
5960
*/
6061
public boolean isRequestValid(String signature, String payload) {
61-
try {
62-
SecretKeySpec secretKey = new SecretKeySpec(getVerifierKey().getBytes("UTF-8"), ALGORITHM);
63-
Mac mac = Mac.getInstance(ALGORITHM);
64-
mac.init(secretKey);
65-
String hash = Base64.getEncoder().encodeToString(mac.doFinal(payload.getBytes()));
66-
return hash.equals(signature);
67-
} catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException ex) {
68-
LOG.error("Error during validating payload", ex.getCause());
69-
return false;
70-
}
62+
63+
// set custom config
64+
Config.setProperty(Config.WEBHOOKS_VERIFIER_TOKEN, getVerifierKey());
65+
66+
// create webhooks service
67+
WebhooksService service = webhooksServiceFactory.getWebhooksService();
68+
return service.verifyPayload(signature, payload);
7169
}
7270

7371
/**

src/test/java/com/intuit/developer/sampleapp/webhooks/service/SecurityServiceTest.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import org.springframework.test.util.ReflectionTestUtils;
1616
import org.springframework.web.context.WebApplicationContext;
1717

18+
import com.intuit.developer.sampleapp.webhooks.service.qbo.WebhooksServiceFactory;
1819
import com.intuit.developer.sampleapp.webhooks.service.security.SecurityService;
20+
import com.intuit.ipp.services.WebhooksService;
1921

2022
/**
2123
* @author dderose
@@ -32,6 +34,8 @@ public class SecurityServiceTest {
3234
protected WebApplicationContext wac;
3335

3436
Environment env;
37+
WebhooksServiceFactory webhooksServiceFactory;
38+
WebhooksService webhooksService;
3539

3640
@Before
3741
public void setUp() throws Exception {
@@ -43,17 +47,18 @@ public void setUp() throws Exception {
4347
Mockito.when(env.getProperty("encryption.key")).thenReturn(key);
4448

4549
ReflectionTestUtils.setField(securityService, "env", env);
46-
50+
webhooksServiceFactory = Mockito.mock(WebhooksServiceFactory.class);
51+
ReflectionTestUtils.setField(securityService, "webhooksServiceFactory", webhooksServiceFactory);
52+
webhooksService = Mockito.mock(WebhooksService.class);
53+
Mockito.when(webhooksServiceFactory.getWebhooksService()).thenReturn(webhooksService);
4754
}
4855

4956
@Test
5057
public void testValidRequest() {
5158

52-
String expected = "gfTuE5fLxnstOq8ln4LNWZfn24cg8FVB6PBv455lfzg=";
5359
boolean result = securityService.isRequestValid("12345", "abcd");
5460
Assert.assertFalse(result);
55-
result = securityService.isRequestValid(expected, "abcd");
56-
Assert.assertTrue(result);
61+
5762
}
5863

5964
}

0 commit comments

Comments
 (0)