-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix for b/74357976 and some cleanup. #887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,7 @@ @implementation FSTEmptyCredentialsProvider | |
|
||
- (void)getTokenForcingRefresh:(BOOL)forceRefresh | ||
completion:(FSTVoidGetTokenResultBlock)completion { | ||
// Invalid token will force the GRPC fallback to use default settings. | ||
completion(Token::Invalid(), nil); | ||
completion(Token::Token("", User::Unauthenticated()), nil); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't this |
||
} | ||
|
||
- (void)setUserChangeListener:(nullable FSTVoidUserBlock)block { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,12 +309,13 @@ - (void)invokeRPCWithFactory:(GRPCProtoCall * (^)(void))rpcFactory | |
if (error) { | ||
errorHandler(error); | ||
} else { | ||
FSTAssert(result.is_valid(), | ||
@"invalid token result even though there was no error."); | ||
GRPCProtoCall *rpc = rpcFactory(); | ||
[FSTDatastore | ||
prepareHeadersForRPC:rpc | ||
databaseID:&self.databaseInfo->database_id() | ||
token:(result.is_valid() ? result.token() | ||
: absl::string_view())]; | ||
token:result.token()]; | ||
[rpc start]; | ||
} | ||
}]; | ||
|
@@ -339,7 +340,8 @@ - (FSTWriteStream *)createWriteStream { | |
+ (void)prepareHeadersForRPC:(GRPCCall *)rpc | ||
databaseID:(const DatabaseId *)databaseID | ||
token:(const absl::string_view)token { | ||
rpc.oauth2AccessToken = token.data() == nullptr ? nil : util::WrapNSString(token); | ||
// "" represents the lack of a token. | ||
rpc.oauth2AccessToken = token == "" ? nil : util::WrapNSString(token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just pass the token.is_valid() ? util::WrapNSString(token.token()) : nil; |
||
rpc.requestHeaders[kXGoogAPIClientHeader] = [FSTDatastore googAPIClientHeaderValue]; | ||
// This header is used to improve routing and project isolation by the backend. | ||
rpc.requestHeaders[kGoogleCloudResourcePrefix] = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,7 +96,7 @@ | |
// Cancel the request since the user changed while the request was | ||
// outstanding so the response is likely for a previous user (which | ||
// user, we can't be sure). | ||
completion({"", User::Unauthenticated()}, | ||
completion(Token::Invalid(), | ||
"getToken aborted due to user change."); | ||
} else { | ||
completion( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the bug in the line below: token can be I think we should probably move to something like what I did for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The C++ credential-provider is not used yet in the release. The porting of credential-provider is after the release cut. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ namespace auth { | |
|
||
Token::Token(const absl::string_view token, const User& user) | ||
: token_(token), user_(user), is_valid_(true) { | ||
// We use "" to represent the absence of a token, which should | ||
// be the case if the user is unauthenticated. | ||
FIREBASE_ASSERT((!user_.is_authenticated()) == (token == "")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this assertion failed I'm not sure what I would do about it; as it stands I can barely understand it. Secondarily this seems like a weak assertion given that we're filling in is_valid_(true) above. Tertiarily this seems backwards: we should be constructing the right kind token given our inputs rather than requiring every caller to get this right. We clearly haven't done that here so I propose we move all the callers to a factory method that returns a valid token given valid inputs or an Invalid token otherwise. At that point we don't have to fail if auth gives us a crazy value: If the user is unauthenticated or the token is empty return invalid. Otherwise fill in a valid Token (and this constructor becomes private). In the interest of good citizenry we could also make the assertion that these align, but that would be for reporting bad values to FIRAuth and less for catching our code out. |
||
} | ||
|
||
Token::Token() : token_(), user_(User::Unauthenticated()), is_valid_(false) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ class Token { | |
public: | ||
Token(const absl::string_view token, const User& user); | ||
|
||
/** The actual raw token. */ | ||
/** The actual raw token; will be "" if the user is unauthenticated. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem right. A valid token is not "". An invalid token might be "" but this should have asserted before. |
||
const std::string& token() const { | ||
FIREBASE_ASSERT(is_valid_); | ||
return token_; | ||
|
@@ -55,21 +55,22 @@ class Token { | |
* state on disk, etc.). | ||
*/ | ||
const User& user() const { | ||
FIREBASE_ASSERT(is_valid_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code somewhere actually obtains user from invalid token; seems that's why you also changed empty-credential-provider to passing-in a valid token with invalid content. I guess we first need to come with what exactly In real world:
In test cases: all of above besides
|
||
return user_; | ||
} | ||
|
||
/** | ||
* Whether the token is a valid one. | ||
* Whether this Token object is valid. All methods will throw if it's not. | ||
* | ||
* ## Portability notes: Invalid token is the equivalent of nil in the iOS | ||
* token implementation. We use value instead of pointer for Token instance in | ||
* the C++ migration. | ||
* ## Portability notes: An invalid Token is the equivalent of nil in the | ||
* iOS token implementation. We use value instead of pointer for Token | ||
* instances in the C++ migration. | ||
*/ | ||
bool is_valid() const { | ||
return is_valid_; | ||
} | ||
|
||
/** Returns an invalid token. */ | ||
/** Returns an invalid Token. */ | ||
static const Token& Invalid(); | ||
|
||
private: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string_view can be initialized with nullptr, in which case, the .data() returns nullptr, which is what the prepareHeadersForRPC relies on whether to prepare the header or fallback to default. (
firebase-ios-sdk/Firestore/Source/Remote/FSTDatastore.mm
Line 347 in b7750b5
i.e. token string_view can differentiate between nil and "". For nil, rpc.oauth2AccessToken is nil, which makes it fallback to default (without actually write that header line) v.s. for "", which makes it write a header line with token being empty string. This also makes me think we should never allow token being empty string, since it will cause the header being invalid to the server.