From 0297944d78bd4d9a8e0f31354bd325588abe9ee5 Mon Sep 17 00:00:00 2001 From: Daniele Ricci Date: Wed, 3 May 2017 14:42:41 +0200 Subject: [PATCH] Complete JMP verification provider (#77) Signed-off-by: Daniele Ricci --- .../registration/JMPVerifyProvider.java | 97 +++++++++++++++++-- .../registration/jmp/JmpVerifyClient.java | 15 ++- 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/kontalk/xmppserver/registration/JMPVerifyProvider.java b/src/main/java/org/kontalk/xmppserver/registration/JMPVerifyProvider.java index 2a50c72..f4ed0ca 100644 --- a/src/main/java/org/kontalk/xmppserver/registration/JMPVerifyProvider.java +++ b/src/main/java/org/kontalk/xmppserver/registration/JMPVerifyProvider.java @@ -18,15 +18,18 @@ package org.kontalk.xmppserver.registration; +import org.kontalk.xmppserver.registration.jmp.CheckResult; +import org.kontalk.xmppserver.registration.jmp.JmpVerifyClient; +import org.kontalk.xmppserver.registration.jmp.VerifyResult; import tigase.db.TigaseDBException; import tigase.xmpp.XMPPResourceConnection; import java.io.IOException; +import java.util.Map; /** * Verification provider for JMP verification service. - * TODO actual implementation * @see JMP - JIDs for Messaging with Phones * @author Daniele Ricci */ @@ -34,6 +37,18 @@ public class JMPVerifyProvider extends BrandedSMSVerificationProvider { private static final String ACK_INSTRUCTIONS = "A SMS containing a verification code will be sent to the phone number you provided."; + private String username; + private String password; + private String brand; + + @Override + public void init(Map settings) throws TigaseDBException { + super.init(settings); + username = (String) settings.get("username"); + password = (String) settings.get("password"); + brand = (String) settings.get("brand"); + } + @Override public String getAckInstructions() { return ACK_INSTRUCTIONS; @@ -41,27 +56,91 @@ public String getAckInstructions() { @Override public RegistrationRequest startVerification(String domain, String phoneNumber) throws IOException, VerificationRepository.AlreadyRegisteredException, TigaseDBException { - // TODO - return new RegistrationRequest() { - @Override - public String getSenderId() { - return "+00TODO"; + JmpVerifyClient client; + + client = new JmpVerifyClient(username, password); + + VerifyResult result; + + try { + result = client.verify(phoneNumber, brand, senderId, VerificationRepository.VERIFICATION_CODE_LENGTH, null); + } + catch (IOException e) { + throw new IOException("Error requesting verification", e); + } + + if (result != null) { + if (result.getStatus() == VerifyResult.STATUS_OK) { + return new JMPVerifyRequest(result.getRequestId()); + } + else { + throw new IOException("verification did not start (" + result.getErrorText() + ")"); } - }; + } + else { + throw new IOException("Unknown response"); + } } @Override public boolean endVerification(XMPPResourceConnection session, RegistrationRequest request, String proof) throws IOException, TigaseDBException { - return false; + if (proof == null || proof.length() == 0) { + return false; + } + + JmpVerifyClient client; + + client = new JmpVerifyClient(username, password); + + CheckResult result; + + try { + JMPVerifyRequest myRequest = (JMPVerifyRequest) request; + result = client.check(myRequest.getId(), proof); + } + catch (IOException e) { + throw new IOException("Error requesting verification", e); + } + + if (result != null) { + if (result.getStatus() == CheckResult.STATUS_OK) { + return true; + } + else if (result.getStatus() == CheckResult.STATUS_INVALID_CODE) { + return false; + } + else { + throw new IOException("verification did not start (" + result.getErrorText() + ")"); + } + } + else { + throw new IOException("Unknown response"); + } } @Override public boolean supportsRequest(RegistrationRequest request) { - return false; + return request instanceof JMPVerifyRequest; } @Override public String getChallengeType() { return CHALLENGE_PIN; } + + private static final class JMPVerifyRequest implements RegistrationRequest { + private final String id; + public JMPVerifyRequest(String id) { + this.id = id; + } + + @Override + public String getSenderId() { + return null; + } + + public String getId() { + return id; + } + } } diff --git a/src/main/java/org/kontalk/xmppserver/registration/jmp/JmpVerifyClient.java b/src/main/java/org/kontalk/xmppserver/registration/jmp/JmpVerifyClient.java index 8eda35e..21f5d6b 100644 --- a/src/main/java/org/kontalk/xmppserver/registration/jmp/JmpVerifyClient.java +++ b/src/main/java/org/kontalk/xmppserver/registration/jmp/JmpVerifyClient.java @@ -27,7 +27,6 @@ import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.util.Locale; @@ -58,13 +57,14 @@ public class JmpVerifyClient { private final String apiKey; private final String apiSecret; + // TODO these two are not used yet private final int connectionTimeout; private final int soTimeout; private JmpVerifyService service; public JmpVerifyClient(final String apiKey, - final String apiSecret) throws ParserConfigurationException { + final String apiSecret) { this(DEFAULT_BASE_URL, apiKey, apiSecret, @@ -73,12 +73,11 @@ public JmpVerifyClient(final String apiKey, } /** - * Instanciate a new NexmoVerifyClient instance that will communicate using the supplied credentials, and will use the supplied connection and read timeout values.
- * Additionally, you can specify an alternative service base url. For example submitting to a testing sandbox environment, - * or if requested to submit to an alternative address by Nexmo, for example, in cases where it may be necessary to prioritize your traffic. + * Instanciate a new JmpVerifyClient instance that will communicate using the supplied credentials, and will use the supplied connection and read timeout values.
+ * Additionally, you can specify an alternative service base url. * - * @param apiKey Your Nexmo account api key - * @param apiSecret Your Nexmo account api secret + * @param apiKey Your JMP account api key + * @param apiSecret Your JMP account api secret * @param connectionTimeout over-ride the default connection timeout with this value (in milliseconds) * @param soTimeout over-ride the default read-timeout with this value (in milliseconds) */ @@ -86,7 +85,7 @@ public JmpVerifyClient(final String baseUrl, final String apiKey, final String apiSecret, final int connectionTimeout, - final int soTimeout) throws ParserConfigurationException { + final int soTimeout) { // Derive a http and a https version of the supplied base url if (baseUrl == null)