FSTCredentialsProvider.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2017 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. #import "Firestore/Source/Auth/FSTCredentialsProvider.h"
  17. #import <FirebaseAuth/FIRAuth.h>
  18. #import <FirebaseAuth/FIRUser.h>
  19. #import <FirebaseCore/FIRApp.h>
  20. #import <FirebaseCore/FIRAppInternal.h>
  21. #import <GRPCClient/GRPCCall.h>
  22. #import "FIRFirestoreErrors.h"
  23. #import "Firestore/Source/Util/FSTAssert.h"
  24. #import "Firestore/Source/Util/FSTClasses.h"
  25. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  26. #import "Firestore/Source/Auth/FSTUser.h"
  27. NS_ASSUME_NONNULL_BEGIN
  28. #pragma mark - FSTGetTokenResult
  29. @implementation FSTGetTokenResult
  30. - (instancetype)initWithUser:(FSTUser *)user token:(NSString *_Nullable)token {
  31. if (self = [super init]) {
  32. _user = user;
  33. _token = token;
  34. }
  35. return self;
  36. }
  37. @end
  38. #pragma mark - FSTFirebaseCredentialsProvider
  39. // TODO(mikelehen): Currently, we have a strong dependency on FIRAuth but we should ideally use
  40. // only internal APIs on FIRApp instead. However, currently the FIRApp internal APIs don't expose
  41. // the uid of the current user and don't expose an auth state change listener. So we use FIRAuth.
  42. @interface FSTFirebaseCredentialsProvider ()
  43. @property(nonatomic, strong, readonly) FIRApp *app;
  44. @property(nonatomic, strong, readonly) FIRAuth *auth;
  45. /** Handle used to stop receiving auth changes once userChangeListener is removed. */
  46. @property(nonatomic, strong, nullable, readwrite)
  47. FIRAuthStateDidChangeListenerHandle authListenerHandle;
  48. /** The current user as reported to us via our AuthStateDidChangeListener. */
  49. @property(nonatomic, strong, nonnull, readwrite) FSTUser *currentUser;
  50. /**
  51. * Counter used to detect if the user changed while a -getTokenForcingRefresh: request was
  52. * outstanding.
  53. */
  54. @property(nonatomic, assign, readwrite) int userCounter;
  55. @end
  56. @implementation FSTFirebaseCredentialsProvider {
  57. FSTVoidUserBlock _userChangeListener;
  58. }
  59. - (instancetype)initWithApp:(FIRApp *)app {
  60. self = [super init];
  61. if (self) {
  62. _app = app;
  63. _auth = [FIRAuth authWithApp:app];
  64. _currentUser = [[FSTUser alloc] initWithUID:self.auth.currentUser.uid];
  65. _userCounter = 0;
  66. // Register for user changes so that we can internally track the current user.
  67. FSTWeakify(self);
  68. _authListenerHandle = [self.auth addAuthStateDidChangeListener:^(FIRAuth *auth, FIRUser *user) {
  69. FSTStrongify(self);
  70. if (self) {
  71. @synchronized(self) {
  72. FSTUser *newUser = [[FSTUser alloc] initWithUID:user.uid];
  73. if (![newUser isEqual:self.currentUser]) {
  74. self.currentUser = newUser;
  75. self.userCounter++;
  76. FSTVoidUserBlock listenerBlock = self.userChangeListener;
  77. if (listenerBlock) {
  78. listenerBlock(self.currentUser);
  79. }
  80. }
  81. }
  82. }
  83. }];
  84. }
  85. return self;
  86. }
  87. - (void)getTokenForcingRefresh:(BOOL)forceRefresh
  88. completion:(FSTVoidGetTokenResultBlock)completion {
  89. FSTAssert(self.authListenerHandle, @"getToken cannot be called after listener removed.");
  90. // Take note of the current value of the userCounter so that this method can fail (with a
  91. // FIRFirestoreErrorCodeAborted error) if there is a user change while the request is outstanding.
  92. int initialUserCounter = self.userCounter;
  93. void (^getTokenCallback)(NSString *, NSError *) = ^(NSString *_Nullable token,
  94. NSError *_Nullable error) {
  95. @synchronized(self) {
  96. if (initialUserCounter != self.userCounter) {
  97. // Cancel the request since the user changed while the request was outstanding so the
  98. // response is likely for a previous user (which user, we can't be sure).
  99. NSDictionary *errorInfo = @{ @"details" : @"getToken aborted due to user change." };
  100. NSError *cancelError = [NSError errorWithDomain:FIRFirestoreErrorDomain
  101. code:FIRFirestoreErrorCodeAborted
  102. userInfo:errorInfo];
  103. completion(nil, cancelError);
  104. } else {
  105. FSTGetTokenResult *result =
  106. [[FSTGetTokenResult alloc] initWithUser:self.currentUser token:token];
  107. completion(result, error);
  108. }
  109. };
  110. };
  111. [self.app getTokenForcingRefresh:forceRefresh withCallback:getTokenCallback];
  112. }
  113. - (void)setUserChangeListener:(nullable FSTVoidUserBlock)block {
  114. @synchronized(self) {
  115. if (block) {
  116. FSTAssert(!_userChangeListener, @"UserChangeListener set twice!");
  117. // Fire initial event.
  118. block(self.currentUser);
  119. } else {
  120. FSTAssert(self.authListenerHandle, @"UserChangeListener removed twice!");
  121. FSTAssert(_userChangeListener, @"UserChangeListener removed without being set!");
  122. [self.auth removeAuthStateDidChangeListener:self.authListenerHandle];
  123. self.authListenerHandle = nil;
  124. }
  125. _userChangeListener = block;
  126. }
  127. }
  128. - (nullable FSTVoidUserBlock)userChangeListener {
  129. @synchronized(self) {
  130. return _userChangeListener;
  131. }
  132. }
  133. @end
  134. NS_ASSUME_NONNULL_END