mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 00:56:50 +00:00
Add Devices.ChangeDevicePassword
This commit is contained in:
1 parent
fbb42763d9
commit
685026dd39
5 files changed
+171
-1
No files matched your search
+19
-1
@@ -12,6 +12,8 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.signal.chat.device.ChangeDevicePasswordRequest;
|
||||
import org.signal.chat.device.ChangeDevicePasswordResponse;
|
||||
import org.signal.chat.device.ClearPushTokenRequest;
|
||||
import org.signal.chat.device.ClearPushTokenResponse;
|
||||
import org.signal.chat.device.GetDevicesRequest;
|
||||
@@ -28,7 +30,6 @@ import org.signal.chat.device.SimpleDevicesGrpc;
|
||||
import org.signal.chat.errors.NotFound;
|
||||
import org.whispersystems.textsecuregcm.auth.grpc.AuthenticatedDevice;
|
||||
import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil;
|
||||
import org.whispersystems.textsecuregcm.identity.IdentityType;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
@@ -184,6 +185,23 @@ public class DevicesGrpcService extends SimpleDevicesGrpc.DevicesImplBase {
|
||||
return SetCapabilitiesResponse.getDefaultInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChangeDevicePasswordResponse changeDevicePassword(final ChangeDevicePasswordRequest request) {
|
||||
final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice();
|
||||
|
||||
if (request.getNewPassword().isBlank()) {
|
||||
throw GrpcExceptions.fieldViolation("new_password", "password must not be blank");
|
||||
}
|
||||
|
||||
if (!accountsManager.updateDevicePassword(authenticatedDevice.accountIdentifier(), authenticatedDevice.deviceId(),
|
||||
request.getNewPassword())) {
|
||||
|
||||
throw GrpcExceptions.invalidCredentials("invalid credentials");
|
||||
}
|
||||
|
||||
return ChangeDevicePasswordResponse.getDefaultInstance();
|
||||
}
|
||||
|
||||
private Account getAuthenticatedAccount() {
|
||||
return accountsManager.getByAccountIdentifier(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier())
|
||||
.orElseThrow(() -> GrpcExceptions.invalidCredentials("invalid credentials"));
|
||||
|
||||
@@ -71,6 +71,7 @@ import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialPresentation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.auth.DisconnectionRequestManager;
|
||||
import org.whispersystems.textsecuregcm.auth.SaltedTokenHash;
|
||||
import org.whispersystems.textsecuregcm.auth.webauthn.AuthenticationCeremonyParameters;
|
||||
import org.whispersystems.textsecuregcm.auth.webauthn.RegistrationCeremonyParameters;
|
||||
import org.whispersystems.textsecuregcm.auth.webauthn.RegistrationCeremonyResult;
|
||||
@@ -1377,6 +1378,43 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
});
|
||||
}
|
||||
|
||||
/// Sets a new password for the given device and requests disconnection for all authenticated device connections.
|
||||
///
|
||||
///
|
||||
/// @return `true` if the password was changed or `false` if the device no longer exists or its credentials
|
||||
/// have been locked
|
||||
public boolean updateDevicePassword(final UUID accountIdentifier, final byte deviceId, final String newPassword) {
|
||||
|
||||
final SaltedTokenHash newCredentials = SaltedTokenHash.generateFor(newPassword);
|
||||
|
||||
final Account updatedAccount = update(accountIdentifier, a -> {
|
||||
|
||||
final Optional<Device> maybeDevice = a.getDevice(deviceId);
|
||||
|
||||
return maybeDevice.map(d -> {
|
||||
|
||||
if (d.hasLockedCredentials()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
d.setAuthTokenHash(newCredentials);
|
||||
|
||||
return true;
|
||||
|
||||
}).orElse(false);
|
||||
});
|
||||
|
||||
final boolean passwordChanged = updatedAccount.getDevice(deviceId)
|
||||
.map(d -> d.getAuthTokenHash().equals(newCredentials))
|
||||
.orElse(false);
|
||||
|
||||
if (passwordChanged) {
|
||||
disconnectionRequestManager.requestDisconnection(accountIdentifier, List.of(deviceId));
|
||||
}
|
||||
|
||||
return passwordChanged;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param accountIdentifier identifier of account to update
|
||||
* @param updater must return {@code true} if the account was actually updated
|
||||
|
||||
@@ -46,6 +46,9 @@ service Devices {
|
||||
|
||||
// Declares that the authenticated device supports certain features.
|
||||
rpc SetCapabilities(SetCapabilitiesRequest) returns (SetCapabilitiesResponse) {}
|
||||
|
||||
// Changes the password for the currently authenticated device.
|
||||
rpc ChangeDevicePassword(ChangeDevicePasswordRequest) returns (ChangeDevicePasswordResponse) {}
|
||||
}
|
||||
|
||||
message GetDevicesRequest {}
|
||||
@@ -136,3 +139,9 @@ message SetCapabilitiesRequest {
|
||||
}
|
||||
|
||||
message SetCapabilitiesResponse {}
|
||||
|
||||
message ChangeDevicePasswordRequest {
|
||||
string new_password = 1 [(require.size) = {min: 32; max: 255}];
|
||||
}
|
||||
|
||||
message ChangeDevicePasswordResponse {}
|
||||
+57
@@ -36,6 +36,7 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.junitpioneer.jupiter.cartesian.CartesianTest;
|
||||
import org.mockito.Mock;
|
||||
import org.signal.chat.device.ChangeDevicePasswordRequest;
|
||||
import org.signal.chat.device.ClearPushTokenRequest;
|
||||
import org.signal.chat.device.ClearPushTokenResponse;
|
||||
import org.signal.chat.device.DevicesGrpc;
|
||||
@@ -504,4 +505,60 @@ class DevicesGrpcServiceTest extends SimpleBaseGrpcTest<DevicesGrpcService, Devi
|
||||
|
||||
verify(device).setCapabilities(expectedCapabilities);
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeDevicePassword() {
|
||||
final byte deviceId = 127;
|
||||
mockAuthenticationInterceptor().setAuthenticatedDevice(AUTHENTICATED_ACI, deviceId);
|
||||
|
||||
final String newPassword = RandomStringUtils.insecure().nextAlphanumeric(32);
|
||||
|
||||
when(accountsManager.updateDevicePassword(any(), anyByte(), any())).thenReturn(true);
|
||||
|
||||
authenticatedServiceStub().changeDevicePassword(ChangeDevicePasswordRequest.newBuilder()
|
||||
.setNewPassword(newPassword)
|
||||
.build());
|
||||
|
||||
verify(accountsManager).updateDevicePassword(AUTHENTICATED_ACI, deviceId, newPassword);
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeDevicePasswordNotChanged() {
|
||||
when(accountsManager.updateDevicePassword(any(), anyByte(), any())).thenReturn(false);
|
||||
|
||||
assertStatusException(Status.UNAUTHENTICATED,
|
||||
() -> authenticatedServiceStub().changeDevicePassword(ChangeDevicePasswordRequest.newBuilder()
|
||||
.setNewPassword(RandomStringUtils.insecure().nextAlphanumeric(32))
|
||||
.build()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void changeDevicePasswordIllegalArgument(final ChangeDevicePasswordRequest request) {
|
||||
assertStatusException(Status.INVALID_ARGUMENT, () -> authenticatedServiceStub().changeDevicePassword(request));
|
||||
verify(accountsManager, never()).updateDevicePassword(any(), anyByte(), any());
|
||||
}
|
||||
|
||||
private static Stream<Arguments> changeDevicePasswordIllegalArgument() {
|
||||
return Stream.of(
|
||||
Arguments.argumentSet("No new password",
|
||||
ChangeDevicePasswordRequest.newBuilder()
|
||||
.build()),
|
||||
|
||||
Arguments.argumentSet("New password too short",
|
||||
ChangeDevicePasswordRequest.newBuilder()
|
||||
.setNewPassword(RandomStringUtils.insecure().nextAlphanumeric(31))
|
||||
.build()),
|
||||
|
||||
Arguments.argumentSet("New password blank",
|
||||
ChangeDevicePasswordRequest.newBuilder()
|
||||
.setNewPassword(" ".repeat(64))
|
||||
.build()),
|
||||
|
||||
Arguments.argumentSet("New password too long",
|
||||
ChangeDevicePasswordRequest.newBuilder()
|
||||
.setNewPassword(RandomStringUtils.insecure().nextAlphanumeric(256))
|
||||
.build())
|
||||
);
|
||||
}
|
||||
}
|
||||
+48
@@ -1414,6 +1414,54 @@ class AccountsManagerTest {
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void testUpdateDevicePassword(final boolean credentialsLocked) {
|
||||
final Account account = AccountsHelper.generateTestAccount(null, UUID.randomUUID(), null, new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]);
|
||||
addRetrievableAccount(account);
|
||||
|
||||
final String newPassword = RandomStringUtils.insecure().nextAlphanumeric(32);
|
||||
|
||||
final Device device = new Device();
|
||||
device.setId(Device.PRIMARY_ID);
|
||||
device.setAuthTokenHash(SaltedTokenHash.generateFor("old-password"));
|
||||
account.addDevice(device);
|
||||
|
||||
if (credentialsLocked) {
|
||||
device.lockAuthTokenHash();
|
||||
}
|
||||
final SaltedTokenHash lockedCredentials = device.getAuthTokenHash();
|
||||
|
||||
assertEquals(!credentialsLocked, accountsManager.updateDevicePassword(account.getAccountIdentifier(),
|
||||
Device.PRIMARY_ID, newPassword));
|
||||
|
||||
if (credentialsLocked) {
|
||||
assertEquals(lockedCredentials, device.getAuthTokenHash());
|
||||
assertTrue(device.hasLockedCredentials());
|
||||
|
||||
verify(accounts, never()).update(any());
|
||||
verifyNoInteractions(disconnectionRequestManager);
|
||||
} else {
|
||||
assertTrue(device.getAuthTokenHash().verify(newPassword));
|
||||
|
||||
verify(accounts).update(account);
|
||||
verify(disconnectionRequestManager)
|
||||
.requestDisconnection(account.getAccountIdentifier(), List.of(Device.PRIMARY_ID));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateDevicePasswordDeviceNotFound() {
|
||||
final Account account = AccountsHelper.generateTestAccount(null, UUID.randomUUID(), null, new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]);
|
||||
addRetrievableAccount(account);
|
||||
final byte unknownDeviceId = 2;
|
||||
|
||||
assertFalse(accountsManager.updateDevicePassword(account.getAccountIdentifier(), unknownDeviceId, "new-password"));
|
||||
|
||||
verify(accounts, never()).update(any());
|
||||
verifyNoInteractions(disconnectionRequestManager);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"+14152222222,+14153333333",
|
||||
|
||||
Reference in new issue
Block a user