Skip to content

Commit

Permalink
Support web login (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
racevedoo authored Nov 17, 2023
1 parent 4257503 commit 5eba8d3
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 3 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ And then download the artifact incognia-api-client
<dependency>
<groupId>com.incognia</groupId>
<artifactId>incognia-api-client</artifactId>
<version>2.4.1</version>
<version>2.5.0</version>
</dependency>
```

Expand All @@ -39,7 +39,7 @@ repositories {
And then add the dependency
```gradle
dependencies {
implementation 'com.incognia:incognia-api-client:2.4.1'
implementation 'com.incognia:incognia-api-client:2.5.0'
}
```
We support Java 8+.
Expand Down Expand Up @@ -141,6 +141,29 @@ try {
}
```

#### Registering Web Login

This method registers a new web login for the given installation and account, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence.

```java
IncogniaAPI api = new IncogniaAPI("client-id", "client-secret");
try {
RegisterLoginRequest registerLoginRequest =
RegisterLoginRequest.builder()
.installationId("installation id")
.accountId("account id")
.externalId("external id")
.sessionToken("session-token")
.evaluateTransaction(true) // can be omitted as it uses true as the default value
.build();
TransactionAssessment assessment = api.registerWebLogin(registerLoginRequest);
} catch (IncogniaAPIException e) {
//Some api error happened (invalid data, invalid credentials)
} catch (IncogniaException e) {
//Something unexpected happened
}
```

#### Registering Payment

This method registers a new payment for the given installation and account, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.incognia"
version = "2.4.1"
version = "2.5.0"

task createProjectVersionFile {
def projectVersionDir = "$projectDir/src/main/java/com/incognia/api"
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/incognia/api/IncogniaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.incognia.transaction.TransactionAddress;
import com.incognia.transaction.TransactionAssessment;
import com.incognia.transaction.login.RegisterLoginRequest;
import com.incognia.transaction.login.RegisterWebLoginRequest;
import com.incognia.transaction.payment.RegisterPaymentRequest;
import java.time.Instant;
import java.util.HashMap;
Expand Down Expand Up @@ -199,6 +200,61 @@ public TransactionAssessment registerLogin(RegisterLoginRequest request)
queryParameters);
}

/**
* Registers a web login to obtain a risk assessment. Check <a
* href="https://dash.incognia.com/api-reference#operation/transactions-post">the docs</a><br>
* Example:
*
* <pre>{@code
* IncogniaAPI api = new IncogniaAPI("client-id", "client-secret", Region.BR);
* try {
* RegisterLoginRequest loginRequest = RegisterLoginRequest.builder()
* .installationId("installation-id")
* .accountId("account-id")
* .externalId("external-id")
* .sessionToken("session-token")
* .evaluateTransaction(true) // can be omitted as it uses true as the default value
* .build();
* TransactionAssessment assessment = api.registerLogin(loginRequest);
* } catch (IncogniaAPIException e) {
* //Some api error happened (invalid data, invalid credentials)
* } catch (IncogniaException e) {
* //Something unexpected happened
* }
* }</pre>
*
* @param request the {@link RegisterWebLoginRequest} model with the properties we need to make
* the assessment
* @return the assessment for the login
* @throws IncogniaAPIException in case of api errors
* @throws IncogniaException in case of unexpected errors
*/
public TransactionAssessment registerWebLogin(RegisterWebLoginRequest request)
throws IncogniaException {
Asserts.assertNotNull(request, "register login request");
Asserts.assertNotEmpty(request.getInstallationId(), "installation id");
Asserts.assertNotEmpty(request.getAccountId(), "account id");
Asserts.assertNotEmpty(request.getSessionToken(), "session token");
PostTransactionRequestBody requestBody =
PostTransactionRequestBody.builder()
.installationId(request.getInstallationId())
.accountId(request.getAccountId())
.externalId(request.getExternalId())
.sessionToken(request.getSessionToken())
.type("login")
.build();

Map<String, String> queryParameters = new HashMap<>();
if (request.shouldEvaluateTransaction() != null) {
queryParameters.put(EVALUATION_PARAMETER, request.shouldEvaluateTransaction().toString());
}
return tokenAwareNetworkingClient.doPost(
"api/v2/authentication/transactions",
requestBody,
TransactionAssessment.class,
queryParameters);
}

/**
* Registers a payment to obtain a risk assessment. Check <a
* href="https://dash.incognia.com/api-reference#operation/transactions-post">the docs</a><br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class PostTransactionRequestBody {
String installationId;
String accountId;
String sessionToken;

String type;
String externalId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.incognia.transaction.login;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.Value;

@Value
@Builder
public class RegisterWebLoginRequest {
String installationId;
String accountId;
String externalId;
String sessionToken;

@Getter(AccessLevel.NONE)
Boolean evaluateTransaction;

public Boolean shouldEvaluateTransaction() {
return this.evaluateTransaction;
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/incognia/api/IncogniaAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.incognia.transaction.TransactionAddress;
import com.incognia.transaction.TransactionAssessment;
import com.incognia.transaction.login.RegisterLoginRequest;
import com.incognia.transaction.login.RegisterWebLoginRequest;
import com.incognia.transaction.payment.CardInfo;
import com.incognia.transaction.payment.PaymentMethod;
import com.incognia.transaction.payment.PaymentType;
Expand Down Expand Up @@ -219,6 +220,42 @@ void testRegisterLogin_whenDataIsValid(Boolean eval) {
assertTransactionAssessment(transactionAssessment);
}

@ParameterizedTest
@ValueSource(booleans = {true})
@NullSource
@DisplayName("should return the expected web login transaction response")
@SneakyThrows
void testRegisterWebLogin_whenDataIsValid(Boolean eval) {
String token = TokenCreationFixture.createToken();
String installationId = "installation-id";
String accountId = "account-id";
String externalId = "external-id";
String sessionToken = "session-token";

TokenAwareDispatcher dispatcher = new TokenAwareDispatcher(token, CLIENT_ID, CLIENT_SECRET);
dispatcher.setExpectedTransactionRequestBody(
PostTransactionRequestBody.builder()
.installationId(installationId)
.externalId(externalId)
.accountId(accountId)
.type("login")
.sessionToken(sessionToken)
.addresses(null)
.paymentMethods(null)
.build());
mockServer.setDispatcher(dispatcher);
RegisterWebLoginRequest loginRequest =
RegisterWebLoginRequest.builder()
.installationId(installationId)
.accountId(accountId)
.externalId(externalId)
.evaluateTransaction(eval)
.sessionToken(sessionToken)
.build();
TransactionAssessment transactionAssessment = client.registerWebLogin(loginRequest);
assertTransactionAssessment(transactionAssessment);
}

@Test
@DisplayName("should return an empty response")
@SneakyThrows
Expand Down

0 comments on commit 5eba8d3

Please sign in to comment.