Change SMS/Voice code delivery priorities.

1) Honor a twilio.international configuration boolean that
   specifies whether to use twilio for international destinations.

2) If a nexmo configuration is specified, and Twilio fails to
   deliver, fall back and attempt delivery again with nexmo.
This commit is contained in:
Moxie Marlinspike
2013-12-10 16:35:25 -08:00
parent c194ce153d
commit 96435648d3
11 changed files with 239 additions and 129 deletions

View File

@@ -30,10 +30,7 @@ import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;
import org.whispersystems.textsecuregcm.sms.SenderFactory.VoxSender;
import org.whispersystems.textsecuregcm.sms.SenderFactory.SmsSender;
public class NexmoSmsSender implements SmsSender, VoxSender {
public class NexmoSmsSender {
private final Meter smsMeter = Metrics.newMeter(NexmoSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
private final Meter voxMeter = Metrics.newMeter(NexmoSmsSender.class, "vox", "delivered", TimeUnit.MINUTES);
@@ -55,10 +52,9 @@ public class NexmoSmsSender implements SmsSender, VoxSender {
this.number = config.getNumber();
}
@Override
public void deliverSmsVerification(String destination, String verificationCode) throws IOException {
URL url = new URL(String.format(NEXMO_SMS_URL, apiKey, apiSecret, number, destination,
URLEncoder.encode(SmsSender.VERIFICATION_TEXT + verificationCode, "UTF-8")));
URLEncoder.encode(SmsSender.SMS_VERIFICATION_TEXT + verificationCode, "UTF-8")));
URLConnection connection = url.openConnection();
connection.setDoInput(true);
@@ -70,10 +66,9 @@ public class NexmoSmsSender implements SmsSender, VoxSender {
smsMeter.mark();
}
@Override
public void deliverVoxVerification(String destination, String message) throws IOException {
URL url = new URL(String.format(NEXMO_VOX_URL, apiKey, apiSecret, destination,
URLEncoder.encode(VoxSender.VERIFICATION_TEXT + message, "UTF-8")));
URLEncoder.encode(SmsSender.VOX_VERIFICATION_TEXT + message, "UTF-8")));
URLConnection connection = url.openConnection();
connection.setDoInput(true);

View File

@@ -1,70 +0,0 @@
/**
* Copyright (C) 2013 Open WhisperSystems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.whispersystems.textsecuregcm.sms;
import com.google.common.base.Optional;
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
import java.io.IOException;
public class SenderFactory {
private final TwilioSmsSender twilioSender;
private final Optional<NexmoSmsSender> nexmoSender;
public SenderFactory(TwilioConfiguration twilioConfig, NexmoConfiguration nexmoConfig) {
this.twilioSender = new TwilioSmsSender(twilioConfig);
if (nexmoConfig != null) {
this.nexmoSender = Optional.of(new NexmoSmsSender(nexmoConfig));
} else {
this.nexmoSender = Optional.absent();
}
}
public SmsSender getSmsSender(String number) {
if (nexmoSender.isPresent() && !isTwilioDestination(number)) {
return nexmoSender.get();
} else {
return twilioSender;
}
}
public VoxSender getVoxSender(String number) {
if (nexmoSender.isPresent()) {
return nexmoSender.get();
} else {
return twilioSender;
}
}
private boolean isTwilioDestination(String number) {
return number.length() == 12 && number.startsWith("+1");
}
public interface SmsSender {
public static final String VERIFICATION_TEXT = "Your TextSecure verification code: ";
public void deliverSmsVerification(String destination, String verificationCode) throws IOException;
}
public interface VoxSender {
public static final String VERIFICATION_TEXT = "Your TextSecure verification code is: ";
public void deliverVoxVerification(String destination, String verificationCode) throws IOException;
}
}

View File

@@ -0,0 +1,84 @@
/**
* Copyright (C) 2013 Open WhisperSystems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.whispersystems.textsecuregcm.sms;
import com.google.common.base.Optional;
import com.twilio.sdk.TwilioRestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class SmsSender {
static final String SMS_VERIFICATION_TEXT = "Your TextSecure verification code: ";
static final String VOX_VERIFICATION_TEXT = "Your TextSecure verification code is: ";
private final Logger logger = LoggerFactory.getLogger(SmsSender.class);
private final TwilioSmsSender twilioSender;
private final Optional<NexmoSmsSender> nexmoSender;
private final boolean isTwilioInternational;
public SmsSender(TwilioSmsSender twilioSender,
Optional<NexmoSmsSender> nexmoSender,
boolean isTwilioInternational)
{
this.isTwilioInternational = isTwilioInternational;
this.twilioSender = twilioSender;
this.nexmoSender = nexmoSender;
}
public void deliverSmsVerification(String destination, String verificationCode)
throws IOException
{
if (!isTwilioDestination(destination) && nexmoSender.isPresent()) {
nexmoSender.get().deliverSmsVerification(destination, verificationCode);
} else {
try {
twilioSender.deliverSmsVerification(destination, verificationCode);
} catch (TwilioRestException e) {
logger.info("Twilio SMS Fallback", e);
if (nexmoSender.isPresent()) {
nexmoSender.get().deliverSmsVerification(destination, verificationCode);
}
}
}
}
public void deliverVoxVerification(String destination, String verificationCode)
throws IOException
{
if (!isTwilioDestination(destination) && nexmoSender.isPresent()) {
nexmoSender.get().deliverVoxVerification(destination, verificationCode);
} else {
try {
twilioSender.deliverVoxVerification(destination, verificationCode);
} catch (TwilioRestException e) {
logger.info("Twilio Vox Fallback", e);
if (nexmoSender.isPresent()) {
nexmoSender.get().deliverVoxVerification(destination, verificationCode);
}
}
}
}
private boolean isTwilioDestination(String number) {
return isTwilioInternational || number.length() == 12 && number.startsWith("+1");
}
}

View File

@@ -33,11 +33,11 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class TwilioSmsSender implements SenderFactory.SmsSender, SenderFactory.VoxSender {
public class TwilioSmsSender {
public static final String SAY_TWIML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Response>\n" +
" <Say voice=\"woman\" language=\"en\">%s</Say>\n" +
" <Say voice=\"woman\" language=\"en\">" + SmsSender.VOX_VERIFICATION_TEXT + "%s</Say>\n" +
"</Response>";
private final Meter smsMeter = Metrics.newMeter(TwilioSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
@@ -55,37 +55,39 @@ public class TwilioSmsSender implements SenderFactory.SmsSender, SenderFactory.V
this.localDomain = config.getLocalDomain();
}
@Override
public void deliverSmsVerification(String destination, String verificationCode)
throws IOException
throws IOException, TwilioRestException
{
TwilioRestClient client = new TwilioRestClient(accountId, accountToken);
MessageFactory messageFactory = client.getAccount().getMessageFactory();
List<NameValuePair> messageParams = new LinkedList<>();
messageParams.add(new BasicNameValuePair("To", destination));
messageParams.add(new BasicNameValuePair("From", number));
messageParams.add(new BasicNameValuePair("Body", SmsSender.SMS_VERIFICATION_TEXT + verificationCode));
try {
TwilioRestClient client = new TwilioRestClient(accountId, accountToken);
MessageFactory messageFactory = client.getAccount().getMessageFactory();
List<NameValuePair> messageParams = new LinkedList<>();
messageParams.add(new BasicNameValuePair("To", destination));
messageParams.add(new BasicNameValuePair("From", number));
messageParams.add(new BasicNameValuePair("Body", SenderFactory.SmsSender.VERIFICATION_TEXT + verificationCode));
messageFactory.create(messageParams);
} catch (TwilioRestException e) {
throw new IOException(e);
} catch (RuntimeException damnYouTwilio) {
throw new IOException(damnYouTwilio);
}
smsMeter.mark();
}
@Override
public void deliverVoxVerification(String destination, String verificationCode) throws IOException {
public void deliverVoxVerification(String destination, String verificationCode)
throws IOException, TwilioRestException
{
TwilioRestClient client = new TwilioRestClient(accountId, accountToken);
CallFactory callFactory = client.getAccount().getCallFactory();
Map<String, String> callParams = new HashMap<>();
callParams.put("To", destination);
callParams.put("From", number);
callParams.put("Url", "https://" + localDomain + "/v1/accounts/voice/twiml/" + verificationCode);
try {
TwilioRestClient client = new TwilioRestClient(accountId, accountToken);
CallFactory callFactory = client.getAccount().getCallFactory();
Map<String, String> callParams = new HashMap<>();
callParams.put("To", destination);
callParams.put("From", number);
callParams.put("Url", "https://" + localDomain + "/v1/accounts/voice/twiml/" + verificationCode);
callFactory.create(callParams);
} catch (TwilioRestException e) {
throw new IOException(e);
} catch (RuntimeException damnYouTwilio) {
throw new IOException(damnYouTwilio);
}
voxMeter.mark();