Skip to content

Commit e67b3ab

Browse files
SyncServer: simplify docs, improve JWT options naming and checks #252
1 parent 3085052 commit e67b3ab

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2019-2025 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,17 +90,8 @@ public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCreden
9090
}
9191

9292
/**
93-
* Starts building a {@link SyncServer}. Once done, complete with {@link SyncServerBuilder#build() build()}.
94-
* <p>
95-
* Note: when also using Admin, make sure it is started before the server.
96-
*
97-
* @param boxStore The {@link BoxStore} the server should use.
98-
* @param url The URL of the Sync server on which the Sync protocol is exposed. This is typically a WebSockets URL
99-
* starting with {@code ws://} or {@code wss://} (for encrypted connections), for example
100-
* {@code ws://0.0.0.0:9999}.
101-
* @param multipleAuthenticatorCredentials An authentication method available to Sync clients and peers. Additional
102-
* authenticator credentials can be supplied using the returned builder. For the embedded server, currently only
103-
* {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none} are supported.
93+
* Like {@link #server(BoxStore, String, SyncCredentials)}, but supports passing a set of authentication methods
94+
* for clients and peers.
10495
*/
10596
public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCredentials[] multipleAuthenticatorCredentials) {
10697
return new SyncServerBuilder(boxStore, url, multipleAuthenticatorCredentials);

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2019-2025 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,10 +55,10 @@ public final class SyncServerBuilder {
5555
private int syncServerFlags;
5656
private int workerThreads;
5757

58-
private String publicKey;
59-
private String publicKeyUrl;
60-
private String claimIss;
61-
private String claimAud;
58+
private @Nullable String jwtPublicKey;
59+
private @Nullable String jwtPublicKeyUrl;
60+
private @Nullable String jwtClaimIss;
61+
private @Nullable String jwtClaimAud;
6262

6363
private static void checkFeatureSyncServerAvailable() {
6464
if (!BoxStore.isSyncServerAvailable()) {
@@ -273,39 +273,43 @@ public SyncServerBuilder workerThreads(int workerThreads) {
273273
}
274274

275275
/**
276-
* Set the public key used to verify JWT tokens.
276+
* Sets the public key used to verify JWT tokens.
277277
* <p>
278278
* The public key should be in the PEM format.
279279
*/
280280
public SyncServerBuilder jwtConfigPublicKey(String publicKey) {
281-
this.publicKey = publicKey;
281+
this.jwtPublicKey = publicKey;
282282
return this;
283283
}
284284

285285
/**
286-
* Set the JWKS (Json Web Key Sets) URL to fetch the current public key used to verify JWT tokens.
286+
* Sets the JWKS (Json Web Key Sets) URL to fetch the current public key used to verify JWT tokens.
287287
*/
288288
public SyncServerBuilder jwtConfigPublicKeyUrl(String publicKeyUrl) {
289-
this.publicKeyUrl = publicKeyUrl;
289+
this.jwtPublicKeyUrl = publicKeyUrl;
290290
return this;
291291
}
292292

293293
/**
294-
* Set the JWT claim "iss" (issuer) used to verify JWT tokens.
294+
* Sets the JWT claim "iss" (issuer) used to verify JWT tokens.
295295
*/
296296
public SyncServerBuilder jwtConfigClaimIss(String claimIss) {
297-
this.claimIss = claimIss;
297+
this.jwtClaimIss = claimIss;
298298
return this;
299299
}
300300

301301
/**
302-
* Set the JWT claim "aud" (audience) used to verify JWT tokens.
302+
* Sets the JWT claim "aud" (audience) used to verify JWT tokens.
303303
*/
304304
public SyncServerBuilder jwtConfigClaimAud(String claimAud) {
305-
this.claimAud = claimAud;
305+
this.jwtClaimAud = claimAud;
306306
return this;
307307
}
308308

309+
private boolean hasJwtConfig() {
310+
return jwtPublicKey != null || jwtPublicKeyUrl != null;
311+
}
312+
309313
/**
310314
* Builds and returns a Sync server ready to {@link SyncServer#start()}.
311315
* <p>
@@ -315,6 +319,14 @@ public SyncServer build() {
315319
if (credentials.isEmpty()) {
316320
throw new IllegalStateException("At least one authenticator is required.");
317321
}
322+
if (hasJwtConfig()) {
323+
if (jwtClaimAud == null) {
324+
throw new IllegalArgumentException("To use JWT authentication, claimAud must be set");
325+
}
326+
if (jwtClaimIss == null) {
327+
throw new IllegalArgumentException("To use JWT authentication, claimIss must be set");
328+
}
329+
}
318330
if (!clusterPeers.isEmpty() || clusterFlags != 0) {
319331
checkNotNull(clusterId, "Cluster ID must be set to use cluster features.");
320332
}
@@ -359,14 +371,8 @@ byte[] buildSyncServerOptions() {
359371
int authenticationMethodsOffset = buildAuthenticationMethods(fbb);
360372
int clusterPeersVectorOffset = buildClusterPeers(fbb);
361373
int jwtConfigOffset = 0;
362-
if (publicKey != null || publicKeyUrl != null) {
363-
if (claimAud == null) {
364-
throw new IllegalArgumentException("claimAud must be set");
365-
}
366-
if (claimIss == null) {
367-
throw new IllegalArgumentException("claimIss must be set");
368-
}
369-
jwtConfigOffset = buildJwtConfig(fbb, publicKey, publicKeyUrl, claimIss, claimAud);
374+
if (hasJwtConfig()) {
375+
jwtConfigOffset = buildJwtConfig(fbb, jwtPublicKey, jwtPublicKeyUrl, jwtClaimIss, jwtClaimAud);
370376
}
371377
// Clear credentials immediately to make abuse less likely,
372378
// but only after setting all options to allow (re-)using the same credentials object

0 commit comments

Comments
 (0)