mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-31 00:26:37 +00:00
Prevent the creation of 'weak' PINs.
Simple checks to prevent the same number, or sequentially increasing/decreasing PINs. e.g. 1111, 1234, 54321, etc.
This commit is contained in:
committed by
Greyson Parrelli
parent
b7296a4fe3
commit
87eab27996
@@ -1,15 +1,20 @@
|
||||
package org.thoughtcrime.securesms.lock.v2;
|
||||
|
||||
import android.view.View;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.TranslateAnimation;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.PluralsRes;
|
||||
import androidx.autofill.HintConstants;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
import androidx.navigation.Navigation;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.SpanUtil;
|
||||
|
||||
public class CreateKbsPinFragment extends BaseKbsPinFragment<CreateKbsPinViewModel> {
|
||||
|
||||
@@ -47,6 +52,15 @@ public class CreateKbsPinFragment extends BaseKbsPinFragment<CreateKbsPinViewMod
|
||||
CreateKbsPinFragmentArgs args = CreateKbsPinFragmentArgs.fromBundle(requireArguments());
|
||||
|
||||
viewModel.getNavigationEvents().observe(getViewLifecycleOwner(), e -> onConfirmPin(e.getUserEntry(), e.getKeyboard(), args.getIsPinChange()));
|
||||
viewModel.getErrorEvents().observe(getViewLifecycleOwner(), e -> {
|
||||
if (e == CreateKbsPinViewModel.PinErrorEvent.WEAK_PIN) {
|
||||
getLabel().setText(SpanUtil.color(ContextCompat.getColor(requireContext(), R.color.red),
|
||||
getString(R.string.CreateKbsPinFragment__choose_a_stronger_pin)));
|
||||
shake(getInput(), () -> getInput().getText().clear());
|
||||
} else {
|
||||
throw new AssertionError("Unexpected PIN error!");
|
||||
}
|
||||
});
|
||||
viewModel.getKeyboard().observe(getViewLifecycleOwner(), k -> {
|
||||
getLabel().setText(getLabelText(k));
|
||||
getInput().getText().clear();
|
||||
@@ -76,4 +90,23 @@ public class CreateKbsPinFragment extends BaseKbsPinFragment<CreateKbsPinViewMod
|
||||
private String getPinLengthRestrictionText(@PluralsRes int plurals) {
|
||||
return requireContext().getResources().getQuantityString(plurals, KbsConstants.MINIMUM_PIN_LENGTH, KbsConstants.MINIMUM_PIN_LENGTH);
|
||||
}
|
||||
|
||||
private static void shake(@NonNull EditText view, @NonNull Runnable afterwards) {
|
||||
TranslateAnimation shake = new TranslateAnimation(0, 30, 0, 0);
|
||||
shake.setDuration(50);
|
||||
shake.setRepeatCount(7);
|
||||
shake.setAnimationListener(new Animation.AnimationListener() {
|
||||
@Override
|
||||
public void onAnimationStart(Animation animation) {}
|
||||
|
||||
@Override
|
||||
public void onAnimationEnd(Animation animation) {
|
||||
afterwards.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationRepeat(Animation animation) {}
|
||||
});
|
||||
view.startAnimation(shake);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,22 @@ package org.thoughtcrime.securesms.lock.v2;
|
||||
|
||||
import androidx.annotation.MainThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.core.util.Preconditions;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.SingleLiveEvent;
|
||||
import org.whispersystems.signalservice.internal.registrationpin.PinValidityChecker;
|
||||
|
||||
public final class CreateKbsPinViewModel extends ViewModel implements BaseKbsPinViewModel {
|
||||
|
||||
private final MutableLiveData<KbsPin> userEntry = new MutableLiveData<>(KbsPin.EMPTY);
|
||||
private final MutableLiveData<PinKeyboardType> keyboard = new MutableLiveData<>(PinKeyboardType.NUMERIC);
|
||||
private final SingleLiveEvent<NavigationEvent> events = new SingleLiveEvent<>();
|
||||
private final SingleLiveEvent<PinErrorEvent> errors = new SingleLiveEvent<>();
|
||||
|
||||
@Override
|
||||
public LiveData<KbsPin> getUserEntry() {
|
||||
@@ -27,6 +31,8 @@ public final class CreateKbsPinViewModel extends ViewModel implements BaseKbsPin
|
||||
|
||||
LiveData<NavigationEvent> getNavigationEvents() { return events; }
|
||||
|
||||
LiveData<PinErrorEvent> getErrorEvents() { return errors; }
|
||||
|
||||
@Override
|
||||
@MainThread
|
||||
public void setUserEntry(String userEntry) {
|
||||
@@ -42,8 +48,14 @@ public final class CreateKbsPinViewModel extends ViewModel implements BaseKbsPin
|
||||
@Override
|
||||
@MainThread
|
||||
public void confirm() {
|
||||
events.setValue(new NavigationEvent(Preconditions.checkNotNull(this.getUserEntry().getValue()),
|
||||
Preconditions.checkNotNull(this.getKeyboard().getValue())));
|
||||
KbsPin pin = Preconditions.checkNotNull(this.getUserEntry().getValue());
|
||||
PinKeyboardType keyboard = Preconditions.checkNotNull(this.getKeyboard().getValue());
|
||||
|
||||
if (PinValidityChecker.valid(pin.toString())) {
|
||||
events.setValue(new NavigationEvent(pin, keyboard));
|
||||
} else {
|
||||
errors.setValue(PinErrorEvent.WEAK_PIN);
|
||||
}
|
||||
}
|
||||
|
||||
static final class NavigationEvent {
|
||||
@@ -63,4 +75,8 @@ public final class CreateKbsPinViewModel extends ViewModel implements BaseKbsPin
|
||||
return keyboard;
|
||||
}
|
||||
}
|
||||
|
||||
enum PinErrorEvent {
|
||||
WEAK_PIN
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user