Skip to content

Commit

Permalink
fix: Remove Silent Auth check
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Oct 12, 2023
1 parent 1c595e3 commit 45de247
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 158 deletions.
68 changes: 6 additions & 62 deletions src/main/java/com/vonage/client/verify2/Verify2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@
import com.vonage.client.auth.JWTAuthMethod;
import com.vonage.client.auth.TokenAuthMethod;
import com.vonage.client.common.HttpMethod;
import java.net.URI;
import java.util.Objects;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;

public class Verify2Client {
final boolean hasJwtAuthMethod;
final RestEndpoint<VerificationRequest, VerificationResponse> verifyUser;
final RestEndpoint<VerifyCodeRequestWrapper, Void> verifyRequest;
final RestEndpoint<UUID, Void> cancel;
final RestEndpoint<URI, SilentAuthResponse> silentAuthCheck;

/**
* Create a new Verify2Client.
Expand All @@ -43,23 +41,22 @@ public Verify2Client(HttpWrapper wrapper) {

@SuppressWarnings("unchecked")
final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
Endpoint(BiFunction<String, T, String> pathGetter, HttpMethod method, R... type) {
Endpoint(Function<T, String> pathGetter, HttpMethod method, R... type) {
super(DynamicEndpoint.<T, R> builder(type)
.responseExceptionType(VerifyResponseException.class)
.wrapper(wrapper).requestMethod(method)
.addAuthMethodIfTrue(method != HttpMethod.GET, JWTAuthMethod.class, TokenAuthMethod.class)
.pathGetter((de, req) -> {
String base = de.getHttpWrapper().getHttpConfig().getVersionedApiBaseUri("v2") + "/verify";
return pathGetter.apply(base, req);
return pathGetter != null ? base + "/" + pathGetter.apply(req) : base;
})
);
}
}

verifyUser = new Endpoint<>((base, req) -> base, HttpMethod.POST);
verifyRequest = new Endpoint<>((base, req) -> base + '/' + req.requestId, HttpMethod.POST);
cancel = new Endpoint<>((base, req) -> base + '/' + req, HttpMethod.DELETE);
silentAuthCheck = new Endpoint<>((base, req) -> req.toString(), HttpMethod.GET);
verifyUser = new Endpoint<>(null, HttpMethod.POST);
verifyRequest = new Endpoint<>(req -> req.requestId, HttpMethod.POST);
cancel = new Endpoint<>(UUID::toString, HttpMethod.DELETE);
}

private UUID validateRequestId(UUID requestId) {
Expand Down Expand Up @@ -137,57 +134,4 @@ public void checkVerificationCode(UUID requestId, String code) {
public void cancelVerification(UUID requestId) {
cancel.execute(validateRequestId(requestId));
}

/**
* Final step of Silent Authentication workflow. Once the {@linkplain #sendVerification(VerificationRequest)}
* has been called, pass the response to this method to complete the verification workflow. This method uses
* the {@linkplain #checkVerificationCode(UUID, String)} under the hood with a code obtained from the API
* after following the `check_url` redirect. Refer to the
* <a href=https://developer.vonage.com/en/verify/guides/silent-authentication>
* Silent Authentication documentation</a> for more details.
*
* @param verifyResponse The VerificationResponse, as obtained from {@link #sendVerification(VerificationRequest)}.
*
* @throws VerifyResponseException If the Silent Authentication workflow failed due
* to a network error (409 HTTP status response).
*
* @since v7.10.0
*/
public void checkSilentAuth(VerificationResponse verifyResponse) {
Objects.requireNonNull(verifyResponse, "Response object cannot be null.");
URI checkUrl = verifyResponse.getCheckUrl();
if (checkUrl == null) {
throw new IllegalStateException("'check_url' is missing in the response.");
}
SilentAuthResponse response = silentAuthCheck.execute(checkUrl);
checkVerificationCode(response.getRequestId(), response.getCode());
}

/*/**
* A fully declarative, automated utility method for performing Silent Authentication using
* a device's mobile network connection. If the authentication failed due to a network error,
* this method will return {@code false}. If a failure occurs for any other reason, a
* {@linkplain VerifyResponseException} will be thrown.
*
* @param number The device's SIM (phone) number in E.164 format.
*
* @return {@code true} if the authentication was successful.
*
* @throws VerifyResponseException If the workflow fails for any reason other than a 409 Network error.
*
* @since v7.10.0
public boolean doSilentAuthWorkflow(String number) {
VerificationRequest request = VerificationRequest.builder()
.addWorkflow(new SilentAuthWorkflow(number))
.brand("Vonage Java SDK").build();
VerificationResponse response = sendVerification(request);
try {
checkSilentAuth(response);
return true;
}
catch (VerifyResponseException ex) {
return false;
}
}*/
}
99 changes: 3 additions & 96 deletions src/test/java/com/vonage/client/verify2/Verify2ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import com.vonage.client.ClientTest;
import com.vonage.client.HttpWrapper;
import com.vonage.client.RestEndpoint;
import com.vonage.client.auth.AuthMethod;
import com.vonage.client.common.HttpMethod;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.Executable;
import java.net.URI;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class Verify2ClientTest extends ClientTest<Verify2Client> {
static final UUID REQUEST_ID = UUID.randomUUID();
Expand Down Expand Up @@ -363,98 +364,4 @@ void testParse404AllParams() throws Exception {
}
.runTests();
}

@Test
public void testVerifySilentAuth() throws Exception {
VerificationResponse verificationResponse = VerificationResponse.fromJson(
"{\"request_id\":\""+REQUEST_ID+"\",\"check_url\":\"https://example.com/verify2/redirect\"}"
);
String successJson = "{\"request_id\":\""+REQUEST_ID+"\",\"code\":\""+CODE+"\"}";
stubResponseAndRun(successJson, () -> client.checkSilentAuth(verificationResponse));
stubResponseAndAssertThrows(200, successJson,
() -> client.checkSilentAuth(null),
NullPointerException.class
);

String title = "Network error", detail =
"The Silent Auth request could not be completed due to formatting or the carrier is not supported";

stubResponse(409, "{\"title\":\""+title+"\",\"detail\":\""+detail+"\"}");
try {
client.checkSilentAuth(verificationResponse);
fail("Expected " + VerifyResponseException.class.getName());
}
catch (VerifyResponseException ex) {
assertEquals(409, ex.getStatusCode());
assertEquals(title, ex.getTitle());
assertEquals(detail, ex.getDetail());
}

VerificationResponse missingCheckUrlResponse = VerificationResponse.fromJson(
"{\"request_id\":\""+REQUEST_ID+"\"}"
);
stubResponseAndAssertThrows(200, successJson,
() -> client.checkSilentAuth(missingCheckUrlResponse),
IllegalStateException.class
);
}

@Test
public void testSilentAuthCheckEndpoint() throws Exception {
new Verify2EndpointTestSpec<URI, SilentAuthResponse>() {

@Override
protected RestEndpoint<URI, SilentAuthResponse> endpoint() {
return client.silentAuthCheck;
}

@Override
protected HttpMethod expectedHttpMethod() {
return HttpMethod.GET;
}

@Override
protected Collection<Class<? extends AuthMethod>> expectedAuthMethods() {
return Collections.emptyList();
}

@Override
protected String expectedDefaultBaseUri() {
return "";
}

@Override
protected String customBaseUri() {
return expectedDefaultBaseUri();
}

@Override
protected String expectedEndpointUri(URI request) {
return request.toString();
}

@Override
protected URI sampleRequest() {
return URI.create("https://api-eu-3.vonage.com/v2/verify/"+REQUEST_ID+"/silent-auth/redirect");
}

@Override
public void runTests() throws Exception {
super.runTests();
testParseResponse();
}

private void testParseResponse() throws Exception {
stubResponse(200, "{\n" +
" \"request_id\": \""+REQUEST_ID+"\",\n" +
" \"code\": \"si9sfG\"\n" +
"}"
);
SilentAuthResponse response = endpoint().execute(sampleRequest());
assertEquals(REQUEST_ID, response.getRequestId());
assertEquals("si9sfG", response.getCode());
}
}
.runTests();
}
}

0 comments on commit 45de247

Please sign in to comment.