Skip to content

Commit

Permalink
Discard the Redis-backed "used token" system
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-signal committed Oct 7, 2024
1 parent 961d6d0 commit 63e4556
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public void run(WhisperServerConfiguration config, Environment environment) thro
ClientPublicKeysManager clientPublicKeysManager =
new ClientPublicKeysManager(clientPublicKeys, accountLockManager, accountLockExecutor);
AccountsManager accountsManager = new AccountsManager(accounts, phoneNumberIdentifiers, cacheCluster,
rateLimitersCluster, accountLockManager, keysManager, messagesManager, profilesManager,
accountLockManager, keysManager, messagesManager, profilesManager,
secureStorageClient, secureValueRecovery2Client,
clientPresenceManager,
registrationRecoveryPasswordsManager, clientPublicKeysManager, accountLockExecutor, clientPresenceExecutor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.lettuce.core.RedisException;
import io.lettuce.core.SetArgs;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
Expand Down Expand Up @@ -109,7 +108,6 @@ public class AccountsManager {
private final Accounts accounts;
private final PhoneNumberIdentifiers phoneNumberIdentifiers;
private final FaultTolerantRedisCluster cacheCluster;
private final FaultTolerantRedisCluster rateLimitCluster;
private final AccountLockManager accountLockManager;
private final KeysManager keysManager;
private final MessagesManager messagesManager;
Expand Down Expand Up @@ -160,7 +158,6 @@ public enum DeletionReason {
public AccountsManager(final Accounts accounts,
final PhoneNumberIdentifiers phoneNumberIdentifiers,
final FaultTolerantRedisCluster cacheCluster,
final FaultTolerantRedisCluster rateLimitCluster,
final AccountLockManager accountLockManager,
final KeysManager keysManager,
final MessagesManager messagesManager,
Expand All @@ -178,7 +175,6 @@ public AccountsManager(final Accounts accounts,
this.accounts = accounts;
this.phoneNumberIdentifiers = phoneNumberIdentifiers;
this.cacheCluster = cacheCluster;
this.rateLimitCluster = rateLimitCluster;
this.accountLockManager = accountLockManager;
this.keysManager = keysManager;
this.messagesManager = messagesManager;
Expand Down Expand Up @@ -345,9 +341,6 @@ private CompletableFuture<Pair<Account, Device>> addDevice(final UUID accountIde
return accounts.updateTransactionallyAsync(account, additionalWriteItems)
.thenApply(ignored -> new Pair<>(account, account.getDevice(nextDeviceId).orElseThrow()));
})
.thenCompose(updatedAccountAndDevice -> rateLimitCluster.withCluster(connection ->
connection.async().set(getUsedTokenKey(linkDeviceToken), "", new SetArgs().ex(LINK_DEVICE_TOKEN_EXPIRATION_DURATION)))
.thenApply(ignored -> updatedAccountAndDevice))
.thenCompose(updatedAccountAndDevice -> redisDeleteAsync(updatedAccountAndDevice.first())
.thenApply(ignored -> updatedAccountAndDevice))
.exceptionallyCompose(throwable -> {
Expand Down Expand Up @@ -412,20 +405,13 @@ static String generateDeviceLinkingToken(final UUID aci, final Key linkDeviceTok

/**
* Checks that a device-linking token is valid and returns the account identifier from the token if so, or empty if
* the token was invalid or has already been used
* the token was invalid
*
* @param token the device-linking token to check
*
* @return the account identifier from a valid token or empty if the token was invalid or already used
* @return the account identifier from a valid token or empty if the token was invalid
*/
public Optional<UUID> checkDeviceLinkingToken(final String token) {
final boolean tokenUsed = rateLimitCluster.withCluster(connection ->
connection.sync().get(getUsedTokenKey(token)) != null);

if (tokenUsed) {
return Optional.empty();
}

final String[] claimsAndSignature = token.split(":", 2);

if (claimsAndSignature.length != 2) {
Expand Down Expand Up @@ -476,10 +462,6 @@ public Optional<UUID> checkDeviceLinkingToken(final String token) {
return Optional.of(aci);
}

private static String getUsedTokenKey(final String token) {
return "usedToken::" + token;
}

public CompletableFuture<Account> removeDevice(final Account account, final byte deviceId) {
if (deviceId == Device.PRIMARY_ID) {
throw new IllegalArgumentException("Cannot remove primary device");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static CommandDependencies build(
ClientPublicKeysManager clientPublicKeysManager =
new ClientPublicKeysManager(clientPublicKeys, accountLockManager, accountLockExecutor);
AccountsManager accountsManager = new AccountsManager(accounts, phoneNumberIdentifiers, cacheCluster,
rateLimitersCluster, accountLockManager, keys, messagesManager, profilesManager,
accountLockManager, keys, messagesManager, profilesManager,
secureStorageClient, secureValueRecovery2Client, clientPresenceManager,
registrationRecoveryPasswordsManager, clientPublicKeysManager, accountLockExecutor, clientPresenceExecutor,
clock, configuration.getLinkDeviceSecretConfiguration().secret().value(), dynamicConfigurationManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ void setUp() {
accounts,
phoneNumberIdentifiers,
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
accountLockManager,
keysManager,
messagesManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ void setup() throws InterruptedException {
accounts,
phoneNumberIdentifiers,
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
accountLockManager,
keysManager,
messagesManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ void setup() throws InterruptedException {
accounts,
phoneNumberIdentifiers,
RedisClusterHelper.builder().stringCommands(commands).build(),
RedisClusterHelper.builder().stringCommands(commands).build(),
accountLockManager,
mock(KeysManager.class),
mock(MessagesManager.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ void setup() throws InterruptedException {
accounts,
phoneNumberIdentifiers,
redisCluster,
redisCluster,
accountLockManager,
keysManager,
messagesManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ private void buildAccountsManager(final int initialWidth, int discriminatorMaxWi
accounts,
phoneNumberIdentifiers,
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
accountLockManager,
keysManager,
messageManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ void setUp() {
accounts,
phoneNumberIdentifiers,
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
accountLockManager,
keysManager,
messagesManager,
Expand Down

0 comments on commit 63e4556

Please sign in to comment.