Skip to content

Commit

Permalink
JMP.Chat verification client (#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 Apr 25, 2017
1 parent 79c1f73 commit 60799f2
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Kontalk XMPP Tigase extension
* Copyright (C) 2017 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.xmppserver.registration.jmp;


/**
* Verification base result class.
* @author Daniele Ricci
*/
public abstract class BaseResult {

/**
* Verify was successfully submitted to the JMP service
*/
public static final int STATUS_OK = 0;

/**
* Verify was rejected due to exceeding the maximum throughput allowed for this account.<br>
* Verify can be re-requested after a short delay
*/
public static final int STATUS_THROTTLED = 1;

/**
* Verify was rejected due to a failure within the JMP systems.<br>
* Verify can be re-submitted after a short delay
*/
public static final int STATUS_INTERNAL_ERROR = 2;

/**
* The code inserted does not match the expected value
*/
public static final int STATUS_INVALID_CODE = 16;

/**
* There are no matching verification requests
*/
public static final int STATUS_NO_RESPONSE = 101;

/**
* A network error occured
*/
public static final int STATUS_COMMS_FAILURE = -1;

private final int status;
private final String errorText;

protected BaseResult(final int status,
final String errorText) {
this.status = status;
this.errorText = errorText;
}

public int getStatus() {
return this.status;
}

public String getErrorText() {
return this.errorText;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Kontalk XMPP Tigase extension
* Copyright (C) 2017 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.xmppserver.registration.jmp;


/**
* Verification check result.
* @author Daniele Ricci
*/
public class CheckResult extends BaseResult {

private final String eventId;

protected CheckResult(final int status,
final String eventId,
final String errorText) {
super(status, errorText);
this.eventId = eventId;
}

public String getEventId() {
return this.eventId;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Kontalk XMPP Tigase extension
* Copyright (C) 2017 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.xmppserver.registration.jmp;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.Locale;


/**
* Client for talking to the JMP REST interface
* @author Daniele Ricci
*/
public class JmpVerifyClient {
private static final Log log = LogFactory.getLog(JmpVerifyClient.class);

/**
* Service url used unless overridden on the constructor
*/
public static final String DEFAULT_BASE_URL = "https://jvr.api.jmp.chat";

/**
* Default connection timeout of 5000ms used by this client unless specifically overridden onb the constructor
*/
public static final int DEFAULT_CONNECTION_TIMEOUT = 5000;

/**
* Default read timeout of 30000ms used by this client unless specifically overridden onb the constructor
*/
public static final int DEFAULT_SO_TIMEOUT = 30000;

private final String baseUrl;
private final String apiKey;
private final String apiSecret;

private final int connectionTimeout;
private final int soTimeout;

private JmpVerifyService service;

public JmpVerifyClient(final String apiKey,
final String apiSecret) throws ParserConfigurationException {
this(DEFAULT_BASE_URL,
apiKey,
apiSecret,
DEFAULT_CONNECTION_TIMEOUT,
DEFAULT_SO_TIMEOUT);
}

/**
* 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.
*
* @param apiKey Your Nexmo account api key
* @param apiSecret Your Nexmo 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 {

// Derive a http and a https version of the supplied base url
if (baseUrl == null)
throw new IllegalArgumentException("base url is null");
String url = baseUrl.trim();
String lc = url.toLowerCase();
if (!lc.startsWith("http://") && !lc.startsWith("https://"))
throw new IllegalArgumentException("base url does not start with http:// or https://");

this.baseUrl = url;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.service = buildService();
}

public VerifyResult verify(String number, String brand) throws IOException {
return verify(number, brand, null, -1, null);
}

public VerifyResult verify(String number, String brand, String from) throws IOException {
return verify(number, brand, from, -1, null);
}

public VerifyResult verify(String number, String brand, String from, int length, Locale locale)
throws IOException {
if (number == null || brand == null)
throw new IllegalArgumentException("number and brand parameters are mandatory.");
if (length > 0 && length != 4 && length != 6)
throw new IllegalArgumentException("code length must be 4 or 6.");

Response<VerifyResult> response = service.verify(apiKey, apiSecret, number, brand).execute();
if (response.isSuccessful()) {
return response.body();
}
else {
return new VerifyResult(BaseResult.STATUS_COMMS_FAILURE, null, "Communication error");
}
}

public CheckResult check(String requestId, String code) throws IOException {
if (requestId == null || code == null)
throw new IllegalArgumentException("request ID and code parameters are mandatory.");

Response<CheckResult> response = service.check(apiKey, apiSecret, requestId, code).execute();
if (response.isSuccessful()) {
return response.body();
}
else {
return new CheckResult(BaseResult.STATUS_COMMS_FAILURE, null, "Communication error");
}
}

private JmpVerifyService buildService() {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

return new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(JmpVerifyService.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Kontalk XMPP Tigase extension
* Copyright (C) 2017 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.xmppserver.registration.jmp;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;


public interface JmpVerifyService {

@GET("verify")
Call<VerifyResult> verify(@Query("api_key") String apiKey, @Query("api_secret") String apiSecret,
@Query("number") String number, @Query("brand") String brand);

@GET("verify/check")
Call<CheckResult> check(@Query("api_key") String apiKey, @Query("api_secret") String apiSecret,
@Query("request_id") String requestId, @Query("code") String code);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Kontalk XMPP Tigase extension
* Copyright (C) 2017 Kontalk Devteam <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kontalk.xmppserver.registration.jmp;


/**
* Verification request result.
* @author Daniele Ricci
*/
public class VerifyResult extends BaseResult {

private final String requestId;

protected VerifyResult(final int status,
final String requestId,
final String errorText) {
super(status, errorText);
this.requestId = requestId;
}

public String getRequestId() {
return this.requestId;
}

}
Loading

0 comments on commit 60799f2

Please sign in to comment.