mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Use MailboxId instead of String for type-safety
This commit is contained in:
@@ -9,6 +9,7 @@ apply from: 'witness.gradle'
|
||||
dependencies {
|
||||
implementation "com.google.dagger:dagger:$dagger_version"
|
||||
implementation 'com.google.code.findbugs:jsr305:3.0.2'
|
||||
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
|
||||
|
||||
testImplementation "junit:junit:$junit_version"
|
||||
testImplementation "org.jmock:jmock:$jmock_version"
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
import org.briarproject.bramble.api.UniqueId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxId extends UniqueId {
|
||||
|
||||
public MailboxId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxId} from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
*/
|
||||
public static MailboxId fromString(String token) {
|
||||
if (token.length() != 64) throw new IllegalArgumentException();
|
||||
return new MailboxId(fromHexString(token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation expected by the mailbox API.
|
||||
* Also used for serialization.
|
||||
*/
|
||||
@Override
|
||||
@JsonValue
|
||||
public String toString() {
|
||||
return toHexString(getBytes()).toLowerCase(Locale.US);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof MailboxId && super.equals(o);
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,11 @@ import javax.annotation.concurrent.Immutable;
|
||||
@NotNullByDefault
|
||||
public class MailboxProperties {
|
||||
|
||||
private final String onionAddress, authToken;
|
||||
private final String onionAddress;
|
||||
private final MailboxId authToken;
|
||||
private final boolean owner;
|
||||
|
||||
public MailboxProperties(String onionAddress, String authToken,
|
||||
public MailboxProperties(String onionAddress, MailboxId authToken,
|
||||
boolean owner) {
|
||||
this.onionAddress = onionAddress;
|
||||
this.authToken = authToken;
|
||||
@@ -22,7 +23,7 @@ public class MailboxProperties {
|
||||
return onionAddress;
|
||||
}
|
||||
|
||||
public String getAuthToken() {
|
||||
public MailboxId getAuthToken() {
|
||||
return authToken;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.Identity;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.sync.ClientId;
|
||||
@@ -35,7 +36,6 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -52,7 +52,6 @@ import static org.briarproject.bramble.api.sync.SyncConstants.MAX_GROUP_DESCRIPT
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
|
||||
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
public class TestUtils {
|
||||
|
||||
@@ -216,8 +215,8 @@ public class TestUtils {
|
||||
getAgreementPublicKey(), verified);
|
||||
}
|
||||
|
||||
public static String getMailboxSecret() {
|
||||
return toHexString(getRandomBytes(32)).toLowerCase(Locale.US);
|
||||
public static MailboxId getMailboxId() {
|
||||
return new MailboxId(getRandomId());
|
||||
}
|
||||
|
||||
public static void writeBytes(File file, byte[] bytes)
|
||||
|
||||
Reference in New Issue
Block a user