FAuthTokenProvider.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "FUtilities.h"
  22. #import "FIRDatabaseQuery_Private.h"
  23. #import "FIRNoopAuthTokenProvider.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 auth:(id<FIRAuthInterop>)auth {
  30. self = [super init];
  31. if (self != nil) {
  32. self->_listener = listener;
  33. self->_auth = auth;
  34. [[NSNotificationCenter defaultCenter] 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 = userInfo[FIRAuthStateDidChangeInternalNotificationTokenKey];
  45. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  46. self.listener(token);
  47. });
  48. }
  49. }
  50. - (void)dealloc {
  51. [[NSNotificationCenter defaultCenter] removeObserver:self];
  52. }
  53. @end
  54. @interface FIRFirebaseAuthTokenProvider : NSObject <FAuthTokenProvider>
  55. @property (nonatomic, strong) id<FIRAuthInterop> auth;
  56. /** Strong references to the auth listeners as they are only weak in FIRFirebaseApp */
  57. @property (nonatomic, strong) NSMutableArray *authListeners;
  58. - (instancetype) initWithAuth:(id<FIRAuthInterop>)auth;
  59. @end
  60. @implementation FIRFirebaseAuthTokenProvider
  61. - (instancetype)initWithAuth:(id<FIRAuthInterop>)auth {
  62. self = [super init];
  63. if (self != nil) {
  64. self->_auth = auth;
  65. self->_authListeners = [NSMutableArray array];
  66. }
  67. return self;
  68. }
  69. - (void)fetchTokenForcingRefresh:(BOOL)forceRefresh
  70. withCallback:(fbt_void_nsstring_nserror)callback {
  71. if (self.auth == nil) {
  72. // Signal that Auth is not available by returning nil.
  73. callback(nil, nil);
  74. } else {
  75. [self.auth getTokenForcingRefresh:forceRefresh
  76. withCallback:^(NSString * _Nullable token, NSError * _Nullable error) {
  77. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  78. callback(token, error);
  79. });
  80. }];
  81. }
  82. }
  83. - (void)listenForTokenChanges:(_Nonnull fbt_void_nsstring)listener {
  84. FAuthStateListenerWrapper *wrapper =
  85. [[FAuthStateListenerWrapper alloc] initWithListener:listener auth:self.auth];
  86. [self.authListeners addObject:wrapper];
  87. }
  88. @end
  89. @implementation FAuthTokenProvider
  90. + (id<FAuthTokenProvider>)authTokenProviderWithAuth:(id<FIRAuthInterop>)authInterop {
  91. return [[FIRFirebaseAuthTokenProvider alloc] initWithAuth:authInterop];
  92. }
  93. @end