Skip to content

Commit ffbc9c6

Browse files
author
Shubham
committed
sync: add methods to initialize SyncBuilder with multiple auth credentials #252
1 parent 4926ccf commit ffbc9c6

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCreden
9393
return new SyncServerBuilder(boxStore, url, authenticatorCredentials);
9494
}
9595

96+
/**
97+
* Starts building a {@link SyncServer}. Once done, complete with {@link SyncServerBuilder#build() build()}.
98+
* <p>
99+
* Note: when also using Admin, make sure it is started before the server.
100+
*
101+
* @param boxStore The {@link BoxStore} the server should use.
102+
* @param url The URL of the Sync server on which the Sync protocol is exposed. This is typically a WebSockets URL
103+
* starting with {@code ws://} or {@code wss://} (for encrypted connections), for example
104+
* {@code ws://0.0.0.0:9999}.
105+
* @param multipleAuthenticatorCredentials An authentication method available to Sync clients and peers. Additional
106+
* authenticator credentials can be supplied using the returned builder. For the embedded server, currently only
107+
* {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none} are supported.
108+
*/
109+
public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCredentials[] multipleAuthenticatorCredentials) {
110+
return new SyncServerBuilder(boxStore, url, multipleAuthenticatorCredentials);
111+
}
112+
96113
/**
97114
* Starts building a {@link SyncHybrid}, a client/server hybrid typically used for embedded cluster setups.
98115
* <p>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public interface SyncClient extends Closeable {
133133
*/
134134
void setLoginCredentials(SyncCredentials credentials);
135135

136+
/**
137+
* Updates the login credentials. This should not be required during regular use.
138+
* It allows passing login credentials that the client can use to authenticate with the server.
139+
*/
136140
void setLoginCredentials(SyncCredentials[] multipleCredentials);
137141

138142
/**

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials authenti
8181
authenticatorCredentials(authenticatorCredentials);
8282
}
8383

84+
/**
85+
* Use {@link Sync#server(BoxStore, String, SyncCredentials)} instead.
86+
*/
87+
@Internal
88+
public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials[] multipleAuthenticatorCredentials) {
89+
checkNotNull(boxStore, "BoxStore is required.");
90+
checkNotNull(url, "Sync server URL is required.");
91+
checkNotNull(multipleAuthenticatorCredentials, "Authenticator credentials are required.");
92+
if (!BoxStore.isSyncServerAvailable()) {
93+
throw new IllegalStateException(
94+
"This library does not include ObjectBox Sync Server. " +
95+
"Please visit https://objectbox.io/sync/ for options.");
96+
}
97+
this.boxStore = boxStore;
98+
try {
99+
this.url = new URI(url);
100+
} catch (URISyntaxException e) {
101+
throw new IllegalArgumentException("Sync server URL is invalid: " + url, e);
102+
}
103+
authenticatorCredentials(multipleAuthenticatorCredentials);
104+
}
105+
84106
/**
85107
* Sets the path to a directory that contains a cert.pem and key.pem file to use to establish encrypted
86108
* connections.
@@ -109,6 +131,20 @@ public SyncServerBuilder authenticatorCredentials(SyncCredentials authenticatorC
109131
return this;
110132
}
111133

134+
/**
135+
* Adds additional authenticator credentials to authenticate clients or peers with.
136+
* <p>
137+
* For the embedded server, currently only {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none}
138+
* are supported.
139+
*/
140+
public SyncServerBuilder authenticatorCredentials(SyncCredentials[] multipleAuthenticatorCredentials) {
141+
checkNotNull(multipleAuthenticatorCredentials, "Authenticator credentials must not be null.");
142+
for (SyncCredentials credentials : multipleAuthenticatorCredentials) {
143+
authenticatorCredentials(credentials);
144+
}
145+
return this;
146+
}
147+
112148
/**
113149
* Sets a listener to observe fine granular changes happening during sync.
114150
* <p>

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ public final class SyncServerOptions extends Table {
129129
*/
130130
public io.objectbox.sync.server.JwtConfig jwtConfig() { return jwtConfig(new io.objectbox.sync.server.JwtConfig()); }
131131
public io.objectbox.sync.server.JwtConfig jwtConfig(io.objectbox.sync.server.JwtConfig obj) { int o = __offset(30); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
132+
/**
133+
* Credential types that are required for clients logging in.
134+
*/
135+
public long requiredCredentials(int j) { int o = __offset(32); return o != 0 ? (long)bb.getInt(__vector(o) + j * 4) & 0xFFFFFFFFL : 0; }
136+
public int requiredCredentialsLength() { int o = __offset(32); return o != 0 ? __vector_len(o) : 0; }
137+
public IntVector requiredCredentialsVector() { return requiredCredentialsVector(new IntVector()); }
138+
public IntVector requiredCredentialsVector(IntVector obj) { int o = __offset(32); return o != 0 ? obj.__assign(__vector(o), bb) : null; }
139+
public ByteBuffer requiredCredentialsAsByteBuffer() { return __vector_as_bytebuffer(32, 4); }
140+
public ByteBuffer requiredCredentialsInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 32, 4); }
132141

133142
public static int createSyncServerOptions(FlatBufferBuilder builder,
134143
int urlOffset,
@@ -144,10 +153,12 @@ public static int createSyncServerOptions(FlatBufferBuilder builder,
144153
int clusterIdOffset,
145154
int clusterPeersOffset,
146155
long clusterFlags,
147-
int jwtConfigOffset) {
148-
builder.startTable(14);
156+
int jwtConfigOffset,
157+
int requiredCredentialsOffset) {
158+
builder.startTable(15);
149159
SyncServerOptions.addHistorySizeTargetKb(builder, historySizeTargetKb);
150160
SyncServerOptions.addHistorySizeMaxKb(builder, historySizeMaxKb);
161+
SyncServerOptions.addRequiredCredentials(builder, requiredCredentialsOffset);
151162
SyncServerOptions.addJwtConfig(builder, jwtConfigOffset);
152163
SyncServerOptions.addClusterFlags(builder, clusterFlags);
153164
SyncServerOptions.addClusterPeers(builder, clusterPeersOffset);
@@ -163,7 +174,7 @@ public static int createSyncServerOptions(FlatBufferBuilder builder,
163174
return SyncServerOptions.endSyncServerOptions(builder);
164175
}
165176

166-
public static void startSyncServerOptions(FlatBufferBuilder builder) { builder.startTable(14); }
177+
public static void startSyncServerOptions(FlatBufferBuilder builder) { builder.startTable(15); }
167178
public static void addUrl(FlatBufferBuilder builder, int urlOffset) { builder.addOffset(0, urlOffset, 0); }
168179
public static void addAuthenticationMethods(FlatBufferBuilder builder, int authenticationMethodsOffset) { builder.addOffset(1, authenticationMethodsOffset, 0); }
169180
public static int createAuthenticationMethodsVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
@@ -182,6 +193,9 @@ public static int createSyncServerOptions(FlatBufferBuilder builder,
182193
public static void startClusterPeersVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
183194
public static void addClusterFlags(FlatBufferBuilder builder, long clusterFlags) { builder.addInt(12, (int) clusterFlags, (int) 0L); }
184195
public static void addJwtConfig(FlatBufferBuilder builder, int jwtConfigOffset) { builder.addOffset(13, jwtConfigOffset, 0); }
196+
public static void addRequiredCredentials(FlatBufferBuilder builder, int requiredCredentialsOffset) { builder.addOffset(14, requiredCredentialsOffset, 0); }
197+
public static int createRequiredCredentialsVector(FlatBufferBuilder builder, long[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addInt((int) data[i]); return builder.endVector(); }
198+
public static void startRequiredCredentialsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
185199
public static int endSyncServerOptions(FlatBufferBuilder builder) {
186200
int o = builder.endTable();
187201
return o;

0 commit comments

Comments
 (0)