FSTCredentialsProvider.m 5.4 KB

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