Skip to content

Commit d70c23e

Browse files
authored
port Firestore Auth module in C++ (firebase#733)
* Implement firestore/auth/user * add user to project and some fixes * implement firestore/auth/{credentials_provider,empty_credentials_provider} * implement firestore/auth/firebase_credentials_provider * refactoring firebase_credentials_provider and add (disabled but working) unit test * add auth test to project * address changes * small fix to style and project * fix the firebase_credentials_provider_test * fix style * address changes * revert the change to static mutex_ * remove my custom plist path * fix style * address changes * refactoring FirebaseCredentialsProvider to fix the issue w.r.t. auth global dispatch queue * add /*force_refresh=*/ tag to bool literal for style purpose * Use a shared_ptr/weak_ptr handoff on FirebaseCredentialsProvider (firebase#778) * Revert "refactoring FirebaseCredentialsProvider to fix the issue w.r.t. auth global dispatch queue" This reverts commit 87175a4. * Use a shared_ptr/weak_ptr handoff on FirebaseCredentialsProvider This avoids any problems with callsbacks retaining pointers to objects destroyed by a C++ destructor
1 parent 633eb7b commit d70c23e

20 files changed

+1063
-0
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@
141141
AB380D04201BC6E400D97691 /* ordered_code_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D03201BC6E400D97691 /* ordered_code_test.cc */; };
142142
AB38D93020236E21000A432D /* database_info_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D92E20235D22000A432D /* database_info_test.cc */; };
143143
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; };
144+
ABC1D7DC2023A04B00BA84F0 /* credentials_provider_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D9342023966E000A432D /* credentials_provider_test.cc */; };
145+
ABC1D7DD2023A04F00BA84F0 /* empty_credentials_provider_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D93620239689000A432D /* empty_credentials_provider_test.cc */; };
146+
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D93220239654000A432D /* user_test.cc */; };
147+
ABC1D7E12023A40C00BA84F0 /* token_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABC1D7DF2023A3EF00BA84F0 /* token_test.cc */; };
148+
ABC1D7E42024AFDE00BA84F0 /* firebase_credentials_provider_test.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABC1D7E22023CDC500BA84F0 /* firebase_credentials_provider_test.mm */; };
144149
ABE6637A201FA81900ED349A /* database_id_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB71064B201FA60300344F18 /* database_id_test.cc */; };
145150
ABF6506C201131F8005F2C74 /* timestamp_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABF6506B201131F8005F2C74 /* timestamp_test.cc */; };
146151
AFE6114F0D4DAECBA7B7C089 /* Pods_Firestore_IntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2FA635DF5D116A67A7441CD /* Pods_Firestore_IntegrationTests.framework */; };
@@ -336,8 +341,13 @@
336341
AB380D01201BC69F00D97691 /* bits_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = bits_test.cc; path = ../../core/test/firebase/firestore/util/bits_test.cc; sourceTree = "<group>"; };
337342
AB380D03201BC6E400D97691 /* ordered_code_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ordered_code_test.cc; path = ../../core/test/firebase/firestore/util/ordered_code_test.cc; sourceTree = "<group>"; };
338343
AB38D92E20235D22000A432D /* database_info_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = database_info_test.cc; sourceTree = "<group>"; };
344+
AB38D93220239654000A432D /* user_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = user_test.cc; sourceTree = "<group>"; };
345+
AB38D9342023966E000A432D /* credentials_provider_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = credentials_provider_test.cc; sourceTree = "<group>"; };
346+
AB38D93620239689000A432D /* empty_credentials_provider_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = empty_credentials_provider_test.cc; sourceTree = "<group>"; };
339347
AB71064B201FA60300344F18 /* database_id_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = database_id_test.cc; sourceTree = "<group>"; };
340348
AB7BAB332012B519001E0872 /* geo_point_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = geo_point_test.cc; path = ../../core/test/firebase/firestore/geo_point_test.cc; sourceTree = "<group>"; };
349+
ABC1D7DF2023A3EF00BA84F0 /* token_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = token_test.cc; sourceTree = "<group>"; };
350+
ABC1D7E22023CDC500BA84F0 /* firebase_credentials_provider_test.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = firebase_credentials_provider_test.mm; sourceTree = "<group>"; };
341351
ABF6506B201131F8005F2C74 /* timestamp_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = timestamp_test.cc; sourceTree = "<group>"; };
342352
B2FA635DF5D116A67A7441CD /* Pods_Firestore_IntegrationTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_IntegrationTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
343353
B686F2AD2023DDB20028D6BE /* field_path_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = field_path_test.cc; sourceTree = "<group>"; };
@@ -430,6 +440,7 @@
430440
54764FAC1FAA0C390085E60A /* GoogleTests */ = {
431441
isa = PBXGroup;
432442
children = (
443+
AB38D9312023962A000A432D /* auth */,
433444
AB380CF7201937B800D97691 /* core */,
434445
54EB764B202277970088B8F3 /* immutable */,
435446
AB356EF5200E9D1A0089B766 /* model */,
@@ -589,6 +600,19 @@
589600
path = ../../core/test/firebase/firestore/core;
590601
sourceTree = "<group>";
591602
};
603+
AB38D9312023962A000A432D /* auth */ = {
604+
isa = PBXGroup;
605+
children = (
606+
AB38D9342023966E000A432D /* credentials_provider_test.cc */,
607+
AB38D93620239689000A432D /* empty_credentials_provider_test.cc */,
608+
ABC1D7E22023CDC500BA84F0 /* firebase_credentials_provider_test.mm */,
609+
ABC1D7DF2023A3EF00BA84F0 /* token_test.cc */,
610+
AB38D93220239654000A432D /* user_test.cc */,
611+
);
612+
name = auth;
613+
path = ../../core/test/firebase/firestore/auth;
614+
sourceTree = "<group>";
615+
};
592616
DE0761E51F2FE611003233AF /* SwiftBuildTest */ = {
593617
isa = PBXGroup;
594618
children = (
@@ -1252,7 +1276,9 @@
12521276
5492E058202154AB00B64F25 /* FSTAPIHelpers.mm in Sources */,
12531277
AB380CFB2019388600D97691 /* target_id_generator_test.cc in Sources */,
12541278
5492E0A82021552D00B64F25 /* FSTLevelDBLocalStoreTests.mm in Sources */,
1279+
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */,
12551280
5491BC721FB44593008B3588 /* FSTIntegrationTestCase.mm in Sources */,
1281+
ABC1D7DD2023A04F00BA84F0 /* empty_credentials_provider_test.cc in Sources */,
12561282
DE2EF0861F3D0B6E003D0CDC /* FSTImmutableSortedDictionary+Testing.m in Sources */,
12571283
B686F2AF2023DDEE0028D6BE /* field_path_test.cc in Sources */,
12581284
5492E03120213FFC00B64F25 /* FSTLevelDBSpecTests.mm in Sources */,
@@ -1272,6 +1298,7 @@
12721298
5492E0AC2021552D00B64F25 /* FSTMutationQueueTests.mm in Sources */,
12731299
5492E056202154AB00B64F25 /* FIRFieldPathTests.mm in Sources */,
12741300
5492E03220213FFC00B64F25 /* FSTMockDatastore.mm in Sources */,
1301+
ABC1D7E12023A40C00BA84F0 /* token_test.cc in Sources */,
12751302
AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */,
12761303
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */,
12771304
5492E0AD2021552D00B64F25 /* FSTMemoryMutationQueueTests.mm in Sources */,
@@ -1296,8 +1323,10 @@
12961323
5492E068202154B900B64F25 /* FSTQueryTests.mm in Sources */,
12971324
5492E0AB2021552D00B64F25 /* StringViewTests.mm in Sources */,
12981325
5492E0C92021557E00B64F25 /* FSTRemoteEventTests.mm in Sources */,
1326+
ABC1D7E42024AFDE00BA84F0 /* firebase_credentials_provider_test.mm in Sources */,
12991327
ABF6506C201131F8005F2C74 /* timestamp_test.cc in Sources */,
13001328
5492E0AE2021552D00B64F25 /* FSTLevelDBQueryCacheTests.mm in Sources */,
1329+
ABC1D7DC2023A04B00BA84F0 /* credentials_provider_test.cc in Sources */,
13011330
5492E059202154AB00B64F25 /* FIRQuerySnapshotTests.mm in Sources */,
13021331
5492E050202154AA00B64F25 /* FIRCollectionReferenceTests.mm in Sources */,
13031332
5492E0A02021552D00B64F25 /* FSTLevelDBMutationQueueTests.mm in Sources */,

Firestore/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
# limitations under the License.
1414

1515
add_subdirectory(src/firebase/firestore)
16+
add_subdirectory(src/firebase/firestore/auth)
1617
add_subdirectory(src/firebase/firestore/core)
1718
add_subdirectory(src/firebase/firestore/immutable)
1819
add_subdirectory(src/firebase/firestore/model)
1920
add_subdirectory(src/firebase/firestore/remote)
2021
add_subdirectory(src/firebase/firestore/util)
2122

2223
add_subdirectory(test/firebase/firestore)
24+
add_subdirectory(test/firebase/firestore/auth)
2325
add_subdirectory(test/firebase/firestore/core)
2426
add_subdirectory(test/firebase/firestore/immutable)
2527
add_subdirectory(test/firebase/firestore/model)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2018 Google
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cc_library(
16+
firebase_firestore_auth_base
17+
SOURCES
18+
credentials_provider.cc
19+
credentials_provider.h
20+
token.cc
21+
token.h
22+
user.cc
23+
user.h
24+
DEPENDS
25+
absl_strings
26+
firebase_firestore_util
27+
)
28+
29+
cc_library(
30+
firebase_firestore_auth_apple
31+
SOURCES
32+
firebase_credentials_provider_apple.h
33+
firebase_credentials_provider_apple.mm
34+
DEPENDS
35+
FirebaseCore
36+
firebase_firestore_auth_base
37+
EXCLUDE_FROM_ALL
38+
)
39+
40+
if(APPLE)
41+
list(APPEND AUTH_DEPENDS firebase_firestore_auth_apple)
42+
endif()
43+
44+
cc_library(
45+
firebase_firestore_auth
46+
SOURCES
47+
empty_credentials_provider.cc
48+
empty_credentials_provider.h
49+
DEPENDS
50+
${AUTH_DEPENDS}
51+
firebase_firestore_auth_base
52+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
18+
19+
namespace firebase {
20+
namespace firestore {
21+
namespace auth {
22+
23+
CredentialsProvider::CredentialsProvider() : user_change_listener_(nullptr) {
24+
}
25+
26+
CredentialsProvider::~CredentialsProvider() {
27+
}
28+
29+
} // namespace auth
30+
} // namespace firestore
31+
} // namespace firebase
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_AUTH_CREDENTIALS_PROVIDER_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_AUTH_CREDENTIALS_PROVIDER_H_
19+
20+
#include <functional>
21+
#include <string>
22+
23+
#include "Firestore/core/src/firebase/firestore/auth/token.h"
24+
#include "Firestore/core/src/firebase/firestore/auth/user.h"
25+
#include "absl/strings/string_view.h"
26+
27+
namespace firebase {
28+
namespace firestore {
29+
namespace auth {
30+
31+
// `TokenErrorListener` is a listener that gets a token or an error.
32+
// token: An auth token as a string, or nullptr if error occurred.
33+
// error: The error if one occurred, or else nullptr.
34+
typedef std::function<void(const Token& token, const absl::string_view error)>
35+
TokenListener;
36+
37+
// Listener notified with a User change.
38+
typedef std::function<void(const User& user)> UserChangeListener;
39+
40+
/**
41+
* Provides methods for getting the uid and token for the current user and
42+
* listen for changes.
43+
*/
44+
class CredentialsProvider {
45+
public:
46+
CredentialsProvider();
47+
48+
virtual ~CredentialsProvider();
49+
50+
/**
51+
* Requests token for the current user, optionally forcing a refreshed token
52+
* to be fetched.
53+
*/
54+
virtual void GetToken(bool force_refresh, TokenListener completion) = 0;
55+
56+
/**
57+
* Sets the listener to be notified of user changes (sign-in / sign-out). It
58+
* is immediately called once with the initial user.
59+
*
60+
* Call with nullptr to remove previous listener.
61+
*/
62+
virtual void SetUserChangeListener(UserChangeListener listener) = 0;
63+
64+
protected:
65+
/**
66+
* A listener to be notified of user changes (sign-in / sign-out). It is
67+
* immediately called once with the initial user.
68+
*
69+
* Note that this block will be called back on an arbitrary thread that is not
70+
* the normal Firestore worker thread.
71+
*/
72+
UserChangeListener user_change_listener_;
73+
};
74+
75+
} // namespace auth
76+
} // namespace firestore
77+
} // namespace firebase
78+
79+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_AUTH_CREDENTIALS_PROVIDER_H_
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#define UNUSED(x) (void)(x)
18+
19+
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace auth {
24+
25+
void EmptyCredentialsProvider::GetToken(bool force_refresh,
26+
TokenListener completion) {
27+
UNUSED(force_refresh);
28+
if (completion) {
29+
completion({"", User::Unauthenticated()}, "");
30+
}
31+
}
32+
33+
void EmptyCredentialsProvider::SetUserChangeListener(
34+
UserChangeListener listener) {
35+
if (listener) {
36+
listener(User::Unauthenticated());
37+
}
38+
}
39+
40+
} // namespace auth
41+
} // namespace firestore
42+
} // namespace firebase
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_AUTH_EMPTY_CREDENTIALS_PROVIDER_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_AUTH_EMPTY_CREDENTIALS_PROVIDER_H_
19+
20+
#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace auth {
25+
26+
/** `EmptyCredentialsProvider` always yields an empty token. */
27+
class EmptyCredentialsProvider : public CredentialsProvider {
28+
public:
29+
void GetToken(bool force_refresh, TokenListener completion) override;
30+
void SetUserChangeListener(UserChangeListener listener) override;
31+
};
32+
33+
} // namespace auth
34+
} // namespace firestore
35+
} // namespace firebase
36+
37+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_AUTH_EMPTY_CREDENTIALS_PROVIDER_H_

0 commit comments

Comments
 (0)