FUNContext.m 3.7 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. #import "Functions/FirebaseFunctions/FUNContext.h"
  16. #import "FirebaseAppCheck/Sources/Interop/FIRAppCheckInterop.h"
  17. #import "FirebaseAppCheck/Sources/Interop/FIRAppCheckTokenResultInterop.h"
  18. #import "FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h"
  19. #import "Interop/Auth/Public/FIRAuthInterop.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @interface FUNContext ()
  22. - (instancetype)initWithAuthToken:(NSString *_Nullable)authToken
  23. FCMToken:(NSString *_Nullable)FCMToken
  24. appCheckToken:(NSString *_Nullable)appCheckToken NS_DESIGNATED_INITIALIZER;
  25. @end
  26. @implementation FUNContext
  27. - (instancetype)initWithAuthToken:(NSString *_Nullable)authToken
  28. FCMToken:(NSString *_Nullable)FCMToken
  29. appCheckToken:(NSString *_Nullable)appCheckToken {
  30. self = [super init];
  31. if (self) {
  32. _authToken = [authToken copy];
  33. _FCMToken = [FCMToken copy];
  34. _appCheckToken = [appCheckToken copy];
  35. }
  36. return self;
  37. }
  38. @end
  39. @interface FUNContextProvider () {
  40. id<FIRAuthInterop> _Nullable _auth;
  41. id<FIRMessagingInterop> _Nullable _messaging;
  42. id<FIRAppCheckInterop> _Nullable _appCheck;
  43. }
  44. @end
  45. @implementation FUNContextProvider
  46. - (instancetype)initWithAuth:(nullable id<FIRAuthInterop>)auth
  47. messaging:(nullable id<FIRMessagingInterop>)messaging
  48. appCheck:(nullable id<FIRAppCheckInterop>)appCheck {
  49. self = [super init];
  50. if (self) {
  51. _auth = auth;
  52. _messaging = messaging;
  53. _appCheck = appCheck;
  54. }
  55. return self;
  56. }
  57. // This is broken out so it can be mocked for tests.
  58. - (NSString *)FCMToken {
  59. return _messaging.FCMToken;
  60. }
  61. - (void)getContext:(void (^)(FUNContext *context, NSError *_Nullable error))completion {
  62. dispatch_group_t dispatchGroup = dispatch_group_create();
  63. // Try to get FCM token.
  64. NSString *FCMToken = [self FCMToken];
  65. __block NSString *authToken;
  66. __block NSString *appCheckToken;
  67. __block NSError *authError;
  68. // Fetch auth token if available.
  69. if (_auth != nil) {
  70. dispatch_group_enter(dispatchGroup);
  71. [_auth getTokenForcingRefresh:NO
  72. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  73. authToken = token;
  74. authError = error;
  75. dispatch_group_leave(dispatchGroup);
  76. }];
  77. }
  78. // Fetch FAC token if available.
  79. if (_appCheck) {
  80. dispatch_group_enter(dispatchGroup);
  81. [_appCheck getTokenForcingRefresh:NO
  82. completion:^(id<FIRAppCheckTokenResultInterop> _Nonnull tokenResult) {
  83. appCheckToken = tokenResult.token;
  84. dispatch_group_leave(dispatchGroup);
  85. }];
  86. }
  87. dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
  88. FUNContext *context = [[FUNContext alloc] initWithAuthToken:authToken
  89. FCMToken:FCMToken
  90. appCheckToken:appCheckToken];
  91. if (completion) {
  92. completion(context, authError);
  93. }
  94. });
  95. }
  96. @end
  97. NS_ASSUME_NONNULL_END