FAuthTokenProvider.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "FirebaseDatabase/Sources/Login/FAuthTokenProvider.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "Interop/Auth/Public/FIRAuthInterop.h"
  19. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  20. #import "FirebaseDatabase/Sources/Login/FIRNoopAuthTokenProvider.h"
  21. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  22. @interface FAuthStateListenerWrapper : NSObject
  23. @property(nonatomic, copy) fbt_void_nsstring listener;
  24. @property(nonatomic, weak) id<FIRAuthInterop> auth;
  25. @end
  26. @implementation FAuthStateListenerWrapper
  27. - (instancetype)initWithListener:(fbt_void_nsstring)listener
  28. auth:(id<FIRAuthInterop>)auth {
  29. self = [super init];
  30. if (self != nil) {
  31. self->_listener = listener;
  32. self->_auth = auth;
  33. [[NSNotificationCenter defaultCenter]
  34. addObserver:self
  35. selector:@selector(authStateDidChangeNotification:)
  36. name:FIRAuthStateDidChangeInternalNotification
  37. object:nil];
  38. }
  39. return self;
  40. }
  41. - (void)authStateDidChangeNotification:(NSNotification *)notification {
  42. NSDictionary *userInfo = notification.userInfo;
  43. if (notification.object == self.auth) {
  44. NSString *token =
  45. userInfo[FIRAuthStateDidChangeInternalNotificationTokenKey];
  46. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  47. self.listener(token);
  48. });
  49. }
  50. }
  51. - (void)dealloc {
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. @end
  55. @interface FIRFirebaseAuthTokenProvider : NSObject <FAuthTokenProvider>
  56. @property(nonatomic, strong) id<FIRAuthInterop> auth;
  57. /** Strong references to the auth listeners as they are only weak in
  58. * FIRFirebaseApp */
  59. @property(nonatomic, strong) NSMutableArray *authListeners;
  60. - (instancetype)initWithAuth:(id<FIRAuthInterop>)auth;
  61. @end
  62. @implementation FIRFirebaseAuthTokenProvider
  63. - (instancetype)initWithAuth:(id<FIRAuthInterop>)auth {
  64. self = [super init];
  65. if (self != nil) {
  66. self->_auth = auth;
  67. self->_authListeners = [NSMutableArray array];
  68. }
  69. return self;
  70. }
  71. - (void)fetchTokenForcingRefresh:(BOOL)forceRefresh
  72. withCallback:(fbt_void_nsstring_nserror)callback {
  73. if (self.auth == nil) {
  74. // Signal that Auth is not available by returning nil.
  75. callback(nil, nil);
  76. } else {
  77. [self.auth getTokenForcingRefresh:forceRefresh
  78. withCallback:^(NSString *_Nullable token,
  79. NSError *_Nullable error) {
  80. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  81. callback(token, error);
  82. });
  83. }];
  84. }
  85. }
  86. - (void)listenForTokenChanges:(_Nonnull fbt_void_nsstring)listener {
  87. FAuthStateListenerWrapper *wrapper =
  88. [[FAuthStateListenerWrapper alloc] initWithListener:listener
  89. auth:self.auth];
  90. [self.authListeners addObject:wrapper];
  91. }
  92. @end
  93. @implementation FAuthTokenProvider
  94. + (id<FAuthTokenProvider>)authTokenProviderWithAuth:
  95. (id<FIRAuthInterop>)authInterop {
  96. return [[FIRFirebaseAuthTokenProvider alloc] initWithAuth:authInterop];
  97. }
  98. @end