Skip to content

Commit

Permalink
Allow backup passphrase verification.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal authored and greyson-signal committed Feb 3, 2020
1 parent ed0c4b8 commit 1dd2a4e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.SwitchPreferenceCompat;
import org.thoughtcrime.securesms.registration.fragments.RestoreBackupFragment;
import org.thoughtcrime.securesms.service.LocalBackupListener;
import org.thoughtcrime.securesms.util.BackupUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.text.AfterTextChanged;

public class BackupDialog {

Expand Down Expand Up @@ -83,4 +89,35 @@ public static void showDisableBackupDialog(@NonNull Context context, @NonNull Sw
.create()
.show();
}

public static void showVerifyBackupPassphraseDialog(@NonNull Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.enter_backup_passphrase_dialog, null);
EditText prompt = view.findViewById(R.id.restore_passphrase_input);
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle(R.string.BackupDialog_enter_backup_passphrase_to_verify)
.setView(view)
.setPositiveButton(R.string.BackupDialog_verify, null)
.setNegativeButton(android.R.string.cancel, null)
.show();

Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);

RestoreBackupFragment.PassphraseAsYouTypeFormatter formatter = new RestoreBackupFragment.PassphraseAsYouTypeFormatter();

prompt.addTextChangedListener(new AfterTextChanged(editable -> {
formatter.afterTextChanged(editable);
positiveButton.setEnabled(editable.length() == BackupUtil.PASSPHRASE_LENGTH);
}));

positiveButton.setOnClickListener(v -> {
String passphrase = prompt.getText().toString();
if (passphrase.equals(BackupPassphrase.get(context))) {
Toast.makeText(context, R.string.BackupDialog_you_successfully_entered_your_backup_passphrase, Toast.LENGTH_SHORT).show();
dialog.dismiss();
} else {
Toast.makeText(context, R.string.BackupDialog_passphrase_was_not_correct, Toast.LENGTH_SHORT).show();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms.preferences;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
Expand Down Expand Up @@ -52,6 +51,8 @@ public void onCreate(Bundle paramBundle) {
.setOnPreferenceClickListener(new BackupClickListener());
findPreference(TextSecurePreferences.BACKUP_NOW)
.setOnPreferenceClickListener(new BackupCreateListener());
findPreference(TextSecurePreferences.BACKUP_PASSPHRASE_VERIFY)
.setOnPreferenceClickListener(new BackupVerifyListener());

initializeListSummary((ListPreference) findPreference(TextSecurePreferences.MESSAGE_BODY_TEXT_SIZE_PREF));

Expand Down Expand Up @@ -145,7 +146,6 @@ public boolean onPreferenceClick(Preference preference) {
}

private class BackupCreateListener implements Preference.OnPreferenceClickListener {
@SuppressLint("StaticFieldLeak")
@Override
public boolean onPreferenceClick(Preference preference) {
Permissions.with(ChatsPreferenceFragment.this)
Expand All @@ -162,6 +162,14 @@ public boolean onPreferenceClick(Preference preference) {
}
}

private class BackupVerifyListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) {
BackupDialog.showVerifyBackupPassphraseDialog(requireContext());
return true;
}
}

private class MediaDownloadChangeListener implements Preference.OnPreferenceChangeListener {
@SuppressWarnings("unchecked")
@Override public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private enum BackupImportResult {
FAILURE_UNKNOWN
}

private static class PassphraseAsYouTypeFormatter implements TextWatcher {
public static class PassphraseAsYouTypeFormatter implements TextWatcher {

private static final int GROUP_SIZE = 5;

Expand All @@ -292,23 +292,23 @@ public void afterTextChanged(Editable editable) {
addSpans(editable);
}

private void removeSpans(Editable editable) {
private static void removeSpans(Editable editable) {
SpaceSpan[] paddingSpans = editable.getSpans(0, editable.length(), SpaceSpan.class);

for (SpaceSpan span : paddingSpans) {
editable.removeSpan(span);
}
}

private void addSpans(Editable editable) {
private static void addSpans(Editable editable) {
final int length = editable.length();

for (int i = GROUP_SIZE; i < length; i += GROUP_SIZE) {
editable.setSpan(new SpaceSpan(), i - 1, i, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

if (editable.length() > 30) {
editable.delete(30, editable.length());
if (editable.length() > BackupUtil.PASSPHRASE_LENGTH) {
editable.delete(BackupUtil.PASSPHRASE_LENGTH, editable.length());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class BackupUtil {

private static final String TAG = BackupUtil.class.getSimpleName();

public static final int PASSPHRASE_LENGTH = 30;

public static @NonNull String getLastBackupTime(@NonNull Context context, @NonNull Locale locale) {
try {
BackupInfo backup = getLatestBackup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public class TextSecurePreferences {
private static final String ENCRYPTED_BACKUP_PASSPHRASE = "pref_encrypted_backup_passphrase";
private static final String BACKUP_TIME = "pref_backup_next_time";
public static final String BACKUP_NOW = "pref_backup_create";
public static final String BACKUP_PASSPHRASE_VERIFY = "pref_backup_passphrase_verify";

public static final String SCREEN_LOCK = "pref_android_screen_lock";
public static final String SCREEN_LOCK_TIMEOUT = "pref_android_screen_lock_timeout";
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,8 @@
<string name="preferences_chats__chat_backups">Chat backups</string>
<string name="preferences_chats__backup_chats_to_external_storage">Backup chats to external storage</string>
<string name="preferences_chats__create_backup">Create backup</string>
<string name="preferences_chats__verify_backup_passphrase">Verify backup passphrase</string>
<string name="preferences_chats__test_your_backup_passphrase_and_verify_that_it_matches">Test your backup passphrase and verify that it matches</string>
<string name="RegistrationActivity_enter_backup_passphrase">Enter backup passphrase</string>
<string name="RegistrationActivity_restore">Restore</string>
<string name="RegistrationActivity_backup_failure_downgrade">Cannot import backups from newer versions of Signal</string>
Expand All @@ -1795,6 +1797,10 @@
<string name="BackupDialog_disable_and_delete_all_local_backups">Disable and delete all local backups?</string>
<string name="BackupDialog_delete_backups_statement">Delete backups</string>
<string name="BackupDialog_copied_to_clipboard">Copied to clipboard</string>
<string name="BackupDialog_enter_backup_passphrase_to_verify">Enter your backup passphrase to verify</string>
<string name="BackupDialog_verify">Verify</string>
<string name="BackupDialog_you_successfully_entered_your_backup_passphrase">You successfully entered your backup passphrase</string>
<string name="BackupDialog_passphrase_was_not_correct">Passphrase was not correct</string>
<string name="ChatsPreferenceFragment_signal_requires_external_storage_permission_in_order_to_create_backups">Signal requires external storage permission in order to create backups, but it has been permanently denied. Please continue to app settings, select \"Permissions\" and enable \"Storage\".</string>
<string name="ChatsPreferenceFragment_last_backup_s">Last backup: %s</string>
<string name="ChatsPreferenceFragment_in_progress">In progress</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences_chats.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
android:dependency="pref_backup_enabled"
tools:summary="Last backup: 3 days ago"/>

<androidx.preference.Preference
android:key="pref_backup_passphrase_verify"
android:title="@string/preferences_chats__verify_backup_passphrase"
android:persistent="false"
android:dependency="pref_backup_enabled"
android:summary="@string/preferences_chats__test_your_backup_passphrase_and_verify_that_it_matches"/>

</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 1dd2a4e

Please sign in to comment.