Skip to content

Commit

Permalink
Complete JMP verification provider (#77)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed May 3, 2017
1 parent 60799f2 commit 0297944
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,129 @@

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 <a href="https://jmp.chat/">JMP - JIDs for Messaging with Phones</a>
* @author Daniele Ricci
*/
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<String, Object> 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;
}

@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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -73,20 +73,19 @@ 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.<br>
* 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.<br>
* 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)
*/
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)
Expand Down

0 comments on commit 0297944

Please sign in to comment.