diff --git a/src/main/java/com/vonage/client/verify2/Verify2Client.java b/src/main/java/com/vonage/client/verify2/Verify2Client.java index 207692788..e0da065ed 100644 --- a/src/main/java/com/vonage/client/verify2/Verify2Client.java +++ b/src/main/java/com/vonage/client/verify2/Verify2Client.java @@ -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 verifyUser; final RestEndpoint verifyRequest; final RestEndpoint cancel; - final RestEndpoint silentAuthCheck; /** * Create a new Verify2Client. @@ -43,23 +41,22 @@ public Verify2Client(HttpWrapper wrapper) { @SuppressWarnings("unchecked") final class Endpoint extends DynamicEndpoint { - Endpoint(BiFunction pathGetter, HttpMethod method, R... type) { + Endpoint(Function pathGetter, HttpMethod method, R... type) { super(DynamicEndpoint. 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) { @@ -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 - * - * Silent Authentication documentation 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; - } - }*/ } diff --git a/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java b/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java index 5eaa3773e..04cea9a5d 100644 --- a/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java +++ b/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java @@ -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 { static final UUID REQUEST_ID = UUID.randomUUID(); @@ -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() { - - @Override - protected RestEndpoint endpoint() { - return client.silentAuthCheck; - } - - @Override - protected HttpMethod expectedHttpMethod() { - return HttpMethod.GET; - } - - @Override - protected Collection> 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(); - } }