Refactor username state to use Username models.

This commit is contained in:
Greyson Parrelli
2024-01-10 14:57:14 -05:00
parent b8dea25aef
commit 50369890f7
10 changed files with 161 additions and 138 deletions

View File

@@ -783,8 +783,8 @@ public class SignalServiceAccountManager {
return this.pushServiceSocket.reserveUsername(usernameHashes);
}
public void confirmUsername(String username, ReserveUsernameResponse reserveUsernameResponse) throws IOException {
this.pushServiceSocket.confirmUsername(username, reserveUsernameResponse);
public void confirmUsername(Username username) throws IOException {
this.pushServiceSocket.confirmUsername(username);
}
public UsernameLinkComponents updateUsernameLink(UsernameLink newUsernameLink) throws IOException {

View File

@@ -0,0 +1,11 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.signalservice.api.util
import org.signal.libsignal.usernames.Username
val Username.nickname: String get() = username.split(Usernames.DELIMITER)[0]
val Username.discriminator: String get() = username.split(Usernames.DELIMITER)[1]

View File

@@ -0,0 +1,10 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.signalservice.api.util
object Usernames {
const val DELIMITER = "."
}

View File

@@ -1109,18 +1109,16 @@ public class PushServiceSocket {
* PUT /v1/accounts/username_hash/confirm
* Set a previously reserved username for the account.
*
* @param username The username the user wishes to confirm. For example, myusername.27
* @param reserveUsernameResponse The response object from the reservation
* @throws IOException Thrown when the username is invalid or taken, or when another network error occurs.
* @param username The username the user wishes to confirm.
* @throws IOException Thrown when the username is invalid or taken, or when another network error occurs.
*/
public void confirmUsername(String username, ReserveUsernameResponse reserveUsernameResponse) throws IOException {
public void confirmUsername(Username username) throws IOException {
try {
byte[] randomness = new byte[32];
random.nextBytes(randomness);
byte[] proof = new Username(username).generateProofWithRandomness(randomness);
ConfirmUsernameRequest confirmUsernameRequest = new ConfirmUsernameRequest(reserveUsernameResponse.getUsernameHash(),
Base64.encodeUrlSafeWithoutPadding(proof));
byte[] proof = username.generateProofWithRandomness(randomness);
ConfirmUsernameRequest confirmUsernameRequest = new ConfirmUsernameRequest(Base64.encodeUrlSafeWithoutPadding(username.getHash()), Base64.encodeUrlSafeWithoutPadding(proof));
makeServiceRequest(CONFIRM_USERNAME_PATH, "PUT", JsonUtil.toJson(confirmUsernameRequest), NO_HEADERS, (responseCode, body) -> {
switch (responseCode) {
@@ -1159,7 +1157,7 @@ public class PushServiceSocket {
makeServiceRequest(USERNAME_LINK_PATH, "DELETE", null);
}
/** Given a link serverId (see {@link #createUsernameLink(String)}), this will return the encrypted username associate with the link. */
/** Given a link serverId (see {@link #createUsernameLink(String, boolean)}}), this will return the encrypted username associate with the link. */
public byte[] getEncryptedUsernameFromLinkServerId(UUID serverId) throws IOException {
String response = makeServiceRequestWithoutAuthentication(String.format(USERNAME_FROM_LINK_PATH, serverId.toString()), "GET", null);
GetUsernameFromLinkResponseBody parsed = JsonUtil.fromJson(response, GetUsernameFromLinkResponseBody.class);