GIDAppCheck.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2023 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 "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h"
  17. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  18. #import <AppCheckCore/GACAppCheck.h>
  19. #import <AppCheckCore/GACAppCheckSettings.h>
  20. #import <AppCheckCore/GACAppCheckTokenResult.h>
  21. #import <AppCheckCore/GACAppAttestProvider.h>
  22. #import <AppCheckCore/GACAppCheckDebugProvider.h>
  23. #import "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h"
  24. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAppCheckError.h"
  25. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  26. NSErrorDomain const kGIDAppCheckErrorDomain = @"com.google.GIDAppCheck";
  27. NSString *const kGIDAppCheckPreparedKey = @"com.google.GIDAppCheckPreparedKey";
  28. static NSString *const kGIDConfigClientIDKey = @"GIDClientID";
  29. static NSString *const kGIDAppAttestServiceName = @"GoogleSignIn-iOS";
  30. static NSString *const kGIDAppAttestResourceNameFormat = @"oauthClients/%@";
  31. static NSString *const kGIDAppAttestBaseURL = @"https://firebaseappcheck.googleapis.com/v1";
  32. typedef void (^GIDAppCheckPrepareCompletion)(NSError * _Nullable);
  33. typedef void (^GIDAppCheckTokenCompletion)(GACAppCheckToken *,NSError * _Nullable);
  34. @interface GIDAppCheck ()
  35. @property(nonatomic, strong) GACAppCheck *appCheck;
  36. @property(nonatomic, strong) dispatch_queue_t workerQueue;
  37. @property(nonatomic, strong) NSUserDefaults *userDefaults;
  38. @property(atomic, strong) NSMutableArray<GIDAppCheckPrepareCompletion> *prepareCompletions;
  39. @property(atomic) BOOL preparing;
  40. @end
  41. @implementation GIDAppCheck
  42. + (instancetype)appCheckUsingDebugProviderWithAPIKey:(NSString *)APIKey {
  43. return [[self alloc] initWithAppCheckProvider:[GIDAppCheck debugAppCheckProviderWithAPIKey:APIKey]
  44. userDefaults:[NSUserDefaults standardUserDefaults]];
  45. }
  46. + (instancetype)appCheckUsingAppAttestProvider {
  47. return [[self alloc] initWithAppCheckProvider:[GIDAppCheck appAttestProvider]
  48. userDefaults:[NSUserDefaults standardUserDefaults]];
  49. }
  50. - (instancetype)initWithAppCheckProvider:(id<GACAppCheckProvider>)appCheckProvider
  51. userDefaults:(NSUserDefaults *)userDefaults {
  52. if (self = [super init]) {
  53. _appCheck = [[GACAppCheck alloc] initWithServiceName:kGIDConfigClientIDKey
  54. resourceName:[GIDAppCheck appAttestResourceName]
  55. appCheckProvider:appCheckProvider
  56. settings:[[GACAppCheckSettings alloc] init]
  57. tokenDelegate:nil
  58. keychainAccessGroup:nil];
  59. _userDefaults = userDefaults;
  60. _workerQueue = dispatch_queue_create("com.google.googlesignin.GIDAppCheckWorkerQueue", nil);
  61. _prepareCompletions = [NSMutableArray array];
  62. _preparing = NO;
  63. }
  64. return self;
  65. }
  66. - (BOOL)isPrepared {
  67. return [self.userDefaults boolForKey:kGIDAppCheckPreparedKey];
  68. }
  69. - (void)prepareForAppCheckWithCompletion:(nullable GIDAppCheckPrepareCompletion)completion {
  70. if (completion) {
  71. @synchronized (self) {
  72. [self.prepareCompletions addObject:completion];
  73. }
  74. }
  75. @synchronized (self) {
  76. if (self.preparing) {
  77. return;
  78. }
  79. self.preparing = YES;
  80. }
  81. dispatch_async(self.workerQueue, ^{
  82. NSArray * __block callbacks;
  83. if ([self isPrepared]) {
  84. NSArray *callbacks;
  85. @synchronized (self) {
  86. callbacks = [self.prepareCompletions copy];
  87. [self.prepareCompletions removeAllObjects];
  88. self.preparing = NO;
  89. }
  90. for (GIDAppCheckPrepareCompletion savedCompletion in callbacks) {
  91. savedCompletion(nil);
  92. }
  93. return;
  94. }
  95. [self.appCheck limitedUseTokenWithCompletion:^(GACAppCheckTokenResult * _Nonnull result) {
  96. NSError * __block maybeError = result.error;
  97. @synchronized (self) {
  98. if (!result.token && !result.error) {
  99. maybeError = [NSError errorWithDomain:kGIDAppCheckErrorDomain
  100. code:kGIDAppCheckUnexpectedError
  101. userInfo:nil];
  102. }
  103. if (result.token) {
  104. [self.userDefaults setBool:YES forKey:kGIDAppCheckPreparedKey];
  105. }
  106. callbacks = [self.prepareCompletions copy];
  107. [self.prepareCompletions removeAllObjects];
  108. self.preparing = NO;
  109. }
  110. for (GIDAppCheckPrepareCompletion savedCompletion in callbacks) {
  111. savedCompletion(maybeError);
  112. }
  113. }];
  114. });
  115. }
  116. - (void)getLimitedUseTokenWithCompletion:(nullable GIDAppCheckTokenCompletion)completion {
  117. dispatch_async(self.workerQueue, ^{
  118. [self.appCheck limitedUseTokenWithCompletion:^(GACAppCheckTokenResult * _Nonnull result) {
  119. if (result.token) {
  120. [self.userDefaults setBool:YES forKey:kGIDAppCheckPreparedKey];
  121. }
  122. if (completion) {
  123. completion(result.token, result.error);
  124. }
  125. }];
  126. });
  127. }
  128. + (NSString *)appAttestResourceName {
  129. NSString *clientID = [NSBundle.mainBundle objectForInfoDictionaryKey:kGIDConfigClientIDKey];
  130. return [NSString stringWithFormat:kGIDAppAttestResourceNameFormat, clientID];
  131. }
  132. + (id<GACAppCheckProvider>)appAttestProvider {
  133. return [[GACAppAttestProvider alloc] initWithServiceName:kGIDAppAttestServiceName
  134. resourceName:[GIDAppCheck appAttestResourceName]
  135. baseURL:kGIDAppAttestBaseURL
  136. APIKey:nil
  137. keychainAccessGroup:nil
  138. requestHooks:nil];
  139. }
  140. + (id<GACAppCheckProvider>)debugAppCheckProviderWithAPIKey:(NSString *)APIKey {
  141. return [[GACAppCheckDebugProvider alloc] initWithServiceName:kGIDAppAttestServiceName
  142. resourceName:[GIDAppCheck appAttestResourceName]
  143. baseURL:kGIDAppAttestBaseURL
  144. APIKey:APIKey
  145. requestHooks:nil];
  146. }
  147. @end
  148. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST