Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ hs_err_pid*
/.project
*.iml
.idea
.java-version
4 changes: 3 additions & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Development

Use java8 for development

## Setup

To be able to run tests, create a file `castle_sdk.properties` in the folder `src/test/resources`. The content should look something like this
Expand Down Expand Up @@ -43,4 +45,4 @@ To check test coverage run:

mvn clean test jacoco:report

The coverage report will be on the page `target/jacoco-ut/index.html`
The coverage report will be on the page `target/jacoco-ut/index.html`
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import io.castle.client.model.AuthenticateAction;
import io.castle.client.model.RiskPolicyType;
import io.castle.client.model.CastleHeaders;
import io.castle.client.model.CastleMessage;

Expand All @@ -21,6 +22,7 @@ public CastleGsonModel() {
builder.registerTypeAdapter(CastleMessage.class, new CastleMessageSerializer());
builder.registerTypeAdapter(CastleHeaders.class, new CastleHeadersDeserializer());
builder.registerTypeAdapter(AuthenticateAction.class, new AuthenticateActionDeserializer());
builder.registerTypeAdapter(RiskPolicyType.class, new RiskPolicyTypeDeserializer());
this.gson = builder.create();

this.jsonParser = new JsonParser();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.castle.client.internal.json;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import io.castle.client.model.RiskPolicyType;

import java.lang.reflect.Type;

public class RiskPolicyTypeDeserializer implements JsonDeserializer<RiskPolicyType> {
@Override
public RiskPolicyType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return RiskPolicyType.fromType(json.getAsString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.google.gson.JsonElement;
import io.castle.client.model.AuthenticateAction;
import io.castle.client.model.RiskPolicyResult;
import io.castle.client.model.Verdict;

public class VerdictBuilder {
private AuthenticateAction action;
private RiskPolicyResult riskPolicy;
private String userId;
private boolean failover;
private String failoverReason;
Expand Down Expand Up @@ -36,6 +38,12 @@ public VerdictBuilder withUserId(String userId) {
return this;
}


public VerdictBuilder withRiskPolicy(RiskPolicyResult riskPolicy) {
this.riskPolicy = riskPolicy;
return this;
}

public VerdictBuilder withDeviceToken(final String deviceToken) {
this.deviceToken = deviceToken;
return this;
Expand All @@ -48,11 +56,11 @@ public Verdict build() {
verdict.setFailover(failover);
verdict.setFailoverReason(failoverReason);
verdict.setDeviceToken(deviceToken);
verdict.setRiskPolicy(riskPolicy);
verdict.setInternal(internal);
return verdict;
}


public VerdictBuilder withFailover(boolean failover) {
this.failover = failover;
return this;
Expand All @@ -74,6 +82,7 @@ public static Verdict fromTransport(VerdictTransportModel transport, JsonElement
.withAction(transport.getAction())
.withUserId(transport.getUserId())
.withDeviceToken(transport.getDeviceToken())
.withRiskPolicy(transport.getRiskPolicy())
.withInternal(internal)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.castle.client.internal.utils;

import io.castle.client.model.AuthenticateAction;
import io.castle.client.model.RiskPolicyResult;

public class VerdictTransportModel {

private AuthenticateAction action;
private RiskPolicyResult riskPolicy;
private String userId;
private String deviceToken;

Expand All @@ -16,6 +18,14 @@ public void setAction(AuthenticateAction action) {
this.action = action;
}

public RiskPolicyResult getRiskPolicy() {
return riskPolicy;
}

public void setRiskPolicy(RiskPolicyResult riskPolicy) {
this.riskPolicy = riskPolicy;
}

public String getUserId() {
return userId;
}
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/io/castle/client/model/AuthenticateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,10 @@ public enum AuthenticateAction {
* @return the enum value matching the name, or null if it does not match any enum
*/
public static AuthenticateAction fromAction(String action) {
if (action == null) {
return null;
}
try {
return AuthenticateAction.valueOf(action);
} catch (IllegalArgumentException e) {
// no op, use string compare functions.
}
if (action.compareToIgnoreCase(ALLOW.name()) == 0) {
return ALLOW;
}
if (action.compareToIgnoreCase(DENY.name()) == 0) {
return DENY;
}
if (action.compareToIgnoreCase(CHALLENGE.name()) == 0) {
return CHALLENGE;
for (AuthenticateAction kind : AuthenticateAction.class.getEnumConstants()) {
Comment thread
bartes marked this conversation as resolved.
if (kind.name().equalsIgnoreCase(action)) {
return kind;
}
}
return null;
}
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/io/castle/client/model/RiskPolicyResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.castle.client.model;

/**
* Information about risk policy used for the action.
* <p>
* See the documentation for the semantics of each case.
* It can be null.
*
* @see <a href="https://castle.io/docs/authentication">Adaptive authentication</a>
*/
public class RiskPolicyResult {

/**
* id of the risk policy
*/
private String id;

/**
* id of the risk policy revision
*/
private String revisionId;

/**
* name of the risk policy
*/
private String name;

/**
* type of the risk policy
*/
private RiskPolicyType type;


public String getId() {
Comment thread
bartes marked this conversation as resolved.
return id;
}

public String getRevisionId() {
return revisionId;
}

public String getName() {
return name;
}

public RiskPolicyType getType() {
return type;
}
}

23 changes: 23 additions & 0 deletions src/main/java/io/castle/client/model/RiskPolicyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.castle.client.model;

/**
* Type of Risk policy
*/
public enum RiskPolicyType {
BOT, AUTHENTICATION;

/**
* Returns an RiskPolicyType from a string representing its name.
*
* @param type string representing the name of the RiskPolicyType, case-insensitive
* @return the enum value matching the name, or null if it does not match any enum
*/
public static RiskPolicyType fromType(String type) {
for (RiskPolicyType kind : RiskPolicyType.class.getEnumConstants()) {
if (kind.name().equalsIgnoreCase(type)) {
return kind;
}
}
return null;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/castle/client/model/Verdict.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class Verdict {
*/
private String deviceToken;

/**
* RiskPolicyResult representing risk policy used for generating this verdict.
*/
private RiskPolicyResult riskPolicy;

/**
* JsonElement representing the full response of the server request
*/
Expand Down Expand Up @@ -80,4 +85,12 @@ public void setDeviceToken(String deviceToken) {
public void setInternal(JsonElement internal) {
this.internal = internal;
}

public RiskPolicyResult getRiskPolicy() {
return riskPolicy;
}

public void setRiskPolicy(RiskPolicyResult riskPolicy) {
this.riskPolicy = riskPolicy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void tearDown() throws Exception {

protected <T> void waitForValueAndVerify(AtomicReference<T> result, T expected) {
T extracted = waitForValue(result);
Assertions.assertThat(extracted).isEqualToComparingFieldByField(expected);
Assertions.assertThat(extracted).isEqualToComparingFieldByFieldRecursively(expected);
}

protected <T> T waitForValue(AtomicReference<T> result) {
Expand Down
Loading