FAuthTokenProvider.m 3.8 KB

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