FAuthTokenProvider.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <FirebaseCore/FIRAppInternal.h>
  19. #import <FirebaseCore/FIRLogger.h>
  20. #import "FIRDatabaseQuery_Private.h"
  21. #import "FIRNoopAuthTokenProvider.h"
  22. @interface FAuthStateListenerWrapper : NSObject
  23. @property (nonatomic, copy) fbt_void_nsstring listener;
  24. @property (nonatomic, weak) FIRApp *app;
  25. @end
  26. @implementation FAuthStateListenerWrapper
  27. - (instancetype) initWithListener:(fbt_void_nsstring)listener app:(FIRApp *)app {
  28. self = [super init];
  29. if (self != nil) {
  30. self->_listener = listener;
  31. self->_app = app;
  32. [[NSNotificationCenter defaultCenter] addObserver:self
  33. selector:@selector(authStateDidChangeNotification:)
  34. name:FIRAuthStateDidChangeInternalNotification
  35. object:nil];
  36. }
  37. return self;
  38. }
  39. - (void) authStateDidChangeNotification:(NSNotification *)notification {
  40. NSDictionary *userInfo = notification.userInfo;
  41. FIRApp *authApp = userInfo[FIRAuthStateDidChangeInternalNotificationAppKey];
  42. if (authApp == self.app) {
  43. NSString *token = userInfo[FIRAuthStateDidChangeInternalNotificationTokenKey];
  44. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  45. self.listener(token);
  46. });
  47. }
  48. }
  49. - (void) dealloc {
  50. [[NSNotificationCenter defaultCenter] removeObserver:self];
  51. }
  52. @end
  53. @interface FIRFirebaseAuthTokenProvider : NSObject <FAuthTokenProvider>
  54. @property (nonatomic, strong) FIRApp *app;
  55. /** Strong references to the auth listeners as they are only weak in FIRFirebaseApp */
  56. @property (nonatomic, strong) NSMutableArray *authListeners;
  57. - (instancetype) initWithFirebaseApp:(FIRApp *)app;
  58. @end
  59. @implementation FIRFirebaseAuthTokenProvider
  60. - (instancetype) initWithFirebaseApp:(FIRApp *)app {
  61. self = [super init];
  62. if (self != nil) {
  63. self->_app = app;
  64. self->_authListeners = [NSMutableArray array];
  65. }
  66. return self;
  67. }
  68. - (void) fetchTokenForcingRefresh:(BOOL)forceRefresh withCallback:(fbt_void_nsstring_nserror)callback {
  69. // TODO: Don't fetch token if there is no current user
  70. [self.app getTokenForcingRefresh:forceRefresh withCallback:^(NSString * _Nullable token, NSError * _Nullable error) {
  71. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  72. callback(token, error);
  73. });
  74. }];
  75. }
  76. - (void) listenForTokenChanges:(_Nonnull fbt_void_nsstring)listener {
  77. FAuthStateListenerWrapper *wrapper = [[FAuthStateListenerWrapper alloc] initWithListener:listener app:self.app];
  78. [self.authListeners addObject:wrapper];
  79. }
  80. @end
  81. @implementation FAuthTokenProvider
  82. + (id<FAuthTokenProvider>) authTokenProviderForApp:(id)app {
  83. return [[FIRFirebaseAuthTokenProvider alloc] initWithFirebaseApp:app];
  84. }
  85. @end