Change reglock expiration check to be > 0 instead of >= 0

This commit is contained in:
Katherine Yen 2023-03-21 12:46:35 -07:00 committed by GitHub
parent cd27fe0409
commit a3a7d7108b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 19 deletions

View File

@ -141,7 +141,7 @@ public class RegistrationLockVerificationManager {
backupServiceCredentialGenerator.generateForUuid(account.getUuid()); backupServiceCredentialGenerator.generateForUuid(account.getUuid());
throw new WebApplicationException(Response.status(FAILURE_HTTP_STATUS) throw new WebApplicationException(Response.status(FAILURE_HTTP_STATUS)
.entity(new RegistrationLockFailure(existingRegistrationLock.getTimeRemaining(), .entity(new RegistrationLockFailure(existingRegistrationLock.getTimeRemaining().toMillis(),
existingRegistrationLock.needsFailureCredentials() ? existingBackupCredentials : null)) existingRegistrationLock.needsFailureCredentials() ? existingBackupCredentials : null))
.build()); .build());
} }

View File

@ -7,8 +7,9 @@ package org.whispersystems.textsecuregcm.auth;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import java.time.Duration; import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.whispersystems.textsecuregcm.util.Util; import org.whispersystems.textsecuregcm.util.Util;
@ -27,13 +28,13 @@ public class StoredRegistrationLock {
private final Optional<String> registrationLockSalt; private final Optional<String> registrationLockSalt;
private final long lastSeen; private final Instant lastSeen;
/** /**
* @return milliseconds since the last time the account was seen. * @return milliseconds since the last time the account was seen.
*/ */
private long timeSinceLastSeen() { private long timeSinceLastSeen() {
return System.currentTimeMillis() - lastSeen; return System.currentTimeMillis() - lastSeen.toEpochMilli();
} }
/** /**
@ -47,21 +48,17 @@ public class StoredRegistrationLock {
return hasLockAndSalt(); return hasLockAndSalt();
} }
public StoredRegistrationLock(Optional<String> registrationLock, Optional<String> registrationLockSalt, long lastSeen) { public StoredRegistrationLock(Optional<String> registrationLock, Optional<String> registrationLockSalt, Instant lastSeen) {
this.registrationLock = registrationLock; this.registrationLock = registrationLock;
this.registrationLockSalt = registrationLockSalt; this.registrationLockSalt = registrationLockSalt;
this.lastSeen = lastSeen; this.lastSeen = lastSeen;
} }
private boolean hasTimeRemaining() {
return getTimeRemaining() >= 0;
}
public Status getStatus() { public Status getStatus() {
if (!isPresent()) { if (!isPresent()) {
return Status.ABSENT; return Status.ABSENT;
} }
if (hasTimeRemaining()) { if (getTimeRemaining().toMillis() > 0) {
return Status.REQUIRED; return Status.REQUIRED;
} }
return Status.EXPIRED; return Status.EXPIRED;
@ -71,8 +68,8 @@ public class StoredRegistrationLock {
return hasLockAndSalt(); return hasLockAndSalt();
} }
public long getTimeRemaining() { public Duration getTimeRemaining() {
return REGISTRATION_LOCK_EXPIRATION_DAYS.toMillis() - timeSinceLastSeen(); return REGISTRATION_LOCK_EXPIRATION_DAYS.minus(timeSinceLastSeen(), ChronoUnit.MILLIS);
} }
public boolean verify(@Nullable String clientRegistrationLock) { public boolean verify(@Nullable String clientRegistrationLock) {
@ -86,6 +83,6 @@ public class StoredRegistrationLock {
@VisibleForTesting @VisibleForTesting
public StoredRegistrationLock forTime(long timestamp) { public StoredRegistrationLock forTime(long timestamp) {
return new StoredRegistrationLock(registrationLock, registrationLockSalt, timestamp); return new StoredRegistrationLock(registrationLock, registrationLockSalt, Instant.ofEpochMilli(timestamp));
} }
} }

View File

@ -414,7 +414,7 @@ public class Account {
public StoredRegistrationLock getRegistrationLock() { public StoredRegistrationLock getRegistrationLock() {
requireNotStale(); requireNotStale();
return new StoredRegistrationLock(Optional.ofNullable(registrationLock), Optional.ofNullable(registrationLockSalt), getLastSeen()); return new StoredRegistrationLock(Optional.ofNullable(registrationLock), Optional.ofNullable(registrationLockSalt), Instant.ofEpochMilli(getLastSeen()));
} }
public Optional<byte[]> getUnidentifiedAccessKey() { public Optional<byte[]> getUnidentifiedAccessKey() {

View File

@ -5,6 +5,7 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import javax.swing.text.html.Option; import javax.swing.text.html.Option;
import java.time.Duration; import java.time.Duration;
import java.time.Instant;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -19,7 +20,7 @@ public class StoredRegistrationLockTest {
@MethodSource @MethodSource
void getStatus(final Optional<String> registrationLock, final Optional<String> salt, final long lastSeen, void getStatus(final Optional<String> registrationLock, final Optional<String> salt, final long lastSeen,
final StoredRegistrationLock.Status expectedStatus) { final StoredRegistrationLock.Status expectedStatus) {
final StoredRegistrationLock storedLock = new StoredRegistrationLock(registrationLock, salt, lastSeen); final StoredRegistrationLock storedLock = new StoredRegistrationLock(registrationLock, salt, Instant.ofEpochMilli(lastSeen));
assertEquals(expectedStatus, storedLock.getStatus()); assertEquals(expectedStatus, storedLock.getStatus());
} }

View File

@ -36,6 +36,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.Duration; import java.time.Duration;
import java.time.Instant;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.Collections; import java.util.Collections;
@ -256,22 +257,22 @@ class AccountControllerTest {
when(senderPinAccount.getLastSeen()).thenReturn(System.currentTimeMillis()); when(senderPinAccount.getLastSeen()).thenReturn(System.currentTimeMillis());
when(senderPinAccount.getRegistrationLock()).thenReturn( when(senderPinAccount.getRegistrationLock()).thenReturn(
new StoredRegistrationLock(Optional.empty(), Optional.empty(), System.currentTimeMillis())); new StoredRegistrationLock(Optional.empty(), Optional.empty(), Instant.ofEpochMilli(System.currentTimeMillis())));
when(senderHasStorage.getUuid()).thenReturn(UUID.randomUUID()); when(senderHasStorage.getUuid()).thenReturn(UUID.randomUUID());
when(senderHasStorage.isStorageSupported()).thenReturn(true); when(senderHasStorage.isStorageSupported()).thenReturn(true);
when(senderHasStorage.getRegistrationLock()).thenReturn( when(senderHasStorage.getRegistrationLock()).thenReturn(
new StoredRegistrationLock(Optional.empty(), Optional.empty(), System.currentTimeMillis())); new StoredRegistrationLock(Optional.empty(), Optional.empty(), Instant.ofEpochMilli(System.currentTimeMillis())));
when(senderRegLockAccount.getRegistrationLock()).thenReturn( when(senderRegLockAccount.getRegistrationLock()).thenReturn(
new StoredRegistrationLock(Optional.of(registrationLockCredentials.hash()), new StoredRegistrationLock(Optional.of(registrationLockCredentials.hash()),
Optional.of(registrationLockCredentials.salt()), System.currentTimeMillis())); Optional.of(registrationLockCredentials.salt()), Instant.ofEpochMilli(System.currentTimeMillis())));
when(senderRegLockAccount.getLastSeen()).thenReturn(System.currentTimeMillis()); when(senderRegLockAccount.getLastSeen()).thenReturn(System.currentTimeMillis());
when(senderRegLockAccount.getUuid()).thenReturn(SENDER_REG_LOCK_UUID); when(senderRegLockAccount.getUuid()).thenReturn(SENDER_REG_LOCK_UUID);
when(senderRegLockAccount.getNumber()).thenReturn(SENDER_REG_LOCK); when(senderRegLockAccount.getNumber()).thenReturn(SENDER_REG_LOCK);
when(senderTransfer.getRegistrationLock()).thenReturn( when(senderTransfer.getRegistrationLock()).thenReturn(
new StoredRegistrationLock(Optional.empty(), Optional.empty(), System.currentTimeMillis())); new StoredRegistrationLock(Optional.empty(), Optional.empty(), Instant.ofEpochMilli(System.currentTimeMillis())));
when(senderTransfer.getUuid()).thenReturn(SENDER_TRANSFER_UUID); when(senderTransfer.getUuid()).thenReturn(SENDER_TRANSFER_UUID);
when(senderTransfer.getNumber()).thenReturn(SENDER_TRANSFER); when(senderTransfer.getNumber()).thenReturn(SENDER_TRANSFER);