Handle 428 rate limiting.

This commit is contained in:
Greyson Parrelli
2021-05-05 12:49:18 -04:00
parent 02d060ca0a
commit 31e1c6f7aa
60 changed files with 1235 additions and 57 deletions

View File

@@ -0,0 +1,55 @@
package org.thoughtcrime.securesms.keyvalue;
import androidx.annotation.NonNull;
import org.greenrobot.eventbus.EventBus;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.ratelimit.RecaptchaRequiredEvent;
import java.util.Collections;
import java.util.List;
public final class RateLimitValues extends SignalStoreValues {
private static final String TAG = Log.tag(RateLimitValues.class);
private static final String KEY_NEEDS_RECAPTCHA = "ratelimit.needs_recaptcha";
private static final String KEY_CHALLENGE = "ratelimit.token";
RateLimitValues(@NonNull KeyValueStore store) {
super(store);
}
@Override
void onFirstEverAppLaunch() {
}
@Override
@NonNull List<String> getKeysToIncludeInBackup() {
return Collections.emptyList();
}
/**
* @param challenge The token associated with the rate limit response.
*/
public void markNeedsRecaptcha(@NonNull String challenge) {
Log.i(TAG, "markNeedsRecaptcha()");
putBoolean(KEY_NEEDS_RECAPTCHA, true);
putString(KEY_CHALLENGE, challenge);
EventBus.getDefault().post(new RecaptchaRequiredEvent());
}
public void onProofAccepted() {
Log.i(TAG, "onProofAccepted()", new Throwable());
putBoolean(KEY_NEEDS_RECAPTCHA, false);
remove(KEY_CHALLENGE);
}
public boolean needsRecaptcha() {
return getBoolean(KEY_NEEDS_RECAPTCHA, false);
}
public @NonNull String getChallenge() {
return getString(KEY_CHALLENGE, "");
}
}

View File

@@ -35,6 +35,7 @@ public final class SignalStore {
private final WallpaperValues wallpaperValues;
private final PaymentsValues paymentsValues;
private final ProxyValues proxyValues;
private final RateLimitValues rateLimitValues;
private SignalStore() {
this.store = new KeyValueStore(ApplicationDependencies.getApplication());
@@ -55,6 +56,7 @@ public final class SignalStore {
this.wallpaperValues = new WallpaperValues(store);
this.paymentsValues = new PaymentsValues(store);
this.proxyValues = new ProxyValues(store);
this.rateLimitValues = new RateLimitValues(store);
}
public static void onFirstEverAppLaunch() {
@@ -75,6 +77,7 @@ public final class SignalStore {
wallpaper().onFirstEverAppLaunch();
paymentsValues().onFirstEverAppLaunch();
proxy().onFirstEverAppLaunch();
rateLimit().onFirstEverAppLaunch();
}
public static List<String> getKeysToIncludeInBackup() {
@@ -96,6 +99,7 @@ public final class SignalStore {
keys.addAll(wallpaper().getKeysToIncludeInBackup());
keys.addAll(paymentsValues().getKeysToIncludeInBackup());
keys.addAll(proxy().getKeysToIncludeInBackup());
keys.addAll(rateLimit().getKeysToIncludeInBackup());
return keys;
}
@@ -176,6 +180,10 @@ public final class SignalStore {
return INSTANCE.proxyValues;
}
public static @NonNull RateLimitValues rateLimit() {
return INSTANCE.rateLimitValues;
}
public static @NonNull GroupsV2AuthorizationSignalStoreCache groupsV2AuthorizationCache() {
return new GroupsV2AuthorizationSignalStoreCache(getStore());
}

View File

@@ -67,4 +67,8 @@ abstract class SignalStoreValues {
void putString(@NonNull String key, String value) {
store.beginWrite().putString(key, value).apply();
}
void remove(@NonNull String key) {
store.beginWrite().remove(key);
}
}