Skip to content

Commit 11d04f6

Browse files
Sync creds: add user and password #202
Also move type up to super class.
1 parent 1968602 commit 11d04f6

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,17 @@ public void setSyncListener(@Nullable SyncListener listener) {
167167

168168
@Override
169169
public void setLoginCredentials(SyncCredentials credentials) {
170-
SyncCredentialsToken credentialsInternal = (SyncCredentialsToken) credentials;
171-
nativeSetLoginInfo(getHandle(), credentialsInternal.getTypeId(), credentialsInternal.getTokenBytes());
172-
credentialsInternal.clear(); // Clear immediately, not needed anymore.
170+
if (credentials instanceof SyncCredentialsToken) {
171+
SyncCredentialsToken credToken = (SyncCredentialsToken) credentials;
172+
nativeSetLoginInfo(getHandle(), credToken.getTypeId(), credToken.getTokenBytes());
173+
credToken.clear(); // Clear immediately, not needed anymore.
174+
} else if (credentials instanceof SyncCredentialsUserPassword) {
175+
SyncCredentialsUserPassword credUserPassword = (SyncCredentialsUserPassword) credentials;
176+
nativeSetLoginInfoUserPassword(getHandle(), credUserPassword.getTypeId(), credUserPassword.getUsername(),
177+
credUserPassword.getPassword());
178+
} else {
179+
throw new IllegalArgumentException("credentials is not a supported type");
180+
}
173181
}
174182

175183
@Override
@@ -296,6 +304,8 @@ public ObjectsMessageBuilder startObjectsMessage(long flags, @Nullable String to
296304

297305
private native void nativeSetLoginInfo(long handle, long credentialsType, @Nullable byte[] credentials);
298306

307+
private native void nativeSetLoginInfoUserPassword(long handle, long credentialsType, String username, String password);
308+
299309
private native void nativeSetListener(long handle, @Nullable InternalSyncClientListener listener);
300310

301311
private native void nativeSetSyncChangesListener(long handle, @Nullable SyncChangeListener advancedListener);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
@SuppressWarnings("unused")
88
public class SyncCredentials {
99

10+
private final CredentialsType type;
11+
1012
/**
1113
* Authenticate with a shared secret. This could be a passphrase, big number or randomly chosen bytes.
1214
* The string is expected to use UTF-8 characters.
@@ -30,6 +32,10 @@ public static SyncCredentials google(String idToken) {
3032
return new SyncCredentialsToken(CredentialsType.GOOGLE, idToken);
3133
}
3234

35+
public static SyncCredentials userAndPassword(String user, String password) {
36+
return new SyncCredentialsUserPassword(user, password);
37+
}
38+
3339
/**
3440
* No authentication, unsecured. Use only for development and testing purposes.
3541
*/
@@ -54,7 +60,12 @@ public enum CredentialsType {
5460
}
5561
}
5662

57-
SyncCredentials() {
63+
SyncCredentials(CredentialsType type) {
64+
this.type = type;
65+
}
66+
67+
public long getTypeId() {
68+
return type.id;
5869
}
5970

6071
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
* Internal credentials implementation. Use {@link SyncCredentials} to build credentials.
1212
*/
1313
@Internal
14-
public class SyncCredentialsToken extends SyncCredentials {
14+
public final class SyncCredentialsToken extends SyncCredentials {
1515

16-
private final CredentialsType type;
1716
@Nullable private byte[] token;
1817
private volatile boolean cleared;
1918

2019
SyncCredentialsToken(CredentialsType type) {
21-
this.type = type;
20+
super(type);
2221
this.token = null;
2322
}
2423

@@ -34,10 +33,6 @@ public class SyncCredentialsToken extends SyncCredentials {
3433
this(type, asUtf8Bytes(token));
3534
}
3635

37-
public long getTypeId() {
38-
return type.id;
39-
}
40-
4136
@Nullable
4237
public byte[] getTokenBytes() {
4338
if (cleared) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.objectbox.sync;
2+
3+
import io.objectbox.annotation.apihint.Internal;
4+
5+
/**
6+
* Internal credentials implementation for user and password authentication.
7+
* Use {@link SyncCredentials} to build credentials.
8+
*/
9+
@Internal
10+
public final class SyncCredentialsUserPassword extends SyncCredentials {
11+
12+
private final String username;
13+
private final String password;
14+
15+
SyncCredentialsUserPassword(String username, String password) {
16+
super(CredentialsType.USER_PASSWORD);
17+
this.username = username;
18+
this.password = password;
19+
}
20+
21+
public String getUsername() {
22+
return username;
23+
}
24+
25+
public String getPassword() {
26+
return password;
27+
}
28+
}

0 commit comments

Comments
 (0)