FIRAppCheck.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheck.h"
  17. #import <AppCheck/AppCheck.h>
  18. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckErrors.h"
  19. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckProvider.h"
  20. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckProviderFactory.h"
  21. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  22. #import "FirebaseAppCheck/Sources/Core/FIRApp+AppCheck.h"
  23. #import "FirebaseAppCheck/Sources/Core/FIRAppCheck+Internal.h"
  24. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"
  25. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckSettings.h"
  26. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"
  27. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckTokenResult.h"
  28. #import "FirebaseAppCheck/Sources/Core/FIRInternalAppCheckProvider.h"
  29. #import "FirebaseAppCheck/Interop/FIRAppCheckInterop.h"
  30. #import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"
  31. NS_ASSUME_NONNULL_BEGIN
  32. /// A notification with the specified name is sent to the default notification center
  33. /// (`NotificationCenter.default`) each time a Firebase app check token is refreshed.
  34. /// The user info dictionary contains `kFIRAppCheckTokenNotificationKey` and
  35. /// `kFIRAppCheckAppNameNotificationKey` keys.
  36. const NSNotificationName FIRAppCheckAppCheckTokenDidChangeNotification =
  37. @"FIRAppCheckAppCheckTokenDidChangeNotification";
  38. /// `userInfo` key for the `AppCheckToken` in `appCheckTokenRefreshNotification`.
  39. NSString *const kFIRAppCheckTokenNotificationKey = @"FIRAppCheckTokenNotificationKey";
  40. /// `userInfo` key for the `FirebaseApp.name` in `appCheckTokenRefreshNotification`.
  41. NSString *const kFIRAppCheckAppNameNotificationKey = @"FIRAppCheckAppNameNotificationKey";
  42. static id<FIRAppCheckProviderFactory> _providerFactory;
  43. static NSString *const kDummyFACTokenValue = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==";
  44. @interface FIRAppCheck () <GACAppCheckTokenDelegate, FIRAppCheckInterop>
  45. @property(class, nullable) id<FIRAppCheckProviderFactory> providerFactory;
  46. @property(nonatomic, readonly) NSString *appName;
  47. @property(nonatomic, readonly) GACAppCheck *internalAppCheck;
  48. @property(nonatomic, readonly) NSNotificationCenter *notificationCenter;
  49. @property(nonatomic, readonly) id<GACAppCheckSettingsProtocol, FIRAppCheckSettingsProtocol>
  50. settings;
  51. @end
  52. @implementation FIRAppCheck
  53. #pragma mark - Internal
  54. - (nullable instancetype)initWithApp:(FIRApp *)app {
  55. id<FIRAppCheckProviderFactory> providerFactory = [FIRAppCheck providerFactory];
  56. if (providerFactory == nil) {
  57. FIRLogError(kFIRLoggerAppCheck, kFIRLoggerAppCheckMessageCodeProviderFactoryIsMissing,
  58. @"Cannot instantiate `FIRAppCheck` for app: %@ without a provider factory. "
  59. @"Please register a provider factory using "
  60. @"`AppCheck.setAppCheckProviderFactory(_ ,forAppName:)` method.",
  61. app.name);
  62. return nil;
  63. }
  64. id<FIRAppCheckProvider> appCheckProvider = [providerFactory createProviderWithApp:app];
  65. if (appCheckProvider == nil) {
  66. FIRLogError(kFIRLoggerAppCheck, kFIRLoggerAppCheckMessageCodeProviderIsMissing,
  67. @"Cannot instantiate `FIRAppCheck` for app: %@ without an app check provider. "
  68. @"Please make sure the provider factory returns a valid app check provider.",
  69. app.name);
  70. return nil;
  71. }
  72. id<GACAppCheckProvider> internalAppCheckProvider =
  73. [[FIRInternalAppCheckProvider alloc] initWithAppCheckProvider:appCheckProvider];
  74. id<GACAppCheckSettingsProtocol, FIRAppCheckSettingsProtocol> settings =
  75. [[FIRAppCheckSettings alloc] initWithApp:app
  76. userDefault:[NSUserDefaults standardUserDefaults]
  77. mainBundle:[NSBundle mainBundle]];
  78. return [self initWithAppName:app.name
  79. appCheckProvider:internalAppCheckProvider
  80. notificationCenter:[NSNotificationCenter defaultCenter]
  81. settings:settings
  82. resourceName:app.resourceName
  83. appGroupID:app.options.appGroupID];
  84. }
  85. - (instancetype)initWithAppName:(NSString *)appName
  86. appCheckProvider:(id<GACAppCheckProvider>)appCheckProvider
  87. notificationCenter:(NSNotificationCenter *)notificationCenter
  88. settings:
  89. (id<GACAppCheckSettingsProtocol, FIRAppCheckSettingsProtocol>)settings
  90. resourceName:(NSString *)resourceName
  91. appGroupID:(NSString *)appGroupID {
  92. self = [super init];
  93. if (self) {
  94. _appName = appName;
  95. _notificationCenter = notificationCenter;
  96. _settings = settings;
  97. _internalAppCheck = [[GACAppCheck alloc] initWithInstanceName:appName
  98. appCheckProvider:appCheckProvider
  99. settings:settings
  100. resourceName:resourceName
  101. keychainAccessGroup:appGroupID];
  102. _internalAppCheck.tokenDelegate = self;
  103. //
  104. // [_internalNotificationCenter addObserver:self
  105. // selector:@selector(tokenUpdateNotification:)
  106. // name:GACAppCheckAppCheckTokenDidChangeNotification
  107. // object:nil];
  108. }
  109. return self;
  110. }
  111. #pragma mark - Public
  112. + (instancetype)appCheck {
  113. FIRApp *defaultApp = [FIRApp defaultApp];
  114. if (!defaultApp) {
  115. [NSException raise:FIRAppCheckErrorDomain
  116. format:@"The default FirebaseApp instance must be configured before the default"
  117. @"AppCheck instance can be initialized. One way to ensure this is to "
  118. @"call `FirebaseApp.configure()` in the App Delegate's "
  119. @"`application(_:didFinishLaunchingWithOptions:)` (or the `@main` struct's "
  120. @"initializer in SwiftUI)."];
  121. }
  122. return [self appCheckWithApp:defaultApp];
  123. }
  124. + (nullable instancetype)appCheckWithApp:(FIRApp *)firebaseApp {
  125. id<FIRAppCheckInterop> appCheck = FIR_COMPONENT(FIRAppCheckInterop, firebaseApp.container);
  126. return (FIRAppCheck *)appCheck;
  127. }
  128. - (void)tokenForcingRefresh:(BOOL)forcingRefresh
  129. completion:(void (^)(FIRAppCheckToken *_Nullable token,
  130. NSError *_Nullable error))handler {
  131. [self.internalAppCheck
  132. tokenForcingRefresh:forcingRefresh
  133. completion:^(GACAppCheckToken *_Nullable internalToken, NSError *_Nullable error) {
  134. if (error) {
  135. handler(nil, [FIRAppCheckErrorUtil publicDomainErrorWithError:error]);
  136. return;
  137. }
  138. handler([[FIRAppCheckToken alloc] initWithInternalToken:internalToken], nil);
  139. }];
  140. }
  141. - (void)limitedUseTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token,
  142. NSError *_Nullable error))handler {
  143. [self.internalAppCheck limitedUseTokenWithCompletion:^(GACAppCheckToken *_Nullable internalToken,
  144. NSError *_Nullable error) {
  145. if (error) {
  146. handler(nil, [FIRAppCheckErrorUtil publicDomainErrorWithError:error]);
  147. return;
  148. }
  149. handler([[FIRAppCheckToken alloc] initWithInternalToken:internalToken], nil);
  150. }];
  151. }
  152. + (void)setAppCheckProviderFactory:(nullable id<FIRAppCheckProviderFactory>)factory {
  153. self.providerFactory = factory;
  154. }
  155. - (void)setIsTokenAutoRefreshEnabled:(BOOL)isTokenAutoRefreshEnabled {
  156. self.settings.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
  157. }
  158. - (BOOL)isTokenAutoRefreshEnabled {
  159. return self.settings.isTokenAutoRefreshEnabled;
  160. }
  161. #pragma mark - App Check Provider Ingestion
  162. + (void)setProviderFactory:(nullable id<FIRAppCheckProviderFactory>)providerFactory {
  163. @synchronized(self) {
  164. _providerFactory = providerFactory;
  165. }
  166. }
  167. + (nullable id<FIRAppCheckProviderFactory>)providerFactory {
  168. @synchronized(self) {
  169. return _providerFactory;
  170. }
  171. }
  172. #pragma mark - FIRAppCheckInterop
  173. - (void)getTokenForcingRefresh:(BOOL)forcingRefresh
  174. completion:(FIRAppCheckTokenHandlerInterop)handler {
  175. [self.internalAppCheck
  176. tokenForcingRefresh:forcingRefresh
  177. completion:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
  178. FIRAppCheckTokenResult *tokenResult;
  179. if (token) {
  180. tokenResult = [[FIRAppCheckTokenResult alloc] initWithToken:token.token
  181. error:nil];
  182. } else {
  183. tokenResult = [[FIRAppCheckTokenResult alloc] initWithToken:kDummyFACTokenValue
  184. error:error];
  185. }
  186. handler(tokenResult);
  187. }];
  188. }
  189. - (void)getLimitedUseTokenWithCompletion:(FIRAppCheckTokenHandlerInterop)handler {
  190. [self.internalAppCheck
  191. limitedUseTokenWithCompletion:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
  192. FIRAppCheckTokenResult *tokenResult;
  193. if (token) {
  194. tokenResult = [[FIRAppCheckTokenResult alloc] initWithToken:token.token error:nil];
  195. } else {
  196. tokenResult = [[FIRAppCheckTokenResult alloc] initWithToken:token.token error:error];
  197. }
  198. handler(tokenResult);
  199. }];
  200. }
  201. - (nonnull NSString *)tokenDidChangeNotificationName {
  202. return FIRAppCheckAppCheckTokenDidChangeNotification;
  203. }
  204. - (nonnull NSString *)notificationAppNameKey {
  205. return kFIRAppCheckAppNameNotificationKey;
  206. }
  207. - (nonnull NSString *)notificationTokenKey {
  208. return kFIRAppCheckTokenNotificationKey;
  209. }
  210. #pragma mark - GACAppCheckTokenDelegate
  211. - (void)didUpdateWithToken:(NSString *)token {
  212. [self.notificationCenter postNotificationName:FIRAppCheckAppCheckTokenDidChangeNotification
  213. object:self
  214. userInfo:@{
  215. kFIRAppCheckTokenNotificationKey : token,
  216. kFIRAppCheckAppNameNotificationKey : self.appName
  217. }];
  218. }
  219. @end
  220. NS_ASSUME_NONNULL_END