Adds TLS to the push notification client (keystore_tusky_api is omitted).

This commit is contained in:
Vavassor 2017-05-22 02:05:37 -04:00
parent b396f2afc8
commit 6ee6157b7f
3 changed files with 11 additions and 12 deletions

View file

@ -163,7 +163,7 @@ public class BaseActivity extends AppCompatActivity {
protected void createTuskyApi() { protected void createTuskyApi() {
Retrofit retrofit = new Retrofit.Builder() Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://" + getString(R.string.tusky_api_domain) + ":8080") .baseUrl("https://" + getString(R.string.tusky_api_domain) + ":8080")
.client(OkHttpUtils.getCompatibleClient()) .client(OkHttpUtils.getCompatibleClient())
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.build(); .build();
@ -172,9 +172,8 @@ public class BaseActivity extends AppCompatActivity {
} }
protected void createPushNotificationClient() { protected void createPushNotificationClient() {
// TODO: Switch to ssl:// when TLS support is added.
pushNotificationClient = new PushNotificationClient(getApplicationContext(), pushNotificationClient = new PushNotificationClient(getApplicationContext(),
"tcp://" + getString(R.string.tusky_api_domain) + ":1883"); "ssl://" + getString(R.string.tusky_api_domain) + ":8883");
} }
protected void redirectIfNotLoggedIn() { protected void redirectIfNotLoggedIn() {
@ -214,15 +213,15 @@ public class BaseActivity extends AppCompatActivity {
retrofit2.Response<ResponseBody> response) { retrofit2.Response<ResponseBody> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
pushNotificationClient.subscribeToTopic(getPushNotificationTopic()); pushNotificationClient.subscribeToTopic(getPushNotificationTopic());
pushNotificationClient.connect(); pushNotificationClient.connect(BaseActivity.this);
} else { } else {
onEnablePushNotificationsFailure(); onEnablePushNotificationsFailure(response.message());
} }
} }
@Override @Override
public void onFailure(Call<ResponseBody> call, Throwable t) { public void onFailure(Call<ResponseBody> call, Throwable t) {
onEnablePushNotificationsFailure(); onEnablePushNotificationsFailure(t.getMessage());
} }
}; };
String deviceToken = pushNotificationClient.getDeviceToken(); String deviceToken = pushNotificationClient.getDeviceToken();
@ -231,8 +230,8 @@ public class BaseActivity extends AppCompatActivity {
.enqueue(callback); .enqueue(callback);
} }
private void onEnablePushNotificationsFailure() { private void onEnablePushNotificationsFailure(String message) {
Log.e(TAG, "Enabling push notifications failed."); Log.e(TAG, "Enabling push notifications failed. " + message);
} }
protected void disablePushNotifications() { protected void disablePushNotifications() {

View file

@ -7,6 +7,7 @@ import android.text.Spanned;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.entity.Notification; import com.keylesspalace.tusky.entity.Notification;
import com.keylesspalace.tusky.json.SpannedTypeAdapter; import com.keylesspalace.tusky.json.SpannedTypeAdapter;
import com.keylesspalace.tusky.json.StringWithEmoji; import com.keylesspalace.tusky.json.StringWithEmoji;
@ -23,6 +24,7 @@ import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttMessage;
import java.io.InputStream;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
@ -104,12 +106,11 @@ public class PushNotificationClient {
} }
/** Connect to the MQTT broker. */ /** Connect to the MQTT broker. */
public void connect() { public void connect(Context context) {
MqttConnectOptions options = new MqttConnectOptions(); MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true); options.setAutomaticReconnect(true);
options.setCleanSession(false); options.setCleanSession(false);
try { try {
/*
String password = context.getString(R.string.tusky_api_keystore_password); String password = context.getString(R.string.tusky_api_keystore_password);
InputStream keystore = context.getResources().openRawResource(R.raw.keystore_tusky_api); InputStream keystore = context.getResources().openRawResource(R.raw.keystore_tusky_api);
try { try {
@ -117,7 +118,6 @@ public class PushNotificationClient {
} finally { } finally {
IOUtils.closeQuietly(keystore); IOUtils.closeQuietly(keystore);
} }
*/
mqttAndroidClient.connect(options).setActionCallback(new IMqttActionListener() { mqttAndroidClient.connect(options).setActionCallback(new IMqttActionListener() {
@Override @Override
public void onSuccess(IMqttToken asyncActionToken) { public void onSuccess(IMqttToken asyncActionToken) {

View file

@ -2,7 +2,7 @@
<resources> <resources>
<string name="app_name" translatable="false">Tusky</string> <string name="app_name" translatable="false">Tusky</string>
<string name="app_website" translatable="false">https://tusky.keylesspalace.com</string> <string name="app_website" translatable="false">https://tusky.keylesspalace.com</string>
<string name="tusky_api_domain" translatable="false">tuskyapi.keylesspalace.com</string> <string name="tusky_api_domain" translatable="false">apitusky.keylesspalace.com</string>
<string name="tusky_api_keystore_password" translatable="false">your_password_here</string> <string name="tusky_api_keystore_password" translatable="false">your_password_here</string>
<string name="oauth_scheme" translatable="false">oauth2redirect</string> <string name="oauth_scheme" translatable="false">oauth2redirect</string>