Skip to content

Commit 0e0ab8b

Browse files
authored
Merge pull request #36 from securenative/dev
Dev
2 parents 0bfbcbe + f49bd1e commit 0e0ab8b

File tree

7 files changed

+52
-26
lines changed

7 files changed

+52
-26
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ SecureNative can automatically load your config from *securenative.properties* f
5757

5858
```java
5959
// Options 1: Use default config file path
60-
SecureNative secureNative = SecureNative.init();
60+
SecureNative securenative = SecureNative.init();
6161

6262
// Options 2: Use specific config file path
6363
Path path = Paths.get("/path/to/securenative.properties");
64-
SecureNative secureNative = SecureNative.init(path);
64+
SecureNative securenative = SecureNative.init(path);
6565
```
6666
### Option 2: Initialize via API Key
6767

6868
```java
69-
SecureNative secureNative = SecureNative.init("YOUR_API_KEY");
69+
SecureNative securenative = SecureNative.init("YOUR_API_KEY");
7070
```
7171

7272
### Option 3: Initialize via ConfigurationBuilder
7373
```java
74-
SecureNative secureNative = SecureNative.init(SecureNative.configBuilder()
74+
SecureNative securenative = SecureNative.init(SecureNative.configBuilder()
7575
.withApiKey("API_KEY")
7676
.withMaxEvents(10)
7777
.withLogLevel("error")
@@ -81,7 +81,7 @@ SecureNative secureNative = SecureNative.init(SecureNative.configBuilder()
8181
## Getting SecureNative instance
8282
Once initialized, sdk will create a singleton instance which you can get:
8383
```java
84-
SecureNative secureNative = SecureNative.getInstance();
84+
SecureNative securenative = SecureNative.getInstance();
8585
```
8686

8787
## Tracking events
@@ -90,7 +90,7 @@ Once the SDK has been initialized, tracking requests sent through the SDK
9090
instance. Make sure you build event with the EventBuilder:
9191

9292
```java
93-
SecureNative secureNative = SecureNative.getInstance();
93+
SecureNative securenative = SecureNative.getInstance();
9494

9595
SecureNativeContext context = SecureNative.contextBuilder()
9696
.withIp("127.0.0.1")
@@ -136,7 +136,7 @@ public void track(HttpServletRequest request, HttpServletResponse response) {
136136
.timestamp(new Date())
137137
.build();
138138

139-
secureNative.track(eventOptions);
139+
securenative.track(eventOptions);
140140
}
141141
```
142142

@@ -163,7 +163,7 @@ public void verify(HttpServletRequest request, HttpServletResponse response) {
163163
.timestamp(new Date())
164164
.build();
165165

166-
VerifyResult verifyResult = secureNative.verify(eventOptions);
166+
VerifyResult verifyResult = securenative.verify(eventOptions);
167167
verifyResult.getRiskLevel(); // Low, Medium, High
168168
verifyResult.score(); // Risk score: 0 -1 (0 - Very Low, 1 - Very High)
169169
verifyResult.getTriggers(); // ["TOR", "New IP", "New City"]
@@ -177,9 +177,9 @@ Apply our filter to verify the request is from us, example in spring:
177177
```java
178178
@RequestMapping("/webhook")
179179
public void webhookEndpoint(HttpServletRequest request, HttpServletResponse response) {
180-
SecureNative secureNative = SecureNative.getInstance();
180+
SecureNative securenative = SecureNative.getInstance();
181181

182182
// Checks if request is verified
183-
Boolean isVerified = secureNative.verifyRequestPayload(request);
183+
Boolean isVerified = securenative.verifyRequestPayload(request);
184184
}
185185
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>com.securenative.java</groupId>
77
<artifactId>securenative-java</artifactId>
88
<packaging>jar</packaging>
9-
<version>0.5.4</version>
9+
<version>0.5.5</version>
1010
<url>https://github.com/securenative/securenative-java</url>
1111

1212
<name>${project.groupId}:${project.artifactId}:${project.version}</name>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.securenative;
22

3+
import com.securenative.exceptions.SecureNativeInvalidOptionsException;
34
import com.securenative.models.EventOptions;
45
import com.securenative.models.VerifyResult;
56

67
public interface ApiManager {
7-
void track(EventOptions eventOptions);
8+
void track(EventOptions eventOptions) throws SecureNativeInvalidOptionsException;
89

9-
VerifyResult verify(EventOptions eventOptions);
10+
VerifyResult verify(EventOptions eventOptions) throws SecureNativeInvalidOptionsException;
1011
}

src/main/java/com/securenative/ApiManagerImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.securenative.enums.ApiRoute;
55
import com.securenative.enums.FailoverStrategy;
66
import com.securenative.enums.RiskLevel;
7+
import com.securenative.exceptions.SecureNativeInvalidOptionsException;
78
import com.securenative.exceptions.SecureNativeSDKException;
89
import com.securenative.models.Event;
910
import com.securenative.models.EventOptions;
@@ -21,14 +22,14 @@ public ApiManagerImpl(EventManager eventManager, SecureNativeOptions options) th
2122
}
2223

2324
@Override
24-
public void track(EventOptions eventOptions) {
25+
public void track(EventOptions eventOptions) throws SecureNativeInvalidOptionsException {
2526
logger.info("Track event call");
2627
Event event = new SDKEvent(eventOptions, this.options);
2728
this.eventManager.sendAsync(event, ApiRoute.TRACK.getApiRoute(), true);
2829
}
2930

3031
@Override
31-
public VerifyResult verify(EventOptions eventOptions) {
32+
public VerifyResult verify(EventOptions eventOptions) throws SecureNativeInvalidOptionsException {
3233
logger.info("Verify event call");
3334
Event event = new SDKEvent(eventOptions, this.options);
3435
try {

src/main/java/com/securenative/SecureNative.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.securenative.config.SecureNativeOptions;
66
import com.securenative.context.SecureNativeContextBuilder;
77
import com.securenative.exceptions.SecureNativeConfigException;
8+
import com.securenative.exceptions.SecureNativeInvalidOptionsException;
89
import com.securenative.exceptions.SecureNativeSDKException;
910
import com.securenative.exceptions.SecureNativeSDKIllegalStateException;
1011
import com.securenative.http.SecureNativeHTTPClient;
@@ -95,12 +96,12 @@ public boolean verifyRequestPayload(HttpServletRequest request) throws IOExcepti
9596
}
9697

9798
@Override
98-
public void track(EventOptions eventOptions) {
99+
public void track(EventOptions eventOptions) throws SecureNativeInvalidOptionsException {
99100
this.apiManager.track(eventOptions);
100101
}
101102

102103
@Override
103-
public VerifyResult verify(EventOptions eventOptions) {
104+
public VerifyResult verify(EventOptions eventOptions) throws SecureNativeInvalidOptionsException {
104105
return this.apiManager.verify(eventOptions);
105106
}
106107
}

src/main/java/com/securenative/models/SDKEvent.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.securenative.config.SecureNativeOptions;
77
import com.securenative.context.SecureNativeContext;
88
import com.securenative.context.SecureNativeContextBuilder;
9+
import com.securenative.exceptions.SecureNativeInvalidOptionsException;
910
import com.securenative.utils.DateUtils;
1011
import com.securenative.utils.EncryptionUtils;
1112

@@ -21,7 +22,15 @@ public class SDKEvent implements Event {
2122
public Map<Object, Object> properties;
2223
public static final Logger logger = Logger.getLogger(SecureNative.class);
2324

24-
public SDKEvent(EventOptions event, SecureNativeOptions options) {
25+
public SDKEvent(EventOptions event, SecureNativeOptions options) throws SecureNativeInvalidOptionsException {
26+
if (event.getUserId() == null || event.getUserId().length() <= 0 || event.getUserId().equals("")) {
27+
throw new SecureNativeInvalidOptionsException("Invalid event structure; User Id is missing");
28+
}
29+
30+
if (event.getEvent() == null || event.getEvent().length() <= 0 || event.getEvent().equals("")) {
31+
throw new SecureNativeInvalidOptionsException("Invalid event structure; Event Type is missing");
32+
}
33+
2534
SecureNativeContext context = event.getContext() != null ? event.getContext() : SecureNativeContextBuilder.defaultContextBuilder().build();
2635

2736
ClientToken clientToken = decryptToken(context.getClientToken(), options.getApiKey());

src/test/java/com/securenative/ApiManagerImplTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void ShouldCallTrackEventTest() throws SecureNativeSDKException, Interrup
8080
JSONAssert.assertEquals(expected, body, false);
8181
assertThat(new JSONObject(body).has("rid")).isTrue();
8282
assertThat(new JSONObject(body).has("timestamp")).isTrue();
83+
} catch (SecureNativeInvalidOptionsException ignored) {
8384
} finally {
8485
eventManager.stopEventsPersist();
8586
}
@@ -114,8 +115,6 @@ public void ShouldThrowWhenSendingMoreThan10CustomPropertiesToTrackEventTest() t
114115
}
115116
}
116117

117-
;
118-
119118

120119
@Test
121120
@Timeout(value = 1000, unit = TimeUnit.MILLISECONDS)
@@ -131,7 +130,10 @@ public void ShouldNotCallTrackEventWhenAutomaticPersistenceDisabledTest() throws
131130
ApiManager apiManager = new ApiManagerImpl(eventManager, options);
132131

133132
// track async event
134-
apiManager.track(eventOptions);
133+
try {
134+
apiManager.track(eventOptions);
135+
} catch (SecureNativeInvalidOptionsException ignored) {
136+
}
135137

136138
// ensure event to be sent
137139
RecordedRequest lastRequest = server.takeRequest(10 * options.getInterval(), TimeUnit.MILLISECONDS);
@@ -156,7 +158,10 @@ public void ShouldNotRetryUnauthorizedTrackEventCallTest() throws SecureNativeSD
156158
ApiManager apiManager = new ApiManagerImpl(eventManager, options);
157159

158160
// track async event
159-
apiManager.track(eventOptions);
161+
try {
162+
apiManager.track(eventOptions);
163+
} catch (SecureNativeInvalidOptionsException ignored) {
164+
}
160165

161166
try {
162167
// ensure event to be sent
@@ -191,8 +196,13 @@ public void ShouldCallVerifyEventTest() throws SecureNativeSDKException, JsonPro
191196

192197

193198
// call verify event
194-
VerifyResult result = apiManager.verify(eventOptions);
199+
VerifyResult result = null;
200+
try {
201+
result = apiManager.verify(eventOptions);
202+
} catch (SecureNativeInvalidOptionsException ignored) {
203+
}
195204

205+
assert result != null;
196206
assertThat(result.getRiskLevel()).isEqualTo(verifyResult.getRiskLevel());
197207
assertThat(result.getScore()).isEqualTo(verifyResult.getScore());
198208
assertThat(result.getTriggers().length).isEqualTo(verifyResult.getTriggers().length);
@@ -220,7 +230,13 @@ public void ShouldFailVerifyEventCallWhenUnauthorizedTest() throws SecureNativeS
220230
ApiManager apiManager = new ApiManagerImpl(eventManager, options);
221231

222232
// call verify event
223-
VerifyResult verifyResult = apiManager.verify(eventOptions);
233+
VerifyResult verifyResult = null;
234+
try {
235+
verifyResult = apiManager.verify(eventOptions);
236+
} catch (SecureNativeInvalidOptionsException ignored) {
237+
}
238+
239+
assert verifyResult != null;
224240
assertThat(verifyResult.getRiskLevel()).isEqualTo(RiskLevel.LOW);
225241
assertThat(verifyResult.getScore()).isEqualTo(0);
226242
assertThat(verifyResult.getTriggers().length).isEqualTo(0);
@@ -230,8 +246,6 @@ public void ShouldFailVerifyEventCallWhenUnauthorizedTest() throws SecureNativeS
230246
RecordedRequest lastRequest = server.takeRequest(10 * options.getInterval(), TimeUnit.MILLISECONDS);
231247
String lastRequestBody = lastRequest != null ? lastRequest.getBody().readUtf8() : null;
232248

233-
String expected = "{\"eventType\":\"sn.user.login\",\"userId\":\"USER_ID\",\"userTraits\":{\"name\":\"USER_NAME'\",\"email\":\"USER_EMAIL'\",\"createdAt\":null},\"request\":{\"cid\":null,\"vid\":null,\"fp\":null,\"ip\":\"127.0.0.1\",\"remoteIp\":null,\"headers\":{\"user-agent\":\"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\"},\"url\":null,\"method\":null},\"properties\":{\"prop2\":true,\"prop1\":\"CUSTOM_PARAM_VALUE\",\"prop3\":3}}";
234249
assertThat(lastRequestBody).isNotNull();
235-
236250
}
237251
}

0 commit comments

Comments
 (0)