Use canonical usernames throughout `AccountsManager`

This commit is contained in:
Jon Chambers 2021-11-15 11:31:01 -05:00 committed by Jon Chambers
parent efb410444b
commit 342323a7e6
2 changed files with 11 additions and 6 deletions

View File

@ -624,7 +624,7 @@ public class AccountController {
rateLimiters.getUsernameSetLimiter().validate(auth.getAccount().getUuid()); rateLimiters.getUsernameSetLimiter().validate(auth.getAccount().getUuid());
try { try {
accounts.setUsername(auth.getAccount(), UsernameValidator.getCanonicalUsername(username)); accounts.setUsername(auth.getAccount(), username);
} catch (final UsernameNotAvailableException e) { } catch (final UsernameNotAvailableException e) {
return Response.status(Response.Status.CONFLICT).build(); return Response.status(Response.Status.CONFLICT).build();
} }

View File

@ -40,6 +40,7 @@ import org.whispersystems.textsecuregcm.securestorage.SecureStorageClient;
import org.whispersystems.textsecuregcm.sqs.DirectoryQueue; import org.whispersystems.textsecuregcm.sqs.DirectoryQueue;
import org.whispersystems.textsecuregcm.util.Constants; import org.whispersystems.textsecuregcm.util.Constants;
import org.whispersystems.textsecuregcm.util.SystemMapper; import org.whispersystems.textsecuregcm.util.SystemMapper;
import org.whispersystems.textsecuregcm.util.UsernameValidator;
import org.whispersystems.textsecuregcm.util.Util; import org.whispersystems.textsecuregcm.util.Util;
public class AccountsManager { public class AccountsManager {
@ -266,11 +267,13 @@ public class AccountsManager {
} }
public Account setUsername(final Account account, final String username) throws UsernameNotAvailableException { public Account setUsername(final Account account, final String username) throws UsernameNotAvailableException {
if (account.getUsername().map(username::equals).orElse(false)) { final String canonicalUsername = UsernameValidator.getCanonicalUsername(username);
if (account.getUsername().map(canonicalUsername::equals).orElse(false)) {
return account; return account;
} }
if (reservedUsernames.isReserved(username, account.getUuid())) { if (reservedUsernames.isReserved(canonicalUsername, account.getUuid())) {
throw new UsernameNotAvailableException(); throw new UsernameNotAvailableException();
} }
@ -279,7 +282,7 @@ public class AccountsManager {
return updateWithRetries( return updateWithRetries(
account, account,
a -> true, a -> true,
a -> accounts.setUsername(a, username), a -> accounts.setUsername(a, canonicalUsername),
() -> accounts.getByAccountIdentifier(account.getUuid()).orElseThrow()); () -> accounts.getByAccountIdentifier(account.getUuid()).orElseThrow());
} }
@ -478,10 +481,12 @@ public class AccountsManager {
public Optional<Account> getByUsername(final String username) { public Optional<Account> getByUsername(final String username) {
try (final Timer.Context ignored = getByUsernameTimer.time()) { try (final Timer.Context ignored = getByUsernameTimer.time()) {
Optional<Account> account = redisGetByUsername(username); final String canonicalUsername = UsernameValidator.getCanonicalUsername(username);
Optional<Account> account = redisGetByUsername(canonicalUsername);
if (account.isEmpty()) { if (account.isEmpty()) {
account = accounts.getByUsername(username); account = accounts.getByUsername(canonicalUsername);
account.ifPresent(this::redisSet); account.ifPresent(this::redisSet);
} }