Skip to content

Commit

Permalink
feat: Add missing Silent Auth fields
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Oct 13, 2023
1 parent 9081ed4 commit 422f4ae
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/main/java/com/vonage/client/verify2/SilentAuthWorkflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vonage.client.verify2;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Defines properties for mobile network-based authentication. See the
Expand All @@ -24,6 +25,7 @@
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public final class SilentAuthWorkflow extends Workflow {
private Boolean sandbox;

/**
* Constructs a new Silent Auth verification workflow.
Expand All @@ -33,4 +35,26 @@ public final class SilentAuthWorkflow extends Workflow {
public SilentAuthWorkflow(String to) {
super(Channel.SILENT_AUTH, to);
}

/**
* Constructs a new Silent Auth verification workflow.
*
* @param to The number to registered to the device on the network to authenticate.
*/
public SilentAuthWorkflow(String to, boolean sandbox) {
this(to);
this.sandbox = sandbox;
}

/**
* Optional parameter if using the Vonage Sandbox to test Silent Auth integrations.
*
* @return Whether the Vonage Sandbox will be used, or {@code null} if not specified (the default).
*
* @since 7.10.0
*/
@JsonProperty("sandbox")
public Boolean getSandbox() {
return sandbox;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public Builder clientRef(String clientRef) {
}

/**
* (OPTIONAL)
* Set this parameter to {@code false} to force through the request even if it's
* blocked by the network's fraud protection. Refer to
* <a href=https://developer.vonage.com/en/verify/verify-v2/guides/v2-anti-fraud>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.vonage.client.Jsonable;
import java.net.URI;
import java.util.UUID;

/**
Expand All @@ -26,6 +27,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class VerificationResponse implements Jsonable {
protected UUID requestId;
protected URI checkUrl;

/**
* Protected to prevent users from explicitly creating this object.
Expand All @@ -43,9 +45,21 @@ public UUID getRequestId() {
return requestId;
}

/**
* URL for Silent Auth Verify workflow completion (only present if using Silent Auth).
*
* @return The URL to check for Silent Authentication, or {@code null} if not applicable.
*
* @since 7.10.0
*/
@JsonProperty("check_url")
public URI getCheckUrl() {
return checkUrl;
}

@Override
public String toString() {
return getClass().getSimpleName()+" {requestId='"+requestId+"'}";
return getClass().getSimpleName() + "{requestId=" + requestId + ", checkUrl=" + checkUrl + '}';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;

public class VerificationRequestTest {
static final boolean SANDBOX = true;
static final Locale LOCALE = Locale.PORTUGUESE_PORTUGAL;
static final int CODE_LENGTH = 8, CHANNEL_TIMEOUT = 120;
static final String
Expand Down Expand Up @@ -64,6 +65,8 @@ Workflow getWorkflowRequiredParamsForChannel(Channel channel) {

Workflow getWorkflowAllParamsForChannel(Channel channel) {
switch (channel) {
case SILENT_AUTH:
return new SilentAuthWorkflow(TO_NUMBER, SANDBOX);
case SMS:
return new SmsWorkflow(TO_NUMBER, APP_HASH);
case WHATSAPP:
Expand Down Expand Up @@ -96,22 +99,30 @@ String getExpectedRequiredParamsForSingleWorkflowJson(Channel channel) {

String getExpectedAllParamsForSingleWorkflowJson(Channel channel) {
String expectedJson = getExpectedRequiredParamsForSingleWorkflowJson(channel), prefix, replacement;

if (channel == Channel.SMS) {
prefix = TO_NUMBER + "\"";
prefix = TO_NUMBER + '"';
replacement = prefix + ",\"app_hash\":\""+APP_HASH+"\"";
expectedJson = expectedJson.replace(prefix, replacement);
}
if (channel == Channel.WHATSAPP) {
prefix = TO_NUMBER + "\"";
prefix = TO_NUMBER + '"';
replacement = prefix + ",\"from\":\""+FROM_NUMBER+"\"";
expectedJson = expectedJson.replace(prefix, replacement);
}
if (channel == Channel.EMAIL) {
prefix = TO_EMAIL + "\"";
prefix = TO_EMAIL + '"';
replacement = prefix + ",\"from\":\""+FROM_EMAIL+"\"";
expectedJson = expectedJson.replace(prefix, replacement);
}
if (channel == Channel.SILENT_AUTH) {
prefix = TO_NUMBER + '"';
replacement = prefix + ",\"sandbox\":" + SANDBOX;
expectedJson = expectedJson.replace(prefix, replacement);
}

prefix = "{\"locale\":\"pt-pt\",\"channel_timeout\":"+ CHANNEL_TIMEOUT;

if (channel != Channel.SILENT_AUTH && channel != Channel.WHATSAPP_INTERACTIVE) {
prefix += ",\"code_length\":"+CODE_LENGTH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
package com.vonage.client.verify2;

import com.vonage.client.VonageResponseParseException;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import java.net.URI;
import java.util.UUID;

public class VerificationResponseTest {

@Test
public void testConstructFromValidJson() {
UUID rqid = UUID.randomUUID();
VerificationResponse response = VerificationResponse.fromJson("{\"request_id\":\""+rqid+"\"}");
String checkUrl = "https://example.com/v2/"+rqid+"/silent-auth/redirect";
VerificationResponse response = VerificationResponse.fromJson(
"{\"request_id\":\""+rqid+"\",\"check_url\":\""+checkUrl+"\"}"
);
assertEquals(rqid, response.getRequestId());
assertEquals(URI.create(checkUrl), response.getCheckUrl());
String toString = response.toString();
assertTrue(toString.contains("VerificationResponse"));
assertTrue(toString.contains(rqid.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ VerificationRequest newVerificationRequestWithAllParamsAndWorkflows() {
String toNumber = "447100000009", fromNumber = "447900000001",
toEmail = "[email protected]", fromEmail = "[email protected]";
List<Workflow> workflows = Arrays.asList(
new SilentAuthWorkflow(toNumber),
new SilentAuthWorkflow(toNumber, true),
new SmsWorkflow(toNumber),
new EmailWorkflow(toEmail, fromEmail),
new VoiceWorkflow(toNumber),
Expand All @@ -71,6 +71,7 @@ void stubSuccessfulVerifyUserResponseAndRun(VerificationRequest request) throws
VerificationResponse response = client.sendVerification(request);
assertNotNull(response);
assertEquals(REQUEST_ID, response.getRequestId());
assertNull(response.getCheckUrl());
}

@Test
Expand Down

0 comments on commit 422f4ae

Please sign in to comment.