Skip to content

Commit f30c003

Browse files
SyncServer: support null authenticator when only using JWT auth #252
1 parent e67b3ab commit f30c003

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

objectbox-java/src/main/java/io/objectbox/sync/Sync.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.objectbox.sync;
1818

19+
import javax.annotation.Nullable;
20+
1921
import io.objectbox.BoxStore;
2022
import io.objectbox.BoxStoreBuilder;
2123
import io.objectbox.sync.server.SyncServer;
@@ -83,17 +85,18 @@ public static SyncBuilder client(BoxStore boxStore, String url, SyncCredentials[
8385
* {@code ws://0.0.0.0:9999}.
8486
* @param authenticatorCredentials An authentication method available to Sync clients and peers. Additional
8587
* authenticator credentials can be supplied using the returned builder. For the embedded server, currently only
86-
* {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none} are supported.
88+
* {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none} are supported. When only JWT
89+
* authentication should be possible, pass {@code null}.
8790
*/
88-
public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCredentials authenticatorCredentials) {
91+
public static SyncServerBuilder server(BoxStore boxStore, String url, @Nullable SyncCredentials authenticatorCredentials) {
8992
return new SyncServerBuilder(boxStore, url, authenticatorCredentials);
9093
}
9194

9295
/**
9396
* Like {@link #server(BoxStore, String, SyncCredentials)}, but supports passing a set of authentication methods
9497
* for clients and peers.
9598
*/
96-
public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCredentials[] multipleAuthenticatorCredentials) {
99+
public static SyncServerBuilder server(BoxStore boxStore, String url, @Nullable SyncCredentials[] multipleAuthenticatorCredentials) {
97100
return new SyncServerBuilder(boxStore, url, multipleAuthenticatorCredentials);
98101
}
99102

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServerBuilder.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,17 @@ private static void checkFeatureSyncServerAvailable() {
7272
* Use {@link Sync#server(BoxStore, String, SyncCredentials)} instead.
7373
*/
7474
@Internal
75-
public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials authenticatorCredentials) {
75+
public SyncServerBuilder(BoxStore boxStore, String url, @Nullable SyncCredentials authenticatorCredentials) {
7676
checkNotNull(boxStore, "BoxStore is required.");
7777
checkNotNull(url, "Sync server URL is required.");
78-
checkNotNull(authenticatorCredentials, "Authenticator credentials are required.");
7978
checkFeatureSyncServerAvailable();
8079
this.boxStore = boxStore;
8180
try {
8281
this.url = new URI(url);
8382
} catch (URISyntaxException e) {
8483
throw new IllegalArgumentException("Sync server URL is invalid: " + url, e);
8584
}
86-
authenticatorCredentials(authenticatorCredentials);
85+
authenticatorCredentialsOrNull(authenticatorCredentials);
8786
}
8887

8988
/**
@@ -116,6 +115,18 @@ public SyncServerBuilder certificatePath(String certificatePath) {
116115
return this;
117116
}
118117

118+
private SyncServerBuilder authenticatorCredentialsOrNull(@Nullable SyncCredentials authenticatorCredentials) {
119+
if (authenticatorCredentials == null) {
120+
return this; // Do nothing
121+
}
122+
if (!(authenticatorCredentials instanceof SyncCredentialsToken)) {
123+
throw new IllegalArgumentException("Sync credentials of type " + authenticatorCredentials.getType()
124+
+ " are not supported");
125+
}
126+
credentials.add((SyncCredentialsToken) authenticatorCredentials);
127+
return this;
128+
}
129+
119130
/**
120131
* Adds additional authenticator credentials to authenticate clients or peers with.
121132
* <p>
@@ -124,12 +135,7 @@ public SyncServerBuilder certificatePath(String certificatePath) {
124135
*/
125136
public SyncServerBuilder authenticatorCredentials(SyncCredentials authenticatorCredentials) {
126137
checkNotNull(authenticatorCredentials, "Authenticator credentials must not be null.");
127-
if (!(authenticatorCredentials instanceof SyncCredentialsToken)) {
128-
throw new IllegalArgumentException("Sync credentials of type " + authenticatorCredentials.getType()
129-
+ " are not supported");
130-
}
131-
credentials.add((SyncCredentialsToken) authenticatorCredentials);
132-
return this;
138+
return authenticatorCredentialsOrNull(authenticatorCredentials);
133139
}
134140

135141
/**
@@ -316,7 +322,7 @@ private boolean hasJwtConfig() {
316322
* Note: this clears all previously set authenticator credentials.
317323
*/
318324
public SyncServer build() {
319-
if (credentials.isEmpty()) {
325+
if (!hasJwtConfig() && credentials.isEmpty()) {
320326
throw new IllegalStateException("At least one authenticator is required.");
321327
}
322328
if (hasJwtConfig()) {
@@ -368,7 +374,10 @@ byte[] buildSyncServerOptions() {
368374
if (clusterId != null) {
369375
clusterIdOffset = fbb.createString(clusterId);
370376
}
371-
int authenticationMethodsOffset = buildAuthenticationMethods(fbb);
377+
int authenticationMethodsOffset = 0;
378+
if (!credentials.isEmpty()) {
379+
authenticationMethodsOffset = buildAuthenticationMethods(fbb);
380+
}
372381
int clusterPeersVectorOffset = buildClusterPeers(fbb);
373382
int jwtConfigOffset = 0;
374383
if (hasJwtConfig()) {
@@ -387,7 +396,9 @@ byte[] buildSyncServerOptions() {
387396
// After collecting all offsets, create options
388397
SyncServerOptions.startSyncServerOptions(fbb);
389398
SyncServerOptions.addUrl(fbb, urlOffset);
390-
SyncServerOptions.addAuthenticationMethods(fbb, authenticationMethodsOffset);
399+
if (authenticationMethodsOffset != 0) {
400+
SyncServerOptions.addAuthenticationMethods(fbb, authenticationMethodsOffset);
401+
}
391402
if (syncFlags != 0) {
392403
SyncServerOptions.addSyncFlags(fbb, syncFlags);
393404
}

0 commit comments

Comments
 (0)