mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:06:50 +00:00
Use a TOTP algorithm supported by a wider range of authenticator apps
This commit is contained in:
1 parent
56a91f104e
commit
b247fc24c1
11 files changed
+58
-89
No files matched your search
+10
-20
@@ -8,7 +8,6 @@ package org.whispersystems.textsecuregcm.storage;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||
|
||||
import com.eatthepath.otp.HmacOneTimePasswordGenerator;
|
||||
import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
@@ -150,7 +149,6 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
private final Duration maxTotpValidationDelay;
|
||||
|
||||
private final KeyGenerator totpKeyGenerator;
|
||||
private final TimeBasedOneTimePasswordGenerator totpGenerator;
|
||||
|
||||
private final Key verificationTokenKey;
|
||||
|
||||
@@ -206,13 +204,13 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
static final String LINK_DEVICE_VERIFICATION_TOKEN_ALGORITHM = "HmacSHA256";
|
||||
|
||||
@VisibleForTesting
|
||||
static final int TOTP_KEY_LENGTH_BITS = 256;
|
||||
static final int TOTP_KEY_LENGTH_BITS = 160;
|
||||
|
||||
@VisibleForTesting
|
||||
public static final TotpParameters TOTP_PARAMETERS = new TotpParameters(
|
||||
TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA256,
|
||||
HmacOneTimePasswordGenerator.DEFAULT_PASSWORD_LENGTH,
|
||||
TimeBasedOneTimePasswordGenerator.DEFAULT_TIME_STEP);
|
||||
public static final TimeBasedOneTimePasswordGenerator TOTP = new TimeBasedOneTimePasswordGenerator();
|
||||
|
||||
private static final TotpParameters TOTP_PARAMETERS =
|
||||
new TotpParameters(TOTP.getAlgorithm(), TOTP.getPasswordLength(), TOTP.getTimeStep());
|
||||
|
||||
public static final int MAX_TOTP_KEYS = 2;
|
||||
|
||||
@@ -324,7 +322,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
this.retryExecutor = retryExecutor;
|
||||
this.clock = requireNonNull(clock);
|
||||
|
||||
if (maxTotpValidationDelay.compareTo(TOTP_PARAMETERS.timeStep()) > 0) {
|
||||
if (maxTotpValidationDelay.compareTo(TOTP.getTimeStep()) > 0) {
|
||||
throw new IllegalArgumentException("Max TOTP validation delay must be less than or equal to TOTP time step");
|
||||
}
|
||||
|
||||
@@ -340,20 +338,12 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
}
|
||||
|
||||
try {
|
||||
this.totpKeyGenerator = KeyGenerator.getInstance(TOTP_PARAMETERS.algorithm());
|
||||
this.totpKeyGenerator = KeyGenerator.getInstance(TOTP.getAlgorithm());
|
||||
totpKeyGenerator.init(TOTP_KEY_LENGTH_BITS);
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new AssertionError("Every implementation of the Java platform is required to support the HmacSHA256 KeyGenerator algorithm", e);
|
||||
}
|
||||
|
||||
try {
|
||||
this.totpGenerator = new TimeBasedOneTimePasswordGenerator(TOTP_PARAMETERS.timeStep(),
|
||||
TOTP_PARAMETERS.passwordLength(),
|
||||
TOTP_PARAMETERS.algorithm());
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new AssertionError("Every implementation of the Java platform is required to support the HmacSHA256 MAC algorithm", e);
|
||||
}
|
||||
|
||||
this.pubSubConnection = pubSubRedisClient.createPubSubConnection();
|
||||
}
|
||||
|
||||
@@ -2076,7 +2066,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
final TotpKey pendingTotpKey = maybePendingTotpKey.get();
|
||||
|
||||
try {
|
||||
if (totpGenerator.validateOneTimePassword(pendingTotpKey, timestamp, oneTimePassword)) {
|
||||
if (TOTP.validateOneTimePassword(pendingTotpKey, timestamp, oneTimePassword)) {
|
||||
final AtomicInteger keyId = new AtomicInteger();
|
||||
|
||||
update(accountIdentifier, account -> {
|
||||
@@ -2112,7 +2102,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
.max(Map.Entry.comparingByKey())
|
||||
.filter(entry -> {
|
||||
try {
|
||||
return totpGenerator.validateOneTimePassword(entry.getValue(), timestamp, oneTimePassword);
|
||||
return TOTP.validateOneTimePassword(entry.getValue(), timestamp, oneTimePassword);
|
||||
} catch (final InvalidKeyException e) {
|
||||
ImpossibleEvents.logImpossible(logger, "Invalid TOTP key for account {}", accountIdentifier, e);
|
||||
return false;
|
||||
@@ -2134,7 +2124,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
for (final Instant timestamp : new Instant[] { validationTimestamp, validationTimestamp.minus(maxTotpValidationDelay) }) {
|
||||
for (final SecretKey totpKey : account.getTotpKeys().values()) {
|
||||
try {
|
||||
if (totpGenerator.validateOneTimePassword(totpKey, timestamp, oneTimePassword)) {
|
||||
if (TOTP.validateOneTimePassword(totpKey, timestamp, oneTimePassword)) {
|
||||
return true;
|
||||
}
|
||||
} catch (final InvalidKeyException e) {
|
||||
|
||||
+14
-8
@@ -161,6 +161,12 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
@Mock
|
||||
private ChangeNumberManager changeNumberManager;
|
||||
|
||||
private static final org.whispersystems.textsecuregcm.storage.TotpParameters TOTP_PARAMETERS =
|
||||
new org.whispersystems.textsecuregcm.storage.TotpParameters(
|
||||
AccountsManager.TOTP.getAlgorithm(),
|
||||
AccountsManager.TOTP.getPasswordLength(),
|
||||
AccountsManager.TOTP.getTimeStep());
|
||||
|
||||
private static final int TOTP_KEY_METADATA_SIZE = 160;
|
||||
|
||||
@Override
|
||||
@@ -1122,7 +1128,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
final byte[] encodedKey = TestRandomUtil.nextBytes(16);
|
||||
|
||||
when(accountsManager.generatePendingTotpKey(AUTHENTICATED_ACI))
|
||||
.thenReturn(new TotpKey(AccountsManager.TOTP_PARAMETERS, encodedKey));
|
||||
.thenReturn(new TotpKey(TOTP_PARAMETERS, encodedKey));
|
||||
|
||||
final GenerateTotpKeyResponse response =
|
||||
authenticatedServiceStub().generateTotpKey(GenerateTotpKeyRequest.getDefaultInstance());
|
||||
@@ -1130,9 +1136,9 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
assertEquals(GenerateTotpKeyResponse.ResponseCase.KEY_GENERATED, response.getResponseCase());
|
||||
|
||||
assertArrayEquals(encodedKey, response.getKeyGenerated().getKey().toByteArray());
|
||||
assertEquals(AccountsManager.TOTP_PARAMETERS.algorithm(), response.getKeyGenerated().getTotpParameters().getAlgorithm());
|
||||
assertEquals(AccountsManager.TOTP_PARAMETERS.passwordLength(), response.getKeyGenerated().getTotpParameters().getPasswordLength());
|
||||
assertEquals(AccountsManager.TOTP_PARAMETERS.timeStep().toSeconds(), response.getKeyGenerated().getTotpParameters().getTimeStepSeconds());
|
||||
assertEquals(AccountsManager.TOTP.getAlgorithm(), response.getKeyGenerated().getTotpParameters().getAlgorithm());
|
||||
assertEquals(AccountsManager.TOTP.getPasswordLength(), response.getKeyGenerated().getTotpParameters().getPasswordLength());
|
||||
assertEquals(AccountsManager.TOTP.getTimeStep().toSeconds(), response.getKeyGenerated().getTotpParameters().getTimeStepSeconds());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1202,9 +1208,9 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
authenticatedServiceStub().listTotpKeys(ListTotpKeysRequest.getDefaultInstance());
|
||||
|
||||
final TotpParameters expectedTotpParameters = TotpParameters.newBuilder()
|
||||
.setAlgorithm(AccountsManager.TOTP_PARAMETERS.algorithm())
|
||||
.setPasswordLength(AccountsManager.TOTP_PARAMETERS.passwordLength())
|
||||
.setTimeStepSeconds(Math.toIntExact(AccountsManager.TOTP_PARAMETERS.timeStep().toSeconds()))
|
||||
.setAlgorithm(AccountsManager.TOTP.getAlgorithm())
|
||||
.setPasswordLength(AccountsManager.TOTP.getPasswordLength())
|
||||
.setTimeStepSeconds(Math.toIntExact(AccountsManager.TOTP.getTimeStep().toSeconds()))
|
||||
.build();
|
||||
|
||||
final Map<Integer, ListTotpKeysResponse.TotpKeyMetadata> expectedTotpKeys = totpKeys.entrySet().stream()
|
||||
@@ -1305,7 +1311,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
}
|
||||
|
||||
private static AnnotatedTotpKey generateRandomAnnotatedTotpKey() {
|
||||
return new AnnotatedTotpKey(new TotpKey(AccountsManager.TOTP_PARAMETERS, TestRandomUtil.nextBytes(32)),
|
||||
return new AnnotatedTotpKey(new TotpKey(TOTP_PARAMETERS, TestRandomUtil.nextBytes(32)),
|
||||
TestRandomUtil.nextBytes(TOTP_KEY_METADATA_SIZE));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -176,7 +176,7 @@ public class AccountCreationDeletionIntegrationTest {
|
||||
executor,
|
||||
CLOCK,
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2));
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
@@ -325,7 +325,7 @@ class AccountTest {
|
||||
.mapToObj(i -> (byte) i)
|
||||
.collect(Collectors.toMap(keyId -> keyId, _ -> new AnnotatedTotpKey(new TotpKey(
|
||||
new TotpParameters(
|
||||
TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA256,
|
||||
TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA1,
|
||||
HmacOneTimePasswordGenerator.DEFAULT_PASSWORD_LENGTH,
|
||||
TimeBasedOneTimePasswordGenerator.DEFAULT_TIME_STEP),
|
||||
TestRandomUtil.nextBytes(16)),
|
||||
|
||||
+1
-1
@@ -156,7 +156,7 @@ class AccountsManagerChangeNumberIntegrationTest {
|
||||
executor,
|
||||
mock(Clock.class),
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2));
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -137,7 +137,7 @@ class AccountsManagerConcurrentModificationIntegrationTest {
|
||||
mock(ScheduledExecutorService.class),
|
||||
mock(Clock.class),
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2)
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ public class AccountsManagerDeviceTransferIntegrationTest {
|
||||
mock(ScheduledExecutorService.class),
|
||||
Clock.systemUTC(),
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2));
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2));
|
||||
|
||||
accountsManager.start();
|
||||
}
|
||||
|
||||
+1
-1
@@ -142,7 +142,7 @@ public class AccountsManagerMigrateRecoveryPasswordIntegrationTest {
|
||||
executor,
|
||||
mock(Clock.class),
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2));
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-53
@@ -33,7 +33,6 @@ import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator;
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import io.lettuce.core.RedisException;
|
||||
import io.lettuce.core.api.async.RedisAsyncCommands;
|
||||
@@ -127,7 +126,11 @@ class AccountsManagerTest {
|
||||
|
||||
private static final byte[] LINK_DEVICE_SECRET = "link-device-secret".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private static final Duration MAX_TOTP_VALIDATION_DELAY = AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2);
|
||||
private static final Duration MAX_TOTP_VALIDATION_DELAY = AccountsManager.TOTP.getTimeStep().dividedBy(2);
|
||||
|
||||
private static final TotpParameters TOTP_PARAMETERS = new TotpParameters(AccountsManager.TOTP.getAlgorithm(),
|
||||
AccountsManager.TOTP.getPasswordLength(),
|
||||
AccountsManager.TOTP.getTimeStep());
|
||||
|
||||
private static TestClock CLOCK;
|
||||
|
||||
@@ -1958,7 +1961,7 @@ class AccountsManagerTest {
|
||||
when(account.getTotpKeys()).thenReturn(IntStream.range(0, AccountsManager.MAX_TOTP_KEYS)
|
||||
.boxed()
|
||||
.collect(Collectors.toMap(Integer::byteValue, _ -> new AnnotatedTotpKey(
|
||||
new TotpKey(AccountsManager.TOTP_PARAMETERS, TestRandomUtil.nextBytes(16)),
|
||||
new TotpKey(TOTP_PARAMETERS, TestRandomUtil.nextBytes(16)),
|
||||
TestRandomUtil.nextBytes(16)))));
|
||||
|
||||
when(accounts.getByAccountIdentifier(accountIdentifier))
|
||||
@@ -1984,15 +1987,10 @@ class AccountsManagerTest {
|
||||
when(account.getPendingTotpKey()).thenReturn(Optional.of(pendingTotpKey));
|
||||
when(account.getNextTotpKeyId()).thenReturn(nextTotpKeyId);
|
||||
|
||||
final TimeBasedOneTimePasswordGenerator totpGenerator =
|
||||
new TimeBasedOneTimePasswordGenerator(AccountsManager.TOTP_PARAMETERS.timeStep(),
|
||||
AccountsManager.TOTP_PARAMETERS.passwordLength(),
|
||||
AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
|
||||
final Instant timestamp = Instant.now();
|
||||
|
||||
assertEquals(Optional.of(nextTotpKeyId), accountsManager.confirmPendingTotpKey(accountIdentifier,
|
||||
totpGenerator.generateOneTimePassword(pendingTotpKey, timestamp),
|
||||
AccountsManager.TOTP.generateOneTimePassword(pendingTotpKey, timestamp),
|
||||
timestamp,
|
||||
TestRandomUtil.nextBytes(16)));
|
||||
|
||||
@@ -2011,11 +2009,11 @@ class AccountsManagerTest {
|
||||
|
||||
final AnnotatedTotpKey confirmedTotpKey;
|
||||
{
|
||||
final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP.getAlgorithm());
|
||||
totpKeyGenerator.init(AccountsManager.TOTP_KEY_LENGTH_BITS);
|
||||
|
||||
confirmedTotpKey = new AnnotatedTotpKey(
|
||||
new TotpKey(AccountsManager.TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
TestRandomUtil.nextBytes(16));
|
||||
}
|
||||
|
||||
@@ -2026,15 +2024,10 @@ class AccountsManagerTest {
|
||||
(byte) (keyId - 1), confirmedTotpKey,
|
||||
keyId, confirmedTotpKey));
|
||||
|
||||
final TimeBasedOneTimePasswordGenerator totpGenerator =
|
||||
new TimeBasedOneTimePasswordGenerator(AccountsManager.TOTP_PARAMETERS.timeStep(),
|
||||
AccountsManager.TOTP_PARAMETERS.passwordLength(),
|
||||
AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
|
||||
final Instant timestamp = Instant.now();
|
||||
|
||||
assertEquals(Optional.of(keyId), accountsManager.confirmPendingTotpKey(accountIdentifier,
|
||||
totpGenerator.generateOneTimePassword(confirmedTotpKey, timestamp),
|
||||
AccountsManager.TOTP.generateOneTimePassword(confirmedTotpKey, timestamp),
|
||||
timestamp,
|
||||
TestRandomUtil.nextBytes(16)));
|
||||
}
|
||||
@@ -2054,15 +2047,10 @@ class AccountsManagerTest {
|
||||
when(account.getPendingTotpKey()).thenReturn(Optional.empty());
|
||||
when(account.getTotpKeys()).thenReturn(Collections.emptyMap());
|
||||
|
||||
final TimeBasedOneTimePasswordGenerator totpGenerator =
|
||||
new TimeBasedOneTimePasswordGenerator(AccountsManager.TOTP_PARAMETERS.timeStep(),
|
||||
AccountsManager.TOTP_PARAMETERS.passwordLength(),
|
||||
AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
|
||||
final Instant timestamp = Instant.now();
|
||||
|
||||
assertEquals(Optional.empty(), accountsManager.confirmPendingTotpKey(accountIdentifier,
|
||||
totpGenerator.generateOneTimePassword(pendingTotpKey, timestamp),
|
||||
AccountsManager.TOTP.generateOneTimePassword(pendingTotpKey, timestamp),
|
||||
timestamp,
|
||||
TestRandomUtil.nextBytes(16)));
|
||||
}
|
||||
@@ -2084,13 +2072,8 @@ class AccountsManagerTest {
|
||||
when(account.getPendingTotpKey()).thenReturn(Optional.of(pendingTotpKey));
|
||||
when(account.getNextTotpKeyId()).thenReturn(nextTotpKeyId);
|
||||
|
||||
final TimeBasedOneTimePasswordGenerator totpGenerator =
|
||||
new TimeBasedOneTimePasswordGenerator(AccountsManager.TOTP_PARAMETERS.timeStep(),
|
||||
AccountsManager.TOTP_PARAMETERS.passwordLength(),
|
||||
AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
|
||||
final Instant timestamp = Instant.now();
|
||||
final int incorrectPassword = totpGenerator.generateOneTimePassword(pendingTotpKey, timestamp) + 1;
|
||||
final int incorrectPassword = AccountsManager.TOTP.generateOneTimePassword(pendingTotpKey, timestamp) + 1;
|
||||
|
||||
assertEquals(Optional.empty(), accountsManager.confirmPendingTotpKey(accountIdentifier,
|
||||
incorrectPassword,
|
||||
@@ -2120,23 +2103,18 @@ class AccountsManagerTest {
|
||||
final AnnotatedTotpKey totpKey;
|
||||
final AnnotatedTotpKey secondTotpKey;
|
||||
{
|
||||
final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP.getAlgorithm());
|
||||
totpKeyGenerator.init(AccountsManager.TOTP_KEY_LENGTH_BITS);
|
||||
|
||||
totpKey = new AnnotatedTotpKey(
|
||||
new TotpKey(AccountsManager.TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
TestRandomUtil.nextBytes(16));
|
||||
|
||||
secondTotpKey = new AnnotatedTotpKey(
|
||||
new TotpKey(AccountsManager.TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
TestRandomUtil.nextBytes(16));
|
||||
}
|
||||
|
||||
final TimeBasedOneTimePasswordGenerator totpGenerator =
|
||||
new TimeBasedOneTimePasswordGenerator(AccountsManager.TOTP_PARAMETERS.timeStep(),
|
||||
AccountsManager.TOTP_PARAMETERS.passwordLength(),
|
||||
AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
|
||||
return List.of(
|
||||
Arguments.argumentSet("No keys, no password provided",
|
||||
Collections.emptyMap(), timestamp, null, true),
|
||||
@@ -2145,16 +2123,16 @@ class AccountsManagerTest {
|
||||
Collections.emptyMap(), timestamp, 123456, false),
|
||||
|
||||
Arguments.argumentSet("Has key, correct password provided",
|
||||
Map.of((byte) 1, totpKey), timestamp, totpGenerator.generateOneTimePassword(totpKey, timestamp), true),
|
||||
Map.of((byte) 1, totpKey), timestamp, AccountsManager.TOTP.generateOneTimePassword(totpKey, timestamp), true),
|
||||
|
||||
Arguments.argumentSet("Has key, incorrect password provided",
|
||||
Map.of((byte) 1, totpKey), timestamp, totpGenerator.generateOneTimePassword(totpKey, timestamp) + 1, false),
|
||||
Map.of((byte) 1, totpKey), timestamp, AccountsManager.TOTP.generateOneTimePassword(totpKey, timestamp) + 1, false),
|
||||
|
||||
Arguments.argumentSet("Has key, no password provided",
|
||||
Map.of((byte) 1, totpKey), timestamp, null, false),
|
||||
|
||||
Arguments.argumentSet("Has multiple keys, correct password provided for one key",
|
||||
Map.of((byte) 1, totpKey, (byte) 2, secondTotpKey), timestamp, totpGenerator.generateOneTimePassword(totpKey, timestamp), true)
|
||||
Map.of((byte) 1, totpKey, (byte) 2, secondTotpKey), timestamp, AccountsManager.TOTP.generateOneTimePassword(totpKey, timestamp), true)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2162,40 +2140,35 @@ class AccountsManagerTest {
|
||||
void verifyTotpWithDelay() throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
final AnnotatedTotpKey totpKey;
|
||||
{
|
||||
final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP.getAlgorithm());
|
||||
totpKeyGenerator.init(AccountsManager.TOTP_KEY_LENGTH_BITS);
|
||||
|
||||
totpKey = new AnnotatedTotpKey(
|
||||
new TotpKey(AccountsManager.TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),
|
||||
TestRandomUtil.nextBytes(16));
|
||||
}
|
||||
|
||||
final TimeBasedOneTimePasswordGenerator totpGenerator =
|
||||
new TimeBasedOneTimePasswordGenerator(AccountsManager.TOTP_PARAMETERS.timeStep(),
|
||||
AccountsManager.TOTP_PARAMETERS.passwordLength(),
|
||||
AccountsManager.TOTP_PARAMETERS.algorithm());
|
||||
|
||||
final Account account = mock(Account.class);
|
||||
when(account.getTotpKeys()).thenReturn(Map.of((byte) 1, totpKey));
|
||||
|
||||
final Instant beginningOfTotpWindow =
|
||||
Instant.ofEpochMilli((Instant.now().toEpochMilli() / AccountsManager.TOTP_PARAMETERS.timeStep().toMillis()) *
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().toMillis());
|
||||
Instant.ofEpochMilli((Instant.now().toEpochMilli() / AccountsManager.TOTP.getTimeStep().toMillis()) *
|
||||
AccountsManager.TOTP.getTimeStep().toMillis());
|
||||
|
||||
final int oneTimePassword = totpGenerator.generateOneTimePassword(totpKey, beginningOfTotpWindow);
|
||||
final int oneTimePassword = AccountsManager.TOTP.generateOneTimePassword(totpKey, beginningOfTotpWindow);
|
||||
|
||||
assertTrue(accountsManager.verifyTotp(account, beginningOfTotpWindow, oneTimePassword),
|
||||
"One-time password should be valid at the start of the window in which it was generated");
|
||||
|
||||
assertTrue(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP_PARAMETERS.timeStep()), oneTimePassword),
|
||||
assertTrue(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP.getTimeStep()), oneTimePassword),
|
||||
"One-time password should be valid at the start of the window after which it was generated");
|
||||
|
||||
assertTrue(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP_PARAMETERS.timeStep()).plus(MAX_TOTP_VALIDATION_DELAY).minusMillis(1), oneTimePassword),
|
||||
assertTrue(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP.getTimeStep()).plus(MAX_TOTP_VALIDATION_DELAY).minusMillis(1), oneTimePassword),
|
||||
"One-time password should be valid up until max delay after end of current TOTP window");
|
||||
|
||||
// With six-digit OTPs, there's a one-in-a-million chance of this returning a false positive, and so we repeat the
|
||||
// test several allowing for failure
|
||||
assertFalse(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP_PARAMETERS.timeStep()).plus(MAX_TOTP_VALIDATION_DELAY), oneTimePassword),
|
||||
assertFalse(accountsManager.verifyTotp(account, beginningOfTotpWindow.plus(AccountsManager.TOTP.getTimeStep()).plus(MAX_TOTP_VALIDATION_DELAY), oneTimePassword),
|
||||
"One-time password should not be valid after max delay past end of current TOTP window");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -158,7 +158,7 @@ class AccountsManagerUsernameIntegrationTest {
|
||||
Executors.newSingleThreadScheduledExecutor(),
|
||||
Clock.systemUTC(),
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2));
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+1
-1
@@ -162,7 +162,7 @@ public class AddRemoveDeviceIntegrationTest {
|
||||
scheduledExecutorService,
|
||||
clock,
|
||||
"link-device-secret".getBytes(StandardCharsets.UTF_8),
|
||||
AccountsManager.TOTP_PARAMETERS.timeStep().dividedBy(2));
|
||||
AccountsManager.TOTP.getTimeStep().dividedBy(2));
|
||||
|
||||
accountsManager.start();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user