Skip to content

Commit

Permalink
Light refactor of SignalStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal authored and alan-signal committed Jun 12, 2020
1 parent e201957 commit 1ce8ac2
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.io.IOException;
import java.security.SecureRandom;

public final class KbsValues {
public final class KbsValues extends SignalStoreValues {

public static final String V2_LOCK_ENABLED = "kbs.v2_lock_enabled";
private static final String MASTER_KEY = "kbs.registration_lock_master_key";
Expand All @@ -21,10 +21,12 @@ public final class KbsValues {
private static final String LOCK_LOCAL_PIN_HASH = "kbs.registration_lock_local_pin_hash";
private static final String LAST_CREATE_FAILED_TIMESTAMP = "kbs.last_create_failed_timestamp";

private final KeyValueStore store;

KbsValues(KeyValueStore store) {
this.store = store;
super(store);
}

@Override
void onFirstEverAppLaunch() {
}

/**
Expand All @@ -33,13 +35,13 @@ public final class KbsValues {
* Should only be called by {@link org.thoughtcrime.securesms.pin.PinState}
*/
public void clearRegistrationLockAndPin() {
store.beginWrite()
.remove(V2_LOCK_ENABLED)
.remove(TOKEN_RESPONSE)
.remove(LOCK_LOCAL_PIN_HASH)
.remove(PIN)
.remove(LAST_CREATE_FAILED_TIMESTAMP)
.commit();
getStore().beginWrite()
.remove(V2_LOCK_ENABLED)
.remove(TOKEN_RESPONSE)
.remove(LOCK_LOCAL_PIN_HASH)
.remove(PIN)
.remove(LAST_CREATE_FAILED_TIMESTAMP)
.commit();
}

/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}. */
Expand All @@ -52,43 +54,43 @@ public synchronized void setKbsMasterKey(@NonNull KbsPinData pinData, @NonNull S
throw new AssertionError(e);
}

store.beginWrite()
.putString(TOKEN_RESPONSE, tokenResponse)
.putBlob(MASTER_KEY, masterKey.serialize())
.putString(LOCK_LOCAL_PIN_HASH, PinHashing.localPinHash(pin))
.putString(PIN, pin)
.putLong(LAST_CREATE_FAILED_TIMESTAMP, -1)
.commit();
getStore().beginWrite()
.putString(TOKEN_RESPONSE, tokenResponse)
.putBlob(MASTER_KEY, masterKey.serialize())
.putString(LOCK_LOCAL_PIN_HASH, PinHashing.localPinHash(pin))
.putString(PIN, pin)
.putLong(LAST_CREATE_FAILED_TIMESTAMP, -1)
.commit();
}

synchronized void setPinIfNotPresent(@NonNull String pin) {
if (store.getString(PIN, null) == null) {
store.beginWrite().putString(PIN, pin).commit();
if (getStore().getString(PIN, null) == null) {
getStore().beginWrite().putString(PIN, pin).commit();
}
}

/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}. */
public synchronized void setV2RegistrationLockEnabled(boolean enabled) {
store.beginWrite().putBoolean(V2_LOCK_ENABLED, enabled).apply();
putBoolean(V2_LOCK_ENABLED, enabled);
}

/**
* Whether or not registration lock V2 is enabled.
*/
public synchronized boolean isV2RegistrationLockEnabled() {
return store.getBoolean(V2_LOCK_ENABLED, false);
return getBoolean(V2_LOCK_ENABLED, false);
}

/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}. */
public synchronized void onPinCreateFailure() {
store.beginWrite().putLong(LAST_CREATE_FAILED_TIMESTAMP, System.currentTimeMillis()).apply();
putLong(LAST_CREATE_FAILED_TIMESTAMP, System.currentTimeMillis());
}

/**
* Whether or not the last time the user attempted to create a PIN, it failed.
*/
public synchronized boolean lastPinCreateFailed() {
return store.getLong(LAST_CREATE_FAILED_TIMESTAMP, -1) > 0;
return getLong(LAST_CREATE_FAILED_TIMESTAMP, -1) > 0;
}

/**
Expand All @@ -98,13 +100,13 @@ public synchronized boolean lastPinCreateFailed() {
* If you only want a key when it's backed up, use {@link #getPinBackedMasterKey()}.
*/
public synchronized @NonNull MasterKey getOrCreateMasterKey() {
byte[] blob = store.getBlob(MASTER_KEY, null);
byte[] blob = getStore().getBlob(MASTER_KEY, null);

if (blob == null) {
store.beginWrite()
.putBlob(MASTER_KEY, MasterKey.createNew(new SecureRandom()).serialize())
.commit();
blob = store.getBlob(MASTER_KEY, null);
getStore().beginWrite()
.putBlob(MASTER_KEY, MasterKey.createNew(new SecureRandom()).serialize())
.commit();
blob = getBlob(MASTER_KEY, null);
}

return new MasterKey(blob);
Expand All @@ -119,7 +121,7 @@ public synchronized boolean lastPinCreateFailed() {
}

private synchronized @Nullable MasterKey getMasterKey() {
byte[] blob = store.getBlob(MASTER_KEY, null);
byte[] blob = getBlob(MASTER_KEY, null);
return blob != null ? new MasterKey(blob) : null;
}

Expand All @@ -133,15 +135,15 @@ public synchronized boolean lastPinCreateFailed() {
}

public synchronized @Nullable String getLocalPinHash() {
return store.getString(LOCK_LOCAL_PIN_HASH, null);
return getString(LOCK_LOCAL_PIN_HASH, null);
}

public synchronized boolean hasPin() {
return getLocalPinHash() != null;
}

public synchronized @Nullable TokenResponse getRegistrationLockTokenResponse() {
String token = store.getString(TOKEN_RESPONSE, null);
String token = getStore().getString(TOKEN_RESPONSE, null);

if (token == null) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Specifically handles just the UI/UX state around PINs. For actual keys, see {@link KbsValues}.
*/
public final class PinValues {
public final class PinValues extends SignalStoreValues {

private static final String TAG = Log.tag(PinValues.class);

Expand All @@ -22,20 +22,22 @@ public final class PinValues {
private static final String PIN_STATE = "pin.pin_state";
public static final String PIN_REMINDERS_ENABLED = "pin.pin_reminders_enabled";

private final KeyValueStore store;

PinValues(KeyValueStore store) {
this.store = store;
super(store);
}

@Override
void onFirstEverAppLaunch() {
}

public void onEntrySuccess(@NonNull String pin) {
long nextInterval = SignalPinReminders.getNextInterval(getCurrentInterval());
Log.i(TAG, "onEntrySuccess() nextInterval: " + nextInterval);

store.beginWrite()
.putLong(LAST_SUCCESSFUL_ENTRY, System.currentTimeMillis())
.putLong(NEXT_INTERVAL, nextInterval)
.apply();
getStore().beginWrite()
.putLong(LAST_SUCCESSFUL_ENTRY, System.currentTimeMillis())
.putLong(NEXT_INTERVAL, nextInterval)
.apply();

SignalStore.kbsValues().setPinIfNotPresent(pin);
}
Expand All @@ -44,10 +46,10 @@ public void onEntrySuccessWithWrongGuess(@NonNull String pin) {
long nextInterval = SignalPinReminders.getPreviousInterval(getCurrentInterval());
Log.i(TAG, "onEntrySuccessWithWrongGuess() nextInterval: " + nextInterval);

store.beginWrite()
.putLong(LAST_SUCCESSFUL_ENTRY, System.currentTimeMillis())
.putLong(NEXT_INTERVAL, nextInterval)
.apply();
getStore().beginWrite()
.putLong(LAST_SUCCESSFUL_ENTRY, System.currentTimeMillis())
.putLong(NEXT_INTERVAL, nextInterval)
.apply();

SignalStore.kbsValues().setPinIfNotPresent(pin);
}
Expand All @@ -56,61 +58,55 @@ public void onEntrySkipWithWrongGuess() {
long nextInterval = SignalPinReminders.getPreviousInterval(getCurrentInterval());
Log.i(TAG, "onEntrySkipWithWrongGuess() nextInterval: " + nextInterval);

store.beginWrite()
.putLong(NEXT_INTERVAL, nextInterval)
.apply();
putLong(NEXT_INTERVAL, nextInterval);
}

public void resetPinReminders() {
long nextInterval = SignalPinReminders.INITIAL_INTERVAL;
Log.i(TAG, "resetPinReminders() nextInterval: " + nextInterval, new Throwable());

store.beginWrite()
.putLong(NEXT_INTERVAL, nextInterval)
.putLong(LAST_SUCCESSFUL_ENTRY, System.currentTimeMillis())
.apply();
getStore().beginWrite()
.putLong(NEXT_INTERVAL, nextInterval)
.putLong(LAST_SUCCESSFUL_ENTRY, System.currentTimeMillis())
.apply();
}

public long getCurrentInterval() {
return store.getLong(NEXT_INTERVAL, TextSecurePreferences.getRegistrationLockNextReminderInterval(ApplicationDependencies.getApplication()));
return getLong(NEXT_INTERVAL, TextSecurePreferences.getRegistrationLockNextReminderInterval(ApplicationDependencies.getApplication()));
}

public long getLastSuccessfulEntryTime() {
return store.getLong(LAST_SUCCESSFUL_ENTRY, TextSecurePreferences.getRegistrationLockLastReminderTime(ApplicationDependencies.getApplication()));
return getLong(LAST_SUCCESSFUL_ENTRY, TextSecurePreferences.getRegistrationLockLastReminderTime(ApplicationDependencies.getApplication()));
}

public void setKeyboardType(@NonNull PinKeyboardType keyboardType) {
store.beginWrite()
.putString(KEYBOARD_TYPE, keyboardType.getCode())
.commit();
putString(KEYBOARD_TYPE, keyboardType.getCode());
}

public void setPinRemindersEnabled(boolean enabled) {
store.beginWrite().putBoolean(PIN_REMINDERS_ENABLED, enabled).apply();
putBoolean(PIN_REMINDERS_ENABLED, enabled);
}

public boolean arePinRemindersEnabled() {
return store.getBoolean(PIN_REMINDERS_ENABLED, true);
return getBoolean(PIN_REMINDERS_ENABLED, true);
}

public @NonNull PinKeyboardType getKeyboardType() {
return PinKeyboardType.fromCode(store.getString(KEYBOARD_TYPE, null));
return PinKeyboardType.fromCode(getStore().getString(KEYBOARD_TYPE, null));
}

public void setNextReminderIntervalToAtMost(long maxInterval) {
if (store.getLong(NEXT_INTERVAL, 0) > maxInterval) {
store.beginWrite()
.putLong(NEXT_INTERVAL, maxInterval)
.apply();
if (getStore().getLong(NEXT_INTERVAL, 0) > maxInterval) {
putLong(NEXT_INTERVAL, maxInterval);
}
}

/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState} */
public void setPinState(@NonNull String pinState) {
store.beginWrite().putString(PIN_STATE, pinState).commit();
getStore().beginWrite().putString(PIN_STATE, pinState).commit();
}

public @Nullable String getPinState() {
return store.getString(PIN_STATE, null);
return getString(PIN_STATE, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,39 @@
import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;

public final class RegistrationValues {
public final class RegistrationValues extends SignalStoreValues {

private static final String REGISTRATION_COMPLETE = "registration.complete";
private static final String PIN_REQUIRED = "registration.pin_required";

private final KeyValueStore store;

RegistrationValues(@NonNull KeyValueStore store) {
this.store = store;
super(store);
}

public synchronized void onFirstEverAppLaunch() {
store.beginWrite()
.putBoolean(REGISTRATION_COMPLETE, false)
.putBoolean(PIN_REQUIRED, true)
.commit();
getStore().beginWrite()
.putBoolean(REGISTRATION_COMPLETE, false)
.putBoolean(PIN_REQUIRED, true)
.commit();
}

public synchronized void clearRegistrationComplete() {
onFirstEverAppLaunch();
}

public synchronized void setRegistrationComplete() {
store.beginWrite()
.putBoolean(REGISTRATION_COMPLETE, true)
.commit();
getStore().beginWrite()
.putBoolean(REGISTRATION_COMPLETE, true)
.commit();
}

@CheckResult
public synchronized boolean pinWasRequiredAtRegistration() {
return store.getBoolean(PIN_REQUIRED, false);
return getStore().getBoolean(PIN_REQUIRED, false);
}

@CheckResult
public synchronized boolean isRegistrationComplete() {
return store.getBoolean(REGISTRATION_COMPLETE, true);
return getStore().getBoolean(REGISTRATION_COMPLETE, true);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
package org.thoughtcrime.securesms.keyvalue;

import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.logging.Log;

public final class RemoteConfigValues {
public final class RemoteConfigValues extends SignalStoreValues {

private static final String TAG = Log.tag(RemoteConfigValues.class);

private static final String CURRENT_CONFIG = "remote_config";
private static final String PENDING_CONFIG = "pending_remote_config";
private static final String LAST_FETCH_TIME = "remote_config_last_fetch_time";

private final KeyValueStore store;
RemoteConfigValues(@NonNull KeyValueStore store) {
super(store);
}

RemoteConfigValues(KeyValueStore store) {
this.store = store;
@Override
void onFirstEverAppLaunch() {
}

public String getCurrentConfig() {
return store.getString(CURRENT_CONFIG, null);
return getString(CURRENT_CONFIG, null);
}

public void setCurrentConfig(String value) {
store.beginWrite().putString(CURRENT_CONFIG, value).apply();
putString(CURRENT_CONFIG, value);
}

public String getPendingConfig() {
return store.getString(PENDING_CONFIG, getCurrentConfig());
return getString(PENDING_CONFIG, getCurrentConfig());
}

public void setPendingConfig(String value) {
store.beginWrite().putString(PENDING_CONFIG, value).apply();
putString(PENDING_CONFIG, value);
}

public long getLastFetchTime() {
return store.getLong(LAST_FETCH_TIME, 0);
return getLong(LAST_FETCH_TIME, 0);
}

public void setLastFetchTime(long time) {
store.beginWrite().putLong(LAST_FETCH_TIME, time).apply();
putLong(LAST_FETCH_TIME, time);
}
}

0 comments on commit 1ce8ac2

Please sign in to comment.