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