FAuthTokenProvider.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "FAuthTokenProvider.h"
  17. #import "FUtilities.h"
  18. #import "FIRDatabaseQuery_Private.h"
  19. #import "FIRNoopAuthTokenProvider.h"
  20. static NSString *const FIRAuthStateDidChangeInternalNotification = @"FIRAuthStateDidChangeInternalNotification";
  21. static NSString *const FIRAuthStateDidChangeInternalNotificationTokenKey = @"FIRAuthStateDidChangeInternalNotificationTokenKey";
  22. /**
  23. * This is a hack that defines all the methods we need from FIRFirebaseApp. At runtime we use reflection to get an
  24. * actual instance of FIRFirebaseApp. Since protocols don't carry any runtime information and selectors are invoked
  25. * by name we can write code against this protocol as long as the method signatures of FIRFirebaseApp don't change.
  26. */
  27. @protocol FIRFirebaseAppLike <NSObject>
  28. - (void)getTokenForcingRefresh:(BOOL)forceRefresh withCallback:(void (^)(NSString *_Nullable token, NSError *_Nullable error))callback;
  29. @end
  30. /**
  31. * This is a hack that defines all the methods we need from FIRAuth.
  32. */
  33. @protocol FIRFirebaseAuthLike <NSObject>
  34. - (id<FIRFirebaseAppLike>) app;
  35. @end
  36. /**
  37. * This is a hack that copies the definitions of Firebase Auth error codes. If the error codes change in the original code, this
  38. * will break at runtime due to undefined behavior!
  39. */
  40. typedef NS_ENUM(NSUInteger, FIRErrorCode) {
  41. /*! @var FIRErrorCodeNoAuth
  42. @brief Represents the case where an auth-related message was sent to a @c FIRFirebaseApp
  43. instance which has no associated @c FIRAuth instance.
  44. */
  45. FIRErrorCodeNoAuth,
  46. /*! @var FIRErrorCodeNoSignedInUser
  47. @brief Represents the case where an attempt was made to fetch a token when there is no signed
  48. in user.
  49. */
  50. FIRErrorCodeNoSignedInUser,
  51. };
  52. @interface FAuthStateListenerWrapper : NSObject
  53. @property (nonatomic, copy) fbt_void_nsstring listener;
  54. @property (nonatomic, weak) id<FIRFirebaseAppLike> app;
  55. @end
  56. @implementation FAuthStateListenerWrapper
  57. - (instancetype) initWithListener:(fbt_void_nsstring)listener app:(id<FIRFirebaseAppLike>)app {
  58. self = [super init];
  59. if (self != nil) {
  60. self->_listener = listener;
  61. self->_app = app;
  62. [[NSNotificationCenter defaultCenter] addObserver:self
  63. selector:@selector(authStateDidChangeNotification:)
  64. name:FIRAuthStateDidChangeInternalNotification
  65. object:nil];
  66. }
  67. return self;
  68. }
  69. - (void) authStateDidChangeNotification:(NSNotification *)notification {
  70. id<FIRFirebaseAuthLike> auth = notification.object;
  71. if (auth.app == self->_app) {
  72. NSDictionary *userInfo = notification.userInfo;
  73. NSString *token = userInfo[FIRAuthStateDidChangeInternalNotificationTokenKey];
  74. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  75. self.listener(token);
  76. });
  77. }
  78. }
  79. - (void) dealloc {
  80. [[NSNotificationCenter defaultCenter] removeObserver:self];
  81. }
  82. @end
  83. @interface FIRFirebaseAuthTokenProvider : NSObject <FAuthTokenProvider>
  84. @property (nonatomic, strong) id<FIRFirebaseAppLike> app;
  85. /** Strong references to the auth listeners as they are only weak in FIRFirebaseApp */
  86. @property (nonatomic, strong) NSMutableArray *authListeners;
  87. - (instancetype) initWithFirebaseApp:(id<FIRFirebaseAppLike>)app;
  88. @end
  89. @implementation FIRFirebaseAuthTokenProvider
  90. - (instancetype) initWithFirebaseApp:(id<FIRFirebaseAppLike>)app {
  91. self = [super init];
  92. if (self != nil) {
  93. self->_app = app;
  94. self->_authListeners = [NSMutableArray array];
  95. }
  96. return self;
  97. }
  98. - (void) fetchTokenForcingRefresh:(BOOL)forceRefresh withCallback:(fbt_void_nsstring_nserror)callback {
  99. // TODO: Don't fetch token if there is no current user
  100. [self.app getTokenForcingRefresh:forceRefresh withCallback:^(NSString * _Nullable token, NSError * _Nullable error) {
  101. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  102. if (error != nil) {
  103. if (error.code == FIRErrorCodeNoAuth) {
  104. FFLog(@"I-RDB073001", @"Firebase Auth is not configured, not going to use authentication.");
  105. callback(nil, nil);
  106. } else if (error.code == FIRErrorCodeNoSignedInUser) {
  107. // No signed in user is an expected case, callback as success with no token
  108. callback(nil, nil);
  109. } else {
  110. callback(nil, error);
  111. }
  112. } else {
  113. callback(token, nil);
  114. }
  115. });
  116. }];
  117. }
  118. - (void) listenForTokenChanges:(_Nonnull fbt_void_nsstring)listener {
  119. FAuthStateListenerWrapper *wrapper = [[FAuthStateListenerWrapper alloc] initWithListener:listener app:self.app];
  120. [self.authListeners addObject:wrapper];
  121. }
  122. @end
  123. @implementation FAuthTokenProvider
  124. + (id<FAuthTokenProvider>) authTokenProviderForApp:(id)app {
  125. return [[FIRFirebaseAuthTokenProvider alloc] initWithFirebaseApp:app];
  126. }
  127. @end