1
1
/*
2
- * Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
2
+ * Copyright 2019-2025 ObjectBox Ltd. All rights reserved.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -55,10 +55,10 @@ public final class SyncServerBuilder {
55
55
private int syncServerFlags ;
56
56
private int workerThreads ;
57
57
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 ;
62
62
63
63
private static void checkFeatureSyncServerAvailable () {
64
64
if (!BoxStore .isSyncServerAvailable ()) {
@@ -273,39 +273,43 @@ public SyncServerBuilder workerThreads(int workerThreads) {
273
273
}
274
274
275
275
/**
276
- * Set the public key used to verify JWT tokens.
276
+ * Sets the public key used to verify JWT tokens.
277
277
* <p>
278
278
* The public key should be in the PEM format.
279
279
*/
280
280
public SyncServerBuilder jwtConfigPublicKey (String publicKey ) {
281
- this .publicKey = publicKey ;
281
+ this .jwtPublicKey = publicKey ;
282
282
return this ;
283
283
}
284
284
285
285
/**
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.
287
287
*/
288
288
public SyncServerBuilder jwtConfigPublicKeyUrl (String publicKeyUrl ) {
289
- this .publicKeyUrl = publicKeyUrl ;
289
+ this .jwtPublicKeyUrl = publicKeyUrl ;
290
290
return this ;
291
291
}
292
292
293
293
/**
294
- * Set the JWT claim "iss" (issuer) used to verify JWT tokens.
294
+ * Sets the JWT claim "iss" (issuer) used to verify JWT tokens.
295
295
*/
296
296
public SyncServerBuilder jwtConfigClaimIss (String claimIss ) {
297
- this .claimIss = claimIss ;
297
+ this .jwtClaimIss = claimIss ;
298
298
return this ;
299
299
}
300
300
301
301
/**
302
- * Set the JWT claim "aud" (audience) used to verify JWT tokens.
302
+ * Sets the JWT claim "aud" (audience) used to verify JWT tokens.
303
303
*/
304
304
public SyncServerBuilder jwtConfigClaimAud (String claimAud ) {
305
- this .claimAud = claimAud ;
305
+ this .jwtClaimAud = claimAud ;
306
306
return this ;
307
307
}
308
308
309
+ private boolean hasJwtConfig () {
310
+ return jwtPublicKey != null || jwtPublicKeyUrl != null ;
311
+ }
312
+
309
313
/**
310
314
* Builds and returns a Sync server ready to {@link SyncServer#start()}.
311
315
* <p>
@@ -315,6 +319,14 @@ public SyncServer build() {
315
319
if (credentials .isEmpty ()) {
316
320
throw new IllegalStateException ("At least one authenticator is required." );
317
321
}
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
+ }
318
330
if (!clusterPeers .isEmpty () || clusterFlags != 0 ) {
319
331
checkNotNull (clusterId , "Cluster ID must be set to use cluster features." );
320
332
}
@@ -359,14 +371,8 @@ byte[] buildSyncServerOptions() {
359
371
int authenticationMethodsOffset = buildAuthenticationMethods (fbb );
360
372
int clusterPeersVectorOffset = buildClusterPeers (fbb );
361
373
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 );
370
376
}
371
377
// Clear credentials immediately to make abuse less likely,
372
378
// but only after setting all options to allow (re-)using the same credentials object
0 commit comments