@@ -5,7 +5,6 @@ import 'dart:isolate';
55import 'dart:typed_data' ;
66
77import 'package:ffi/ffi.dart' ;
8- import 'package:meta/meta.dart' ;
98
109import '../common.dart' ;
1110import '../util.dart' ;
@@ -16,32 +15,61 @@ import 'store.dart';
1615/// Credentials used to authenticate a sync client against a server.
1716class 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 ;
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 , 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- }
0 commit comments