-
Notifications
You must be signed in to change notification settings - Fork 8
[MSUE-129] - Updates in the lib #88
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
Merged
sbogolii-sift
merged 4 commits into
SiftScience:master
from
sreejithKExalture:combined_features
Dec 22, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.siftscience; | ||
|
|
||
| public class Constants { | ||
|
|
||
| public static final String API_VERSION = "v205"; | ||
| public static final String LIB_VERSION = "3.7.0"; | ||
| public static final String USER_AGENT_HEADER = String.format("SiftScience/%s sift-java/%s", API_VERSION, LIB_VERSION); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.siftscience.utils; | ||
|
|
||
| import org.apache.commons.codec.digest.HmacAlgorithms; | ||
| import org.apache.commons.codec.digest.HmacUtils; | ||
|
|
||
| public class WebhookValidator { | ||
| private static final String SHA1 = "sha1="; | ||
| private static final String SHA256 = "sha256="; | ||
|
|
||
| /** | ||
| * | ||
| * @param siftScienceSignature value of the 'X-Sift-Science-Signature' signature in the http header of the webhook request. | ||
| * @param requestBody body of the webhook request | ||
| * @param secretKey sift webhook secret key | ||
| * @return true, if the webhook request received is coming from Sift | ||
| */ | ||
|
|
||
| public static boolean isValidWebhook(String siftScienceSignature, String requestBody, String secretKey) { | ||
| String verificationSignature_Sha1 = SHA1 + new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secretKey.getBytes()).hmacHex(requestBody); | ||
| String verificationSignature_Sha256 = SHA256 + new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secretKey.getBytes()).hmacHex(requestBody); | ||
|
|
||
| return siftScienceSignature.equals(verificationSignature_Sha1) || siftScienceSignature.equals(verificationSignature_Sha256); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.siftscience; | ||
|
|
||
| import com.siftscience.model.ApplyDecisionFieldSet; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.mockwebserver.MockResponse; | ||
| import okhttp3.mockwebserver.MockWebServer; | ||
| import okhttp3.mockwebserver.RecordedRequest; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import static java.net.HttpURLConnection.HTTP_OK; | ||
|
|
||
| public class SiftRequestTest { | ||
|
|
||
| @Test | ||
| public void testUserAgentHeader() throws Exception { | ||
| MockWebServer server = new MockWebServer(); | ||
| MockResponse response = new MockResponse(); | ||
| response.setResponseCode(HTTP_OK); | ||
|
|
||
| server.enqueue(response); | ||
| server.start(); | ||
| String userId = "a_user_id"; | ||
|
|
||
| // Create a new client and link it to the mock server. | ||
| SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID", | ||
| new OkHttpClient.Builder() | ||
| .addInterceptor(OkHttpUtils.urlRewritingInterceptor(server)) | ||
| .build()); | ||
|
|
||
| // Build and execute the request against the mock server. | ||
| ApplyDecisionRequest request = client.buildRequest( | ||
| new ApplyDecisionFieldSet() | ||
| .setUserId(userId) | ||
| .setTime(System.currentTimeMillis())); | ||
|
|
||
| request.send(); | ||
|
|
||
| // Verify the request. | ||
| RecordedRequest recordedRequest = server.takeRequest(); | ||
| Assert.assertEquals("SiftScience/v205 sift-java/3.7.0", recordedRequest.getHeader("User-Agent")); | ||
| } | ||
|
|
||
| } |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/siftscience/utils/WebhookValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package com.siftscience.utils; | ||
|
|
||
| import org.apache.commons.codec.digest.HmacAlgorithms; | ||
| import org.apache.commons.codec.digest.HmacUtils; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import static com.siftscience.utils.WebhookValidator.isValidWebhook; | ||
|
|
||
| public class WebhookValidatorTest { | ||
|
|
||
| @Test | ||
| public void testWebhookValidation_sha1() { | ||
|
|
||
| final String secretKey = "1d708fe409f22591"; | ||
| final String requestBody = "{\n" + | ||
| " \"entity\": {\n" + | ||
| " \"type\": \"user\",\n" + | ||
| " \"id\": \"USER123\"\n" + | ||
| " },\n" + | ||
| " \"decision\": {\n" + | ||
| " \"id\": \"block_user_payment_abuse\"\n" + | ||
| " },\n" + | ||
| " \"time\": 1461963439151\n" + | ||
| "}"; | ||
| final String signature = "sha1=" + new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secretKey).hmacHex(requestBody); | ||
|
|
||
| Assert.assertTrue(isValidWebhook(signature, requestBody, secretKey)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWebhookValidation_sha256() { | ||
|
|
||
| final String secretKey = "1d708fe409f22591"; | ||
| final String requestBody = "{\n" + | ||
| " \"entity\": {\n" + | ||
| " \"type\": \"user\",\n" + | ||
| " \"id\": \"USER123\"\n" + | ||
| " },\n" + | ||
| " \"decision\": {\n" + | ||
| " \"id\": \"block_user_payment_abuse\"\n" + | ||
| " },\n" + | ||
| " \"time\": 1461963439151\n" + | ||
| "}"; | ||
| final String signature = "sha256=" + new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secretKey.getBytes()).hmacHex(requestBody); | ||
|
|
||
| Assert.assertTrue(isValidWebhook(signature, requestBody, secretKey)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWebhookValidationForInvalidSecretKey() { | ||
|
|
||
| final String secretKey = "1d708fe409f22591"; | ||
| final String requestBody = "{\n" + | ||
| " \"entity\": {\n" + | ||
| " \"type\": \"user\",\n" + | ||
| " \"id\": \"USER123\"\n" + | ||
| " },\n" + | ||
| " \"decision\": {\n" + | ||
| " \"id\": \"block_user_payment_abuse\"\n" + | ||
| " },\n" + | ||
| " \"time\": 1461963439151\n" + | ||
| "}"; | ||
| final String signature = "sha1=" + new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secretKey.getBytes()).hmacHex(requestBody); | ||
|
|
||
| Assert.assertFalse(isValidWebhook(signature, requestBody, "invalid key")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.