Skip to content

Commit 1531350

Browse files
Sync creds: add user and password #99
To support credential classes with different types of properties change from named constructors to static methods that return subclasses.
1 parent 7772d3d commit 1531350

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

objectbox/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## latest
22

3+
* Add `SyncCredentials.userAndPassword()`.
4+
* Change `SyncCredentials` from constructors to static methods. This should not require any changes
5+
in your code.
6+
37
## 2.5.0 (2024-02-14)
48

59
* Support creating file-less in-memory databases, for example for caching or testing. To create one

objectbox/lib/internal.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ export 'src/native/store.dart' show InternalStoreAccess;
88
export 'src/relations/info.dart';
99
export 'src/relations/to_many.dart'
1010
show InternalToManyAccess, InternalToManyTestAccess;
11-
export 'src/sync.dart' show InternalSyncTestAccess;

objectbox/lib/src/native/sync.dart

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'dart:isolate';
55
import 'dart:typed_data';
66

77
import 'package:ffi/ffi.dart';
8-
import 'package:meta/meta.dart';
98

109
import '../common.dart';
1110
import '../util.dart';
@@ -16,32 +15,61 @@ import 'store.dart';
1615
/// Credentials used to authenticate a sync client against a server.
1716
class SyncCredentials {
1817
final int _type;
19-
final Uint8List _data;
2018

21-
SyncCredentials._(this._type, String data)
22-
: _data = Uint8List.fromList(utf8.encode(data));
19+
SyncCredentials._(this._type);
2320

2421
/// No credentials - usually only for development purposes with a server
2522
/// configured to accept all connections without authentication.
26-
SyncCredentials.none()
27-
: _type = OBXSyncCredentialsType.NONE,
28-
_data = Uint8List(0);
23+
static SyncCredentials none() => _SyncCredentialsNone._();
2924

3025
/// Shared secret authentication.
31-
SyncCredentials.sharedSecretUint8List(this._data)
32-
: _type = OBXSyncCredentialsType.SHARED_SECRET_SIPPED;
26+
static SyncCredentials sharedSecretUint8List(Uint8List data) =>
27+
SyncCredentialsSecret._(
28+
OBXSyncCredentialsType.SHARED_SECRET_SIPPED, data);
3329

3430
/// Shared secret authentication.
35-
SyncCredentials.sharedSecretString(String data)
36-
: this._(OBXSyncCredentialsType.SHARED_SECRET_SIPPED, data);
31+
static SyncCredentials sharedSecretString(String data) =>
32+
SyncCredentialsSecret._encode(
33+
OBXSyncCredentialsType.SHARED_SECRET_SIPPED, data);
3734

3835
/// Google authentication.
39-
SyncCredentials.googleAuthUint8List(this._data)
40-
: _type = OBXSyncCredentialsType.GOOGLE_AUTH;
36+
static SyncCredentials googleAuthUint8List(Uint8List data) =>
37+
SyncCredentialsSecret._(OBXSyncCredentialsType.GOOGLE_AUTH, data);
4138

4239
/// Google authentication.
43-
SyncCredentials.googleAuthString(String data)
44-
: this._(OBXSyncCredentialsType.GOOGLE_AUTH, data);
40+
static SyncCredentials googleAuthString(String data) =>
41+
SyncCredentialsSecret._encode(OBXSyncCredentialsType.GOOGLE_AUTH, data);
42+
43+
/// Username and password authentication.
44+
static SyncCredentials userAndPassword(String user, String password) =>
45+
_SyncCredentialsUserPassword._(
46+
OBXSyncCredentialsType.USER_PASSWORD, user, password);
47+
}
48+
49+
class _SyncCredentialsNone extends SyncCredentials {
50+
_SyncCredentialsNone._() : super._(OBXSyncCredentialsType.NONE);
51+
}
52+
53+
/// Do not export, internal use only.
54+
///
55+
/// Sync credential that is a single secret string.
56+
class SyncCredentialsSecret extends SyncCredentials {
57+
/// UTF-8 encoded string.
58+
final Uint8List data;
59+
60+
SyncCredentialsSecret._(super.type, this.data) : super._();
61+
62+
SyncCredentialsSecret._encode(super.type, String data)
63+
: data = Uint8List.fromList(utf8.encode(data)),
64+
super._();
65+
}
66+
67+
class _SyncCredentialsUserPassword extends SyncCredentials {
68+
final String _user;
69+
final String _password;
70+
71+
_SyncCredentialsUserPassword._(super._type, this._user, this._password)
72+
: super._();
4573
}
4674

4775
/// Current state of the [SyncClient].
@@ -205,11 +233,18 @@ class SyncClient {
205233

206234
/// Configure authentication credentials, depending on your server config.
207235
void setCredentials(SyncCredentials creds) {
208-
if (creds._type == OBXSyncCredentialsType.NONE) {
236+
if (creds is _SyncCredentialsNone) {
209237
checkObx(C.sync_credentials(_ptr, creds._type, nullptr, 0));
210-
} else {
238+
} else if (creds is _SyncCredentialsUserPassword) {
239+
withNativeString(
240+
creds._user,
241+
(userCStr) => withNativeString(
242+
creds._password,
243+
(passwordCStr) => C.sync_credentials_user_password(
244+
_ptr, creds._type, userCStr, passwordCStr)));
245+
} else if (creds is SyncCredentialsSecret) {
211246
withNativeBytes(
212-
creds._data,
247+
creds.data,
213248
(Pointer<Uint8> credsPtr, int credsSize) => checkObx(
214249
C.sync_credentials(_ptr, creds._type, credsPtr, credsSize)));
215250
}
@@ -583,10 +618,3 @@ class Sync {
583618
return client;
584619
}
585620
}
586-
587-
/// Tests only.
588-
@visibleForTesting
589-
class InternalSyncTestAccess {
590-
/// Access credentials internal data representation.
591-
static Uint8List credentialsData(SyncCredentials creds) => creds._data;
592-
}

objectbox_test/test/sync_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:typed_data';
44

55
import 'package:objectbox/internal.dart';
66
import 'package:objectbox/src/native/store.dart';
7+
import 'package:objectbox/src/native/sync.dart';
78
import 'package:test/test.dart';
89

910
import 'entity.dart';
@@ -63,8 +64,7 @@ void main() {
6364
// an utf-8 encoded json file (i.e. the usual way).
6465
final str = 'uũú';
6566
expect(
66-
InternalSyncTestAccess.credentialsData(
67-
SyncCredentials.sharedSecretString(str)),
67+
(SyncCredentials.sharedSecretString(str) as SyncCredentialsSecret).data,
6868
equals(Uint8List.fromList([117, 197, 169, 195, 186])));
6969
});
7070

@@ -159,6 +159,7 @@ void main() {
159159
SyncCredentials.googleAuthUint8List(Uint8List.fromList([13, 0, 25])));
160160
c.setCredentials(SyncCredentials.sharedSecretUint8List(
161161
Uint8List.fromList([13, 0, 25])));
162+
c.setCredentials(SyncCredentials.userAndPassword('obx', 'secret'));
162163
c.setCredentials(SyncCredentials.none());
163164
c.setRequestUpdatesMode(SyncRequestUpdatesMode.manual);
164165
c.start();

0 commit comments

Comments
 (0)