Skip to content

Commit 3e7c062

Browse files
authored
replacing Auth by C++ auth implementation (firebase#802)
* lazy replacing FST(Firebase)CredentialsProvider by (Firebase)CredentialsProvider * lazy replacing FSTUser by User * adding error-code parameter to TokenListener * actually use const user& instead of pointer; also add an error util * add HashUser and pass into the unordered_map * use User in test * use c++ CredentialsProvider and subclass in test * fix unit test * use explicit capture in lambda instead of capture all by reference * cache currentUser explicitly when reset sync engineer test driver * objc object should be captured by value in lambda * replacing Auth/FSTUser by C++ auth implementation * address changes * replacing FSTGetTokenResult by C++ Token implementation * address changes * fix unintentional change in merging * patch the change in objc Auth up-stream * somehow, the lambda-version of set-user-change-listener does not work... fallback to block * address changes * fix another const& v.s. dispatch bug * fix more const& v.s. dispatch bug zxu123 committed * fix a bad sync line * address changes * address change * address change * fix upstream change from merge * fix upstream changes * Suggested fixes for cpp/port_auth (firebase#846) * Get rid of MockDatastore factory This avoids the need to statically allocate (and leak) a credentials provider * Use absl::make_unique std::make_unique technically does not exist until C++14. * #include <utility> for std::move * Use std::future for the initial user * fix style
1 parent 13aeb61 commit 3e7c062

26 files changed

+360
-470
lines changed

Firestore/Example/Tests/Integration/FSTDatastoreTests.mm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
2525
#import "Firestore/Source/API/FSTUserDataConverter.h"
26-
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
2726
#import "Firestore/Source/Core/FSTFirestoreClient.h"
2827
#import "Firestore/Source/Core/FSTQuery.h"
2928
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
@@ -41,11 +40,13 @@
4140

4241
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
4342

43+
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
4444
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
4545
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
4646
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
4747

4848
namespace util = firebase::firestore::util;
49+
using firebase::firestore::auth::EmptyCredentialsProvider;
4950
using firebase::firestore::core::DatabaseInfo;
5051
using firebase::firestore::model::DatabaseId;
5152

@@ -141,7 +142,7 @@ @interface FSTDatastoreTests : XCTestCase
141142
@implementation FSTDatastoreTests {
142143
FSTDispatchQueue *_testWorkerQueue;
143144
FSTLocalStore *_localStore;
144-
id<FSTCredentialsProvider> _credentials;
145+
EmptyCredentialsProvider _credentials;
145146

146147
DatabaseInfo _databaseInfo;
147148
FSTDatastore *_datastore;
@@ -170,11 +171,9 @@ - (void)setUp {
170171
queueWith:dispatch_queue_create("com.google.firestore.FSTDatastoreTestsWorkerQueue",
171172
DISPATCH_QUEUE_SERIAL)];
172173

173-
_credentials = [[FSTEmptyCredentialsProvider alloc] init];
174-
175174
_datastore = [FSTDatastore datastoreWithDatabase:&_databaseInfo
176175
workerDispatchQueue:_testWorkerQueue
177-
credentials:_credentials];
176+
credentials:&_credentials];
178177

179178
_remoteStore = [FSTRemoteStore remoteStoreWithLocalStore:_localStore datastore:_datastore];
180179

Firestore/Example/Tests/Integration/FSTStreamTests.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222

2323
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2424
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
25-
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
2625
#import "Firestore/Source/Remote/FSTDatastore.h"
2726
#import "Firestore/Source/Remote/FSTStream.h"
2827
#import "Firestore/Source/Util/FSTAssert.h"
2928

29+
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
3030
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
3131
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
3232
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
3333

3434
namespace util = firebase::firestore::util;
35+
using firebase::firestore::auth::EmptyCredentialsProvider;
3536
using firebase::firestore::core::DatabaseInfo;
3637
using firebase::firestore::model::DatabaseId;
3738

@@ -136,7 +137,7 @@ @implementation FSTStreamTests {
136137
dispatch_queue_t _testQueue;
137138
FSTDispatchQueue *_workerDispatchQueue;
138139
DatabaseInfo _databaseInfo;
139-
FSTEmptyCredentialsProvider *_credentials;
140+
EmptyCredentialsProvider _credentials;
140141
FSTStreamStatusDelegate *_delegate;
141142

142143
/** Single mutation to send to the write stream. */
@@ -155,7 +156,6 @@ DatabaseId database_id(util::MakeStringView([FSTIntegrationTestCase projectID]),
155156

156157
_databaseInfo = DatabaseInfo(database_id, "test-key", util::MakeStringView(settings.host),
157158
settings.sslEnabled);
158-
_credentials = [[FSTEmptyCredentialsProvider alloc] init];
159159

160160
_delegate = [[FSTStreamStatusDelegate alloc] initWithTestCase:self queue:_workerDispatchQueue];
161161

@@ -165,14 +165,14 @@ DatabaseId database_id(util::MakeStringView([FSTIntegrationTestCase projectID]),
165165
- (FSTWriteStream *)setUpWriteStream {
166166
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:&_databaseInfo
167167
workerDispatchQueue:_workerDispatchQueue
168-
credentials:_credentials];
168+
credentials:&_credentials];
169169
return [datastore createWriteStream];
170170
}
171171

172172
- (FSTWatchStream *)setUpWatchStream {
173173
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:&_databaseInfo
174174
workerDispatchQueue:_workerDispatchQueue
175-
credentials:_credentials];
175+
credentials:&_credentials];
176176
return [datastore createWatchStream];
177177
}
178178

Firestore/Example/Tests/SpecTests/FSTMockDatastore.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ NS_ASSUME_NONNULL_BEGIN
3434
*/
3535
@property(nonatomic) int writeStreamRequestCount;
3636

37-
+ (instancetype)mockDatastoreWithWorkerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue;
38-
3937
#pragma mark - Watch Stream manipulation.
4038

4139
/** Injects an Added WatchChange containing the given targetIDs. */

Firestore/Example/Tests/SpecTests/FSTMockDatastore.mm

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#import "Firestore/Example/Tests/SpecTests/FSTMockDatastore.h"
1818

19-
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
2019
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
2120
#import "Firestore/Source/Local/FSTQueryData.h"
2221
#import "Firestore/Source/Model/FSTMutation.h"
@@ -27,10 +26,14 @@
2726

2827
#import "Firestore/Example/Tests/Remote/FSTWatchChange+Testing.h"
2928

29+
#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
30+
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
3031
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
3132
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
3233
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
3334

35+
using firebase::firestore::auth::CredentialsProvider;
36+
using firebase::firestore::auth::EmptyCredentialsProvider;
3437
using firebase::firestore::core::DatabaseInfo;
3538
using firebase::firestore::model::DatabaseId;
3639

@@ -44,17 +47,17 @@ @interface FSTMockWatchStream : FSTWatchStream
4447

4548
- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
4649
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
47-
credentials:(id<FSTCredentialsProvider>)credentials
50+
credentials:(CredentialsProvider *)credentials
4851
serializer:(FSTSerializerBeta *)serializer NS_DESIGNATED_INITIALIZER;
4952

5053
- (instancetype)initWithDatabase:(const DatabaseInfo *)database
5154
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
52-
credentials:(id<FSTCredentialsProvider>)credentials
55+
credentials:(CredentialsProvider *)credentials
5356
serializer:(FSTSerializerBeta *)serializer NS_UNAVAILABLE;
5457

5558
- (instancetype)initWithDatabase:(const DatabaseInfo *)database
5659
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
57-
credentials:(id<FSTCredentialsProvider>)credentials
60+
credentials:(CredentialsProvider *)credentials
5861
responseMessageClass:(Class)responseMessageClass NS_UNAVAILABLE;
5962

6063
@property(nonatomic, assign) BOOL open;
@@ -69,7 +72,7 @@ @implementation FSTMockWatchStream
6972

7073
- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
7174
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
72-
credentials:(id<FSTCredentialsProvider>)credentials
75+
credentials:(CredentialsProvider *)credentials
7376
serializer:(FSTSerializerBeta *)serializer {
7477
self = [super initWithDatabase:datastore.databaseInfo
7578
workerDispatchQueue:workerDispatchQueue
@@ -170,17 +173,17 @@ @interface FSTMockWriteStream : FSTWriteStream
170173

171174
- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
172175
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
173-
credentials:(id<FSTCredentialsProvider>)credentials
176+
credentials:(CredentialsProvider *)credentials
174177
serializer:(FSTSerializerBeta *)serializer NS_DESIGNATED_INITIALIZER;
175178

176179
- (instancetype)initWithDatabase:(const DatabaseInfo *)database
177180
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
178-
credentials:(id<FSTCredentialsProvider>)credentials
181+
credentials:(CredentialsProvider *)credentials
179182
serializer:(FSTSerializerBeta *)serializer NS_UNAVAILABLE;
180183

181184
- (instancetype)initWithDatabase:(const DatabaseInfo *)database
182185
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
183-
credentials:(id<FSTCredentialsProvider>)credentials
186+
credentials:(CredentialsProvider *)credentials
184187
responseMessageClass:(Class)responseMessageClass NS_UNAVAILABLE;
185188

186189
@property(nonatomic, strong, readonly) FSTMockDatastore *datastore;
@@ -193,7 +196,7 @@ @implementation FSTMockWriteStream
193196

194197
- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
195198
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
196-
credentials:(id<FSTCredentialsProvider>)credentials
199+
credentials:(CredentialsProvider *)credentials
197200
serializer:(FSTSerializerBeta *)serializer {
198201
self = [super initWithDatabase:datastore.databaseInfo
199202
workerDispatchQueue:workerDispatchQueue
@@ -279,24 +282,12 @@ @interface FSTMockDatastore ()
279282

280283
/** Properties implemented in FSTDatastore that are nonpublic. */
281284
@property(nonatomic, strong, readonly) FSTDispatchQueue *workerDispatchQueue;
282-
@property(nonatomic, strong, readonly) id<FSTCredentialsProvider> credentials;
285+
@property(nonatomic, assign, readonly) CredentialsProvider *credentials;
283286

284287
@end
285288

286289
@implementation FSTMockDatastore
287290

288-
+ (instancetype)mockDatastoreWithWorkerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue {
289-
// This owns the DatabaseInfos since we do not have FirestoreClient instance to own them.
290-
static DatabaseInfo database_info{DatabaseId{"project", "database"}, "persistence", "host",
291-
false};
292-
293-
FSTEmptyCredentialsProvider *credentials = [[FSTEmptyCredentialsProvider alloc] init];
294-
295-
return [[FSTMockDatastore alloc] initWithDatabaseInfo:&database_info
296-
workerDispatchQueue:workerDispatchQueue
297-
credentials:credentials];
298-
}
299-
300291
#pragma mark - Overridden FSTDatastore methods.
301292

302293
- (FSTWatchStream *)createWatchStream {

Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.mm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@
3737
#import "Firestore/Example/Tests/Core/FSTSyncEngine+Testing.h"
3838
#import "Firestore/Example/Tests/SpecTests/FSTMockDatastore.h"
3939

40+
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
4041
#include "Firestore/core/src/firebase/firestore/auth/user.h"
42+
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
43+
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
4144

45+
using firebase::firestore::auth::EmptyCredentialsProvider;
4246
using firebase::firestore::auth::HashUser;
4347
using firebase::firestore::auth::User;
48+
using firebase::firestore::core::DatabaseInfo;
49+
using firebase::firestore::model::DatabaseId;
4450

4551
NS_ASSUME_NONNULL_BEGIN
4652

@@ -83,7 +89,9 @@ @implementation FSTSyncEngineTestDriver {
8389
// ivar is declared as mutable.
8490
std::unordered_map<User, NSMutableArray<FSTOutstandingWrite *> *, HashUser> _outstandingWrites;
8591

92+
DatabaseInfo _databaseInfo;
8693
User _currentUser;
94+
EmptyCredentialsProvider _credentialProvider;
8795
}
8896

8997
- (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
@@ -106,13 +114,17 @@ - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
106114

107115
_events = [NSMutableArray array];
108116

117+
_databaseInfo = {DatabaseId{"project", "database"}, "persistence", "host", false};
118+
109119
// Set up the sync engine and various stores.
110120
dispatch_queue_t mainQueue = dispatch_get_main_queue();
111121
FSTDispatchQueue *dispatchQueue = [FSTDispatchQueue queueWith:mainQueue];
112122
_localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
113123
garbageCollector:garbageCollector
114124
initialUser:initialUser];
115-
_datastore = [FSTMockDatastore mockDatastoreWithWorkerDispatchQueue:dispatchQueue];
125+
_datastore = [[FSTMockDatastore alloc] initWithDatabaseInfo:&_databaseInfo
126+
workerDispatchQueue:dispatchQueue
127+
credentials:&_credentialProvider];
116128

117129
_remoteStore = [FSTRemoteStore remoteStoreWithLocalStore:_localStore datastore:_datastore];
118130

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@
1616

1717
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
1818

19+
#include <memory>
20+
#include <utility>
21+
1922
#import <FirebaseCore/FIRLogger.h>
2023
#import <FirebaseFirestore/FirebaseFirestore-umbrella.h>
2124
#import <GRPCClient/GRPCCall+ChannelArg.h>
2225
#import <GRPCClient/GRPCCall+Tests.h>
2326

27+
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
28+
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
2429
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
30+
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
31+
#include "absl/memory/memory.h"
2532

2633
#import "Firestore/Source/API/FIRFirestore+Internal.h"
27-
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
2834
#import "Firestore/Source/Core/FSTFirestoreClient.h"
2935
#import "Firestore/Source/Local/FSTLevelDB.h"
3036
#import "Firestore/Source/Util/FSTDispatchQueue.h"
3137

3238
#import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
3339

34-
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
35-
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
36-
3740
namespace util = firebase::firestore::util;
41+
using firebase::firestore::auth::CredentialsProvider;
42+
using firebase::firestore::auth::EmptyCredentialsProvider;
3843
using firebase::firestore::model::DatabaseId;
3944
using firebase::firestore::util::CreateAutoId;
4045

@@ -135,15 +140,16 @@ - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
135140
FSTDispatchQueue *workerDispatchQueue = [FSTDispatchQueue
136141
queueWith:dispatch_queue_create("com.google.firebase.firestore", DISPATCH_QUEUE_SERIAL)];
137142

138-
FSTEmptyCredentialsProvider *credentialsProvider = [[FSTEmptyCredentialsProvider alloc] init];
139-
140143
FIRSetLoggerLevel(FIRLoggerLevelDebug);
141144
// HACK: FIRFirestore expects a non-nil app, but for tests we cheat.
142145
FIRApp *app = nil;
146+
std::unique_ptr<CredentialsProvider> credentials_provider =
147+
absl::make_unique<firebase::firestore::auth::EmptyCredentialsProvider>();
148+
143149
FIRFirestore *firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeStringView(projectID)
144150
database:DatabaseId::kDefault
145151
persistenceKey:persistenceKey
146-
credentialsProvider:credentialsProvider
152+
credentialsProvider:std::move(credentials_provider)
147153
workerDispatchQueue:workerDispatchQueue
148154
firebaseApp:app];
149155

Firestore/Source/API/FIRFirestore+Internal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#import "FIRFirestore.h"
1818

19+
#include <memory>
20+
21+
#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
1922
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
2023
#include "absl/strings/string_view.h"
2124

@@ -24,7 +27,6 @@ NS_ASSUME_NONNULL_BEGIN
2427
@class FSTDispatchQueue;
2528
@class FSTFirestoreClient;
2629
@class FSTUserDataConverter;
27-
@protocol FSTCredentialsProvider;
2830

2931
@interface FIRFirestore (/* Init */)
3032

@@ -35,7 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
3537
- (instancetype)initWithProjectID:(const absl::string_view)projectID
3638
database:(const absl::string_view)database
3739
persistenceKey:(NSString *)persistenceKey
38-
credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
40+
credentialsProvider:(std::unique_ptr<firebase::firestore::auth::CredentialsProvider>)
41+
credentialsProvider
3942
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
4043
firebaseApp:(FIRApp *)app;
4144

0 commit comments

Comments
 (0)