mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 00:56:50 +00:00
Handle duplicated tokens in /v2/svr/auth/check
This commit is contained in:
1 parent
adfab44ed6
commit
2f0a081d2e
5 files changed
+38
-13
No files matched your search
+5
-3
@@ -7,9 +7,11 @@ package org.whispersystems.textsecuregcm.auth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class ExternalServiceCredentialsSelector {
|
||||
|
||||
@@ -44,13 +46,13 @@ public class ExternalServiceCredentialsSelector {
|
||||
* A credential is valid if it passes validation by the provided credentialsGenerator AND it is the most recent
|
||||
* credential in the provided list for a username.
|
||||
*
|
||||
* @param tokens A list of credentials, potentially with different usernames
|
||||
* @param tokens A set of credentials, potentially with different usernames
|
||||
* @param credentialsGenerator To validate these credentials
|
||||
* @param maxAgeSeconds The maximum allowable age of the credential
|
||||
* @return A {@link CredentialInfo} for each provided token
|
||||
* @return An unordered list of {@link CredentialInfo} for each provided token
|
||||
*/
|
||||
public static List<CredentialInfo> check(
|
||||
final List<String> tokens,
|
||||
final Set<String> tokens,
|
||||
final ExternalServiceCredentialsGenerator credentialsGenerator,
|
||||
final long maxAgeSeconds) {
|
||||
|
||||
|
||||
+2
-1
@@ -28,6 +28,7 @@ import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -118,7 +119,7 @@ public class SecureValueRecovery2Controller {
|
||||
@HeaderParam(HttpHeaders.USER_AGENT) final String userAgent,
|
||||
@NotNull @Valid final AuthCheckRequest request) {
|
||||
final List<ExternalServiceCredentialsSelector.CredentialInfo> credentials = ExternalServiceCredentialsSelector.check(
|
||||
request.tokens(),
|
||||
new HashSet<>(request.tokens()),
|
||||
backupServiceCredentialGenerator,
|
||||
MAX_AGE.getSeconds());
|
||||
|
||||
|
||||
+3
-2
@@ -14,6 +14,7 @@ import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsGenerator
|
||||
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsSelector;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -34,9 +35,9 @@ public class CredentialsAnonymousGrpcService extends SimpleCredentialsAnonymousG
|
||||
|
||||
@Override
|
||||
public CheckSvrCredentialsResponse checkSvrCredentials(final CheckSvrCredentialsRequest request) {
|
||||
final List<String> tokens = request.getPasswordsList();
|
||||
final List<String> tokens = request.getPasswordsList().stream().distinct().toList();
|
||||
final List<ExternalServiceCredentialsSelector.CredentialInfo> credentials = ExternalServiceCredentialsSelector.check(
|
||||
tokens,
|
||||
new HashSet<>(tokens),
|
||||
svrCredentialsGenerator,
|
||||
MAX_SVR_PASSWORD_AGE_SECONDS);
|
||||
|
||||
|
||||
+9
-7
@@ -9,8 +9,10 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -44,11 +46,11 @@ public class ExternalServiceCredentialsSelectorTest {
|
||||
|
||||
private static ExternalServiceCredentials atTime(
|
||||
final ExternalServiceCredentialsGenerator gen,
|
||||
final long deltaMillis,
|
||||
final long deltaSeconds,
|
||||
final UUID identity) {
|
||||
final Instant old = CLOCK.instant();
|
||||
try {
|
||||
CLOCK.incrementMillis(deltaMillis);
|
||||
CLOCK.incrementMillis(deltaSeconds * 1000);
|
||||
return gen.generateForUuid(identity);
|
||||
} finally {
|
||||
CLOCK.setTimeInstant(old);
|
||||
@@ -63,7 +65,7 @@ public class ExternalServiceCredentialsSelectorTest {
|
||||
void single() {
|
||||
final ExternalServiceCredentials cred = GEN1.generateForUuid(UUID1);
|
||||
var result = ExternalServiceCredentialsSelector.check(
|
||||
List.of(token(cred)), GEN1, TimeUnit.MINUTES.toSeconds(1));
|
||||
Set.of(token(cred)), GEN1, TimeUnit.MINUTES.toSeconds(1));
|
||||
assertThat(result).singleElement()
|
||||
.matches(CredentialInfo::valid)
|
||||
.matches(info -> info.credentials().equals(cred));
|
||||
@@ -77,9 +79,9 @@ public class ExternalServiceCredentialsSelectorTest {
|
||||
final ExternalServiceCredentials cred2New = GEN1.generateForUuid(UUID2);
|
||||
final ExternalServiceCredentials cred2Old = atTime(GEN1, -1, UUID2);
|
||||
|
||||
final List<String> tokens = Stream.of(cred1New, cred1Old, cred2New, cred2Old)
|
||||
final Set<String> tokens = Stream.of(cred1New, cred1Old, cred2New, cred2Old)
|
||||
.map(ExternalServiceCredentialsSelectorTest::token)
|
||||
.toList();
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
final List<CredentialInfo> result = ExternalServiceCredentialsSelector.check(tokens, GEN1,
|
||||
TimeUnit.MINUTES.toSeconds(1));
|
||||
@@ -99,7 +101,7 @@ public class ExternalServiceCredentialsSelectorTest {
|
||||
final ExternalServiceCredentials gen2Cred = GEN2.generateForUuid(UUID1);
|
||||
|
||||
final List<CredentialInfo> result = ExternalServiceCredentialsSelector.check(
|
||||
List.of(token(gen1Cred), token(gen2Cred)),
|
||||
Set.of(token(gen1Cred), token(gen2Cred)),
|
||||
GEN2,
|
||||
TimeUnit.MINUTES.toSeconds(1));
|
||||
|
||||
@@ -120,7 +122,7 @@ public class ExternalServiceCredentialsSelectorTest {
|
||||
void invalidCredentials(final String invalidCredential) {
|
||||
final ExternalServiceCredentials validCredential = GEN1.generateForUuid(UUID1);
|
||||
var result = ExternalServiceCredentialsSelector.check(
|
||||
List.of(invalidCredential, token(validCredential)), GEN1, TimeUnit.MINUTES.toSeconds(1));
|
||||
Set.of(invalidCredential, token(validCredential)), GEN1, TimeUnit.MINUTES.toSeconds(1));
|
||||
assertThat(result).hasSize(2);
|
||||
assertThat(result).filteredOn(CredentialInfo::valid).singleElement()
|
||||
.matches(info -> info.credentials().equals(validCredential));
|
||||
|
||||
+19
@@ -211,6 +211,25 @@ public class SecureValueRecovery2ControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuplicateTokens() {
|
||||
CLOCK.setTimeMillis(dayToMillis(2));
|
||||
|
||||
// There's no good reason to do it, but the endpoint should handle a duplicated token in the request
|
||||
final String duplicatedToken = token(USER_1, dayToMillis(1));
|
||||
final AuthCheckRequest in = new AuthCheckRequest(E164_VALID, List.of(duplicatedToken, duplicatedToken));
|
||||
|
||||
final Response response = RESOURCES.getJerseyTest()
|
||||
.target(pathPrefix + "/auth/check")
|
||||
.request()
|
||||
.post(Entity.entity(in, MediaType.APPLICATION_JSON));
|
||||
|
||||
try (response) {
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(Map.of(duplicatedToken, CheckStatus.MATCH), parseCheckResponse(response));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHttpResponseCodeWhenInvalidNumber() {
|
||||
final AuthCheckRequest in = new AuthCheckRequest(E164_INVALID, Collections.singletonList("1"));
|
||||
|
||||
Reference in new issue
Block a user