GIDSignIn.m 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  15. #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
  19. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h"
  20. #import "GoogleSignIn/Sources/GIDAuthStateMigration/GIDAuthStateMigration.h"
  21. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  22. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  23. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  24. #import "GoogleSignIn/Sources/GIDCallbackQueue.h"
  25. #import "GoogleSignIn/Sources/GIDScopes.h"
  26. #import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
  27. #import "GoogleSignIn/Sources/GIDClaimsInternalOptions.h"
  28. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  29. #import <AppCheckCore/GACAppCheckToken.h>
  30. #import "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h"
  31. #import "GoogleSignIn/Sources/GIDAppCheck/UI/GIDActivityIndicatorViewController.h"
  32. #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
  33. #import "GoogleSignIn/Sources/GIDTimedLoader/GIDTimedLoader.h"
  34. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  35. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  36. #import "GoogleSignIn/Sources/GIDProfileData_Private.h"
  37. #import "GoogleSignIn/Sources/GIDSignInResult_Private.h"
  38. @import GTMAppAuth;
  39. #ifdef SWIFT_PACKAGE
  40. @import AppAuth;
  41. @import GTMSessionFetcherCore;
  42. #else
  43. #import <AppAuth/OIDAuthState.h>
  44. #import <AppAuth/OIDAuthorizationRequest.h>
  45. #import <AppAuth/OIDAuthorizationResponse.h>
  46. #import <AppAuth/OIDAuthorizationService.h>
  47. #import <AppAuth/OIDError.h>
  48. #import <AppAuth/OIDExternalUserAgentSession.h>
  49. #import <AppAuth/OIDIDToken.h>
  50. #import <AppAuth/OIDResponseTypes.h>
  51. #import <AppAuth/OIDServiceConfiguration.h>
  52. #import <AppAuth/OIDTokenRequest.h>
  53. #import <AppAuth/OIDTokenResponse.h>
  54. #import <AppAuth/OIDURLQueryComponent.h>
  55. #import <GTMSessionFetcher/GTMSessionFetcher.h>
  56. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  57. #import <AppAuth/OIDAuthorizationService+IOS.h>
  58. #elif TARGET_OS_OSX
  59. #import <AppAuth/OIDAuthorizationService+Mac.h>
  60. #endif
  61. #endif
  62. NS_ASSUME_NONNULL_BEGIN
  63. // The name of the query parameter used for logging the restart of auth from EMM callback.
  64. static NSString *const kEMMRestartAuthParameter = @"emmres";
  65. // The URL template for the authorization endpoint.
  66. static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";
  67. // The URL template for the token endpoint.
  68. static NSString *const kTokenURLTemplate = @"https://%@/token";
  69. // The URL template for the URL to get user info.
  70. static NSString *const kUserInfoURLTemplate = @"https://%@/oauth2/v3/userinfo?access_token=%@";
  71. // The URL template for the URL to revoke the token.
  72. static NSString *const kRevokeTokenURLTemplate = @"https://%@/o/oauth2/revoke?token=%@";
  73. // Expected path in the URL scheme to be handled.
  74. static NSString *const kBrowserCallbackPath = @"/oauth2callback";
  75. // Expected path for EMM callback.
  76. static NSString *const kEMMCallbackPath = @"/emmcallback";
  77. // The EMM support version
  78. static NSString *const kEMMVersion = @"1";
  79. // The error code for Google Identity.
  80. NSErrorDomain const kGIDSignInErrorDomain = @"com.google.GIDSignIn";
  81. // Keychain constants for saving state in the authentication flow.
  82. static NSString *const kGTMAppAuthKeychainName = @"auth";
  83. // Basic profile (Fat ID Token / userinfo endpoint) keys
  84. static NSString *const kBasicProfileEmailKey = @"email";
  85. static NSString *const kBasicProfilePictureKey = @"picture";
  86. static NSString *const kBasicProfileNameKey = @"name";
  87. static NSString *const kBasicProfileGivenNameKey = @"given_name";
  88. static NSString *const kBasicProfileFamilyNameKey = @"family_name";
  89. // Parameters in the callback URL coming back from browser.
  90. static NSString *const kAuthorizationCodeKeyName = @"code";
  91. static NSString *const kOAuth2ErrorKeyName = @"error";
  92. static NSString *const kOAuth2AccessDenied = @"access_denied";
  93. static NSString *const kEMMPasscodeInfoRequiredKeyName = @"emm_passcode_info_required";
  94. // Error string for unavailable keychain.
  95. static NSString *const kKeychainError = @"keychain error";
  96. // Error string for user cancelations.
  97. static NSString *const kUserCanceledError = @"The user canceled the sign-in flow.";
  98. // User preference key to detect fresh install of the app.
  99. static NSString *const kAppHasRunBeforeKey = @"GID_AppHasRunBefore";
  100. // Maximum retry interval in seconds for the fetcher.
  101. static const NSTimeInterval kFetcherMaxRetryInterval = 15.0;
  102. // The delay before the new sign-in flow can be presented after the existing one is cancelled.
  103. static const NSTimeInterval kPresentationDelayAfterCancel = 1.0;
  104. // Parameters for the auth and token exchange endpoints.
  105. static NSString *const kAudienceParameter = @"audience";
  106. // See b/11669751 .
  107. static NSString *const kOpenIDRealmParameter = @"openid.realm";
  108. static NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes";
  109. static NSString *const kLoginHintParameter = @"login_hint";
  110. static NSString *const kHostedDomainParameter = @"hd";
  111. // Parameter for requesting the token claims.
  112. static NSString *const kClaimsParameter = @"claims";
  113. // Parameters for auth and token exchange endpoints using App Attest.
  114. static NSString *const kClientAssertionParameter = @"client_assertion";
  115. static NSString *const kClientAssertionTypeParameter = @"client_assertion_type";
  116. static NSString *const kClientAssertionTypeParameterValue =
  117. @"urn:ietf:params:oauth:client-assertion-type:appcheck";
  118. // Minimum time to expiration for a restored access token.
  119. static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
  120. // Info.plist config keys
  121. static NSString *const kConfigClientIDKey = @"GIDClientID";
  122. static NSString *const kConfigServerClientIDKey = @"GIDServerClientID";
  123. static NSString *const kConfigHostedDomainKey = @"GIDHostedDomain";
  124. static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
  125. // The callback queue used for authentication flow.
  126. @interface GIDAuthFlow : GIDCallbackQueue
  127. @property(nonatomic, strong, nullable) OIDAuthState *authState;
  128. @property(nonatomic, strong, nullable) NSError *error;
  129. @property(nonatomic, copy, nullable) NSString *emmSupport;
  130. @property(nonatomic, nullable) GIDProfileData *profileData;
  131. @end
  132. @implementation GIDAuthFlow
  133. @end
  134. @implementation GIDSignIn {
  135. // This value is used when sign-in flows are resumed via the handling of a URL. Its value is
  136. // set when a sign-in flow is begun via |signInWithOptions:| when the options passed don't
  137. // represent a sign in continuation.
  138. GIDSignInInternalOptions *_currentOptions;
  139. GIDClaimsInternalOptions *_claimsInternalOptions;
  140. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  141. GIDAppCheck *_appCheck API_AVAILABLE(ios(14));
  142. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  143. // AppAuth configuration object.
  144. OIDServiceConfiguration *_appAuthConfiguration;
  145. // AppAuth external user-agent session state.
  146. id<OIDExternalUserAgentSession> _currentAuthorizationFlow;
  147. // Flag to indicate that the auth flow is restarting.
  148. BOOL _restarting;
  149. // Keychain manager for GTMAppAuth
  150. GTMKeychainStore *_keychainStore;
  151. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  152. // The class used to manage presenting the loading screen for fetching app check tokens.
  153. GIDTimedLoader *_timedLoader;
  154. // Flag indicating developer's intent to use App Check.
  155. BOOL _configureAppCheckCalled;
  156. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  157. }
  158. #pragma mark - Public methods
  159. // Handles the custom scheme URL opened by SFSafariViewController or the Device Policy App.
  160. //
  161. // For SFSafariViewController invoked via AppAuth, this method is used on iOS 10.
  162. // For the Device Policy App (EMM flow) this method is used on all iOS versions.
  163. - (BOOL)handleURL:(NSURL *)url {
  164. // Check if the callback path matches the expected one for a URL from Safari/Chrome/SafariVC.
  165. if ([url.path isEqual:kBrowserCallbackPath]) {
  166. if ([_currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) {
  167. _currentAuthorizationFlow = nil;
  168. return YES;
  169. }
  170. return NO;
  171. }
  172. // Check if the callback path matches the expected one for a URL from Google Device Policy app.
  173. if ([url.path isEqual:kEMMCallbackPath]) {
  174. return [self handleDevicePolicyAppURL:url];
  175. }
  176. return NO;
  177. }
  178. - (BOOL)hasPreviousSignIn {
  179. if ([_currentUser.authState isAuthorized]) {
  180. return YES;
  181. }
  182. OIDAuthState *authState = [self loadAuthState];
  183. return [authState isAuthorized];
  184. }
  185. - (void)restorePreviousSignInWithCompletion:(nullable void (^)(GIDGoogleUser *_Nullable user,
  186. NSError *_Nullable error))completion {
  187. [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCompletion:
  188. ^(GIDSignInResult *signInResult, NSError *error) {
  189. if (!completion) {
  190. return;
  191. }
  192. if (signInResult) {
  193. completion(signInResult.user, nil);
  194. } else {
  195. completion(nil, error);
  196. }
  197. }]];
  198. }
  199. - (BOOL)restorePreviousSignInNoRefresh {
  200. if (_currentUser) {
  201. return YES;
  202. }
  203. // Try retrieving an authorization object from the keychain.
  204. OIDAuthState *authState = [self loadAuthState];
  205. if (!authState) {
  206. return NO;
  207. }
  208. // Restore current user without refreshing the access token.
  209. OIDIDToken *idToken =
  210. [[OIDIDToken alloc] initWithIDTokenString:authState.lastTokenResponse.idToken];
  211. GIDProfileData *profileData = [self profileDataWithIDToken:idToken];
  212. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState profileData:profileData];
  213. self.currentUser = user;
  214. return YES;
  215. }
  216. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  217. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  218. hint:(nullable NSString *)hint
  219. completion:(nullable GIDSignInCompletion)completion {
  220. GIDSignInInternalOptions *options =
  221. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  222. presentingViewController:presentingViewController
  223. loginHint:hint
  224. addScopesFlow:NO
  225. completion:completion];
  226. [self signInWithOptions:options];
  227. }
  228. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  229. hint:(nullable NSString *)hint
  230. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  231. completion:(nullable GIDSignInCompletion)completion {
  232. [self signInWithPresentingViewController:presentingViewController
  233. hint:hint
  234. additionalScopes:additionalScopes
  235. nonce:nil
  236. completion:completion];
  237. }
  238. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  239. hint:(nullable NSString *)hint
  240. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  241. nonce:(nullable NSString *)nonce
  242. completion:(nullable GIDSignInCompletion)completion {
  243. [self signInWithPresentingViewController:presentingViewController
  244. hint:hint
  245. additionalScopes:additionalScopes
  246. nonce:nonce
  247. claims:nil
  248. completion:completion];
  249. }
  250. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  251. claims:(nullable NSSet<GIDClaim *> *)claims
  252. completion:(nullable GIDSignInCompletion)completion {
  253. [self signInWithPresentingViewController:presentingViewController
  254. hint:nil
  255. claims:claims
  256. completion:completion];
  257. }
  258. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  259. hint:(nullable NSString *)hint
  260. claims:(nullable NSSet<GIDClaim *> *)claims
  261. completion:(nullable GIDSignInCompletion)completion {
  262. [self signInWithPresentingViewController:presentingViewController
  263. hint:hint
  264. additionalScopes:@[]
  265. claims:claims
  266. completion:completion];
  267. }
  268. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  269. hint:(nullable NSString *)hint
  270. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  271. claims:(nullable NSSet<GIDClaim *> *)claims
  272. completion:(nullable GIDSignInCompletion)completion {
  273. [self signInWithPresentingViewController:presentingViewController
  274. hint:hint
  275. additionalScopes:additionalScopes
  276. nonce:nil
  277. claims:claims
  278. completion:completion];
  279. }
  280. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  281. hint:(nullable NSString *)hint
  282. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  283. nonce:(nullable NSString *)nonce
  284. claims:(nullable NSSet<GIDClaim *> *)claims
  285. completion:(nullable GIDSignInCompletion)completion {
  286. GIDSignInInternalOptions *options =
  287. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  288. presentingViewController:presentingViewController
  289. loginHint:hint
  290. addScopesFlow:NO
  291. scopes:additionalScopes
  292. nonce:nonce
  293. claims:claims
  294. completion:completion];
  295. [self signInWithOptions:options];
  296. }
  297. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  298. completion:(nullable GIDSignInCompletion)completion {
  299. [self signInWithPresentingViewController:presentingViewController
  300. hint:nil
  301. completion:completion];
  302. }
  303. - (void)addScopes:(NSArray<NSString *> *)scopes
  304. presentingViewController:(UIViewController *)presentingViewController
  305. completion:(nullable GIDSignInCompletion)completion {
  306. GIDConfiguration *configuration = self.currentUser.configuration;
  307. GIDSignInInternalOptions *options =
  308. [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
  309. presentingViewController:presentingViewController
  310. loginHint:self.currentUser.profile.email
  311. addScopesFlow:YES
  312. completion:completion];
  313. OIDAuthorizationRequest *lastAuthorizationRequest =
  314. self.currentUser.authState.lastAuthorizationResponse.request;
  315. NSString *lastClaimsAsJSON =
  316. lastAuthorizationRequest.additionalParameters[kClaimsParameter];
  317. if (lastClaimsAsJSON) {
  318. options.claimsAsJSON = lastClaimsAsJSON;
  319. }
  320. NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
  321. NSMutableSet<NSString *> *grantedScopes =
  322. [NSMutableSet setWithArray:self.currentUser.grantedScopes];
  323. // Check to see if all requested scopes have already been granted.
  324. if ([requestedScopes isSubsetOfSet:grantedScopes]) {
  325. // All requested scopes have already been granted, notify callback of failure.
  326. NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  327. code:kGIDSignInErrorCodeScopesAlreadyGranted
  328. userInfo:nil];
  329. if (completion) {
  330. dispatch_async(dispatch_get_main_queue(), ^{
  331. completion(nil, error);
  332. });
  333. }
  334. return;
  335. }
  336. // Use the union of granted and requested scopes.
  337. [grantedScopes unionSet:requestedScopes];
  338. options.scopes = [grantedScopes allObjects];
  339. [self signInWithOptions:options];
  340. }
  341. #elif TARGET_OS_OSX
  342. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  343. hint:(nullable NSString *)hint
  344. completion:(nullable GIDSignInCompletion)completion {
  345. GIDSignInInternalOptions *options =
  346. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  347. presentingWindow:presentingWindow
  348. loginHint:hint
  349. addScopesFlow:NO
  350. completion:completion];
  351. [self signInWithOptions:options];
  352. }
  353. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  354. completion:(nullable GIDSignInCompletion)completion {
  355. [self signInWithPresentingWindow:presentingWindow
  356. hint:nil
  357. completion:completion];
  358. }
  359. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  360. hint:(nullable NSString *)hint
  361. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  362. completion:(nullable GIDSignInCompletion)completion {
  363. [self signInWithPresentingWindow:presentingWindow
  364. hint:hint
  365. additionalScopes:additionalScopes
  366. nonce:nil
  367. completion:completion];
  368. }
  369. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  370. hint:(nullable NSString *)hint
  371. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  372. nonce:(nullable NSString *)nonce
  373. completion:(nullable GIDSignInCompletion)completion {
  374. [self signInWithPresentingWindow:presentingWindow
  375. hint:hint
  376. additionalScopes:additionalScopes
  377. nonce:nonce
  378. claims:nil
  379. completion:completion];
  380. }
  381. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  382. claims:(nullable NSSet<GIDClaim *> *)claims
  383. completion:(nullable GIDSignInCompletion)completion {
  384. [self signInWithPresentingWindow:presentingWindow
  385. hint:nil
  386. claims:claims
  387. completion:completion];
  388. }
  389. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  390. hint:(nullable NSString *)hint
  391. claims:(nullable NSSet<GIDClaim *> *)claims
  392. completion:(nullable GIDSignInCompletion)completion {
  393. [self signInWithPresentingWindow:presentingWindow
  394. hint:hint
  395. additionalScopes:@[]
  396. claims:claims
  397. completion:completion];
  398. }
  399. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  400. hint:(nullable NSString *)hint
  401. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  402. claims:(nullable NSSet<GIDClaim *> *)claims
  403. completion:(nullable GIDSignInCompletion)completion {
  404. [self signInWithPresentingWindow:presentingWindow
  405. hint:hint
  406. additionalScopes:additionalScopes
  407. nonce:nil
  408. claims:claims
  409. completion:completion];
  410. }
  411. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  412. hint:(nullable NSString *)hint
  413. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  414. nonce:(nullable NSString *)nonce
  415. claims:(nullable NSSet<GIDClaim *> *)claims
  416. completion:(nullable GIDSignInCompletion)completion {
  417. GIDSignInInternalOptions *options =
  418. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  419. presentingWindow:presentingWindow
  420. loginHint:hint
  421. addScopesFlow:NO
  422. scopes:additionalScopes
  423. nonce:nonce
  424. claims:claims
  425. completion:completion];
  426. [self signInWithOptions:options];
  427. }
  428. - (void)addScopes:(NSArray<NSString *> *)scopes
  429. presentingWindow:(NSWindow *)presentingWindow
  430. completion:(nullable GIDSignInCompletion)completion {
  431. GIDConfiguration *configuration = self.currentUser.configuration;
  432. GIDSignInInternalOptions *options =
  433. [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
  434. presentingWindow:presentingWindow
  435. loginHint:self.currentUser.profile.email
  436. addScopesFlow:YES
  437. completion:completion];
  438. OIDAuthorizationRequest *lastAuthorizationRequest =
  439. self.currentUser.authState.lastAuthorizationResponse.request;
  440. NSString *lastClaimsAsJSON =
  441. lastAuthorizationRequest.additionalParameters[kClaimsParameter];
  442. if (lastClaimsAsJSON) {
  443. options.claimsAsJSON = lastClaimsAsJSON;
  444. }
  445. NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
  446. NSMutableSet<NSString *> *grantedScopes =
  447. [NSMutableSet setWithArray:self.currentUser.grantedScopes];
  448. // Check to see if all requested scopes have already been granted.
  449. if ([requestedScopes isSubsetOfSet:grantedScopes]) {
  450. // All requested scopes have already been granted, notify callback of failure.
  451. NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  452. code:kGIDSignInErrorCodeScopesAlreadyGranted
  453. userInfo:nil];
  454. if (completion) {
  455. dispatch_async(dispatch_get_main_queue(), ^{
  456. completion(nil, error);
  457. });
  458. }
  459. return;
  460. }
  461. // Use the union of granted and requested scopes.
  462. [grantedScopes unionSet:requestedScopes];
  463. options.scopes = [grantedScopes allObjects];
  464. [self signInWithOptions:options];
  465. }
  466. #endif // TARGET_OS_OSX
  467. - (void)signOut {
  468. // Clear the current user if there is one.
  469. if (_currentUser) {
  470. self.currentUser = nil;
  471. }
  472. // Remove all state from the keychain.
  473. [self removeAllKeychainEntries];
  474. }
  475. - (void)disconnectWithCompletion:(nullable GIDDisconnectCompletion)completion {
  476. OIDAuthState *authState = _currentUser.authState;
  477. if (!authState) {
  478. // Even the user is not signed in right now, we still need to remove any token saved in the
  479. // keychain.
  480. authState = [self loadAuthState];
  481. }
  482. // Either access or refresh token would work, but we won't have access token if the auth is
  483. // retrieved from keychain.
  484. NSString *token = authState.lastTokenResponse.accessToken;
  485. if (!token) {
  486. token = authState.lastTokenResponse.refreshToken;
  487. }
  488. if (!token) {
  489. [self signOut];
  490. // Nothing to do here, consider the operation successful.
  491. if (completion) {
  492. dispatch_async(dispatch_get_main_queue(), ^{
  493. completion(nil);
  494. });
  495. }
  496. return;
  497. }
  498. NSString *revokeURLString = [NSString stringWithFormat:kRevokeTokenURLTemplate,
  499. [GIDSignInPreferences googleAuthorizationServer], token];
  500. // Append logging parameter
  501. revokeURLString = [NSString stringWithFormat:@"%@&%@=%@&%@=%@",
  502. revokeURLString,
  503. kSDKVersionLoggingParameter,
  504. GIDVersion(),
  505. kEnvironmentLoggingParameter,
  506. GIDEnvironment()];
  507. NSURL *revokeURL = [NSURL URLWithString:revokeURLString];
  508. [self startFetchURL:revokeURL
  509. fromAuthState:authState
  510. withComment:@"GIDSignIn: revoke tokens"
  511. withCompletionHandler:^(NSData *data, NSError *error) {
  512. // Revoking an already revoked token seems always successful, which helps us here.
  513. if (!error) {
  514. [self signOut];
  515. }
  516. if (completion) {
  517. dispatch_async(dispatch_get_main_queue(), ^{
  518. completion(error);
  519. });
  520. }
  521. }];
  522. }
  523. #pragma mark - Custom getters and setters
  524. + (GIDSignIn *)sharedInstance {
  525. static dispatch_once_t once;
  526. static GIDSignIn *sharedInstance;
  527. dispatch_once(&once, ^{
  528. GTMKeychainStore *keychainStore =
  529. [[GTMKeychainStore alloc] initWithItemName:kGTMAppAuthKeychainName];
  530. GIDAuthStateMigration *authStateMigrationService =
  531. [[GIDAuthStateMigration alloc] initWithKeychainStore:keychainStore];
  532. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  533. if (@available(iOS 14.0, *)) {
  534. GIDAppCheck *appCheck = [GIDAppCheck appCheckUsingAppAttestProvider];
  535. sharedInstance = [[self alloc] initWithKeychainStore:keychainStore
  536. authStateMigrationService:authStateMigrationService
  537. appCheck:appCheck];
  538. }
  539. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  540. if (!sharedInstance) {
  541. sharedInstance = [[self alloc] initWithKeychainStore:keychainStore
  542. authStateMigrationService:authStateMigrationService];
  543. }
  544. });
  545. return sharedInstance;
  546. }
  547. #pragma mark - Configuring and pre-warming
  548. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  549. - (void)configureWithCompletion:(nullable void (^)(NSError * _Nullable))completion {
  550. @synchronized(self) {
  551. _configureAppCheckCalled = YES;
  552. [_appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  553. if (completion) {
  554. completion(error);
  555. }
  556. }];
  557. }
  558. }
  559. - (void)configureDebugProviderWithAPIKey:(NSString *)APIKey
  560. completion:(nullable void (^)(NSError * _Nullable))completion {
  561. @synchronized(self) {
  562. _appCheck = [GIDAppCheck appCheckUsingDebugProviderWithAPIKey:APIKey];
  563. [_appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  564. if (completion) {
  565. completion(error);
  566. }
  567. }];
  568. }
  569. }
  570. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  571. #pragma mark - Private methods
  572. - (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore
  573. authStateMigrationService:(GIDAuthStateMigration *)authStateMigrationService {
  574. self = [super init];
  575. if (self) {
  576. _keychainStore = keychainStore;
  577. _claimsInternalOptions = [[GIDClaimsInternalOptions alloc] init];
  578. // Get the bundle of the current executable.
  579. NSBundle *bundle = NSBundle.mainBundle;
  580. // If we have a bundle, try to set the active configuration from the bundle's Info.plist.
  581. if (bundle) {
  582. _configuration = [GIDSignIn configurationFromBundle:bundle];
  583. }
  584. // Check to see if the 3P app is being run for the first time after a fresh install.
  585. BOOL isFreshInstall = [self isFreshInstall];
  586. NSString *authorizationEnpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
  587. [GIDSignInPreferences googleAuthorizationServer]];
  588. NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
  589. [GIDSignInPreferences googleTokenServer]];
  590. _appAuthConfiguration = [[OIDServiceConfiguration alloc]
  591. initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEnpointURL]
  592. tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]];
  593. // Perform migration of auth state from old versions of the SDK if needed.
  594. [authStateMigrationService migrateIfNeededWithTokenURL:_appAuthConfiguration.tokenEndpoint
  595. callbackPath:kBrowserCallbackPath
  596. isFreshInstall:isFreshInstall];
  597. }
  598. return self;
  599. }
  600. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  601. - (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore
  602. authStateMigrationService:(GIDAuthStateMigration *)authStateMigrationService
  603. appCheck:(GIDAppCheck *)appCheck {
  604. self = [self initWithKeychainStore:keychainStore
  605. authStateMigrationService:authStateMigrationService];
  606. if (self) {
  607. _appCheck = appCheck;
  608. _configureAppCheckCalled = NO;
  609. }
  610. return self;
  611. }
  612. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  613. // Does sanity check for parameters and then authenticates if necessary.
  614. - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
  615. // Options for continuation are not the options we want to cache. The purpose of caching the
  616. // options in the first place is to provide continuation flows with a starting place from which to
  617. // derive suitable options for the continuation!
  618. if (!options.continuation) {
  619. _currentOptions = options;
  620. }
  621. if (options.interactive) {
  622. // Ensure that a configuration has been provided.
  623. if (!_configuration) {
  624. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  625. [NSException raise:NSInvalidArgumentException
  626. format:@"No active configuration. Make sure GIDClientID is set in Info.plist."];
  627. return;
  628. }
  629. // Explicitly throw exception for missing client ID here. This must come before
  630. // scheme check because schemes rely on reverse client IDs.
  631. [self assertValidParameters];
  632. [self assertValidPresentingViewController];
  633. // If the application does not support the required URL schemes tell the developer so.
  634. GIDSignInCallbackSchemes *schemes =
  635. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  636. NSArray<NSString *> *unsupportedSchemes = [schemes unsupportedSchemes];
  637. if (unsupportedSchemes.count != 0) {
  638. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  639. [NSException raise:NSInvalidArgumentException
  640. format:@"Your app is missing support for the following URL schemes: %@",
  641. [unsupportedSchemes componentsJoinedByString:@", "]];
  642. }
  643. }
  644. // If this is a non-interactive flow, use cached authentication if possible.
  645. if (!options.interactive && _currentUser) {
  646. [_currentUser refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *unused, NSError *error) {
  647. if (error) {
  648. [self authenticateWithOptions:options];
  649. } else {
  650. if (options.completion) {
  651. self->_currentOptions = nil;
  652. dispatch_async(dispatch_get_main_queue(), ^{
  653. GIDSignInResult *signInResult =
  654. [[GIDSignInResult alloc] initWithGoogleUser:self->_currentUser serverAuthCode:nil];
  655. options.completion(signInResult, nil);
  656. });
  657. }
  658. }
  659. }];
  660. } else {
  661. // Only serialize claims if options.claimsAsJSON isn't already set.
  662. if (!options.claimsAsJSON) {
  663. NSError *claimsError;
  664. // If claims are invalid or JSON serialization fails, return with an error.
  665. options.claimsAsJSON = [_claimsInternalOptions
  666. validatedJSONStringForClaims:options.claims
  667. error:&claimsError];
  668. if (claimsError) {
  669. if (options.completion) {
  670. _currentOptions = nil;
  671. dispatch_async(dispatch_get_main_queue(), ^{
  672. options.completion(nil, claimsError);
  673. });
  674. }
  675. return;
  676. }
  677. }
  678. [self authenticateWithOptions:options];
  679. }
  680. }
  681. #pragma mark - Authentication flow
  682. - (void)authenticateInteractivelyWithOptions:(GIDSignInInternalOptions *)options {
  683. NSString *emmSupport;
  684. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  685. emmSupport = [[self class] isOperatingSystemAtLeast9] ? kEMMVersion : nil;
  686. #elif TARGET_OS_MACCATALYST || TARGET_OS_OSX
  687. emmSupport = nil;
  688. #endif // TARGET_OS_MACCATALYST || TARGET_OS_OSX
  689. [self authorizationRequestWithOptions:options
  690. completion:^(OIDAuthorizationRequest * _Nullable request,
  691. NSError * _Nullable error) {
  692. self->_currentAuthorizationFlow =
  693. [OIDAuthorizationService presentAuthorizationRequest:request
  694. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  695. presentingViewController:options.presentingViewController
  696. #elif TARGET_OS_OSX
  697. presentingWindow:options.presentingWindow
  698. #endif // TARGET_OS_OSX
  699. callback:
  700. ^(OIDAuthorizationResponse *_Nullable authorizationResponse,
  701. NSError *_Nullable error) {
  702. [self processAuthorizationResponse:authorizationResponse
  703. error:error
  704. emmSupport:emmSupport];
  705. }];
  706. }];
  707. }
  708. - (void)authorizationRequestWithOptions:(GIDSignInInternalOptions *)options completion:
  709. (void (^)(OIDAuthorizationRequest *_Nullable request, NSError *_Nullable error))completion {
  710. BOOL shouldCreateAuthRequest = YES;
  711. NSMutableDictionary<NSString *, NSString *> *additionalParameters =
  712. [self additionalParametersFromOptions:options];
  713. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  714. if (@available(iOS 14.0, *)) {
  715. // Only use `_appCheck` (created via singleton `+[GIDSignIn sharedInstance]` call) if
  716. // `GIDAppCheck` has been successfully prepared OR if the developer has attempted to configure.
  717. // If former is false and the latter true, then preparation step failed for some reason; we
  718. // still want to try to pass along the app check token (it just may take longer since the
  719. // pre-warm step failed).
  720. if ([_appCheck isPrepared] || _configureAppCheckCalled) {
  721. shouldCreateAuthRequest = NO;
  722. UIViewController *presentingVC = options.presentingViewController;
  723. if (!_timedLoader) {
  724. _timedLoader = [[GIDTimedLoader alloc] initWithPresentingViewController:presentingVC];
  725. }
  726. [_timedLoader startTiming];
  727. [self->_appCheck getLimitedUseTokenWithCompletion:^(GACAppCheckToken * _Nullable token,
  728. NSError * _Nullable error) {
  729. if (token) {
  730. additionalParameters[kClientAssertionTypeParameter] = kClientAssertionTypeParameterValue;
  731. additionalParameters[kClientAssertionParameter] = token.token;
  732. }
  733. #if DEBUG
  734. if (error) {
  735. NSLog(@"[Google Sign-In iOS]: Error retrieving App Check limited use token: %@", error);
  736. }
  737. #endif
  738. OIDAuthorizationRequest *request = [self authorizationRequestWithOptions:options
  739. additionalParameters:additionalParameters];
  740. if (self->_timedLoader.animationStatus == GIDTimedLoaderAnimationStatusAnimating) {
  741. [self->_timedLoader stopTimingWithCompletion:^{
  742. completion(request, error);
  743. }];
  744. } else {
  745. completion(request, error);
  746. }
  747. }];
  748. }
  749. }
  750. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  751. if (shouldCreateAuthRequest) {
  752. OIDAuthorizationRequest *request = [self authorizationRequestWithOptions:options
  753. additionalParameters:additionalParameters];
  754. completion(request, nil);
  755. }
  756. }
  757. - (OIDAuthorizationRequest *)
  758. authorizationRequestWithOptions:(GIDSignInInternalOptions *)options
  759. additionalParameters:(NSDictionary<NSString *, NSString *> *)additionalParameters {
  760. OIDAuthorizationRequest *request;
  761. if (options.nonce) {
  762. request = [[OIDAuthorizationRequest alloc] initWithConfiguration:_appAuthConfiguration
  763. clientId:options.configuration.clientID
  764. scopes:options.scopes
  765. redirectURL:[self redirectURLWithOptions:options]
  766. responseType:OIDResponseTypeCode
  767. nonce:options.nonce
  768. additionalParameters:additionalParameters];
  769. } else {
  770. request = [[OIDAuthorizationRequest alloc] initWithConfiguration:_appAuthConfiguration
  771. clientId:options.configuration.clientID
  772. scopes:options.scopes
  773. redirectURL:[self redirectURLWithOptions:options]
  774. responseType:OIDResponseTypeCode
  775. additionalParameters:additionalParameters];
  776. }
  777. return request;
  778. }
  779. - (NSMutableDictionary<NSString *, NSString *> *)
  780. additionalParametersFromOptions:(GIDSignInInternalOptions *)options {
  781. NSString *emmSupport;
  782. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  783. emmSupport = [[self class] isOperatingSystemAtLeast9] ? kEMMVersion : nil;
  784. #elif TARGET_OS_MACCATALYST || TARGET_OS_OSX
  785. emmSupport = nil;
  786. #endif // TARGET_OS_MACCATALYST || TARGET_OS_OSX
  787. NSMutableDictionary<NSString *, NSString *> *additionalParameters =
  788. [[NSMutableDictionary alloc] init];
  789. additionalParameters[kIncludeGrantedScopesParameter] = @"true";
  790. if (options.configuration.serverClientID) {
  791. additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
  792. }
  793. if (options.loginHint) {
  794. additionalParameters[kLoginHintParameter] = options.loginHint;
  795. }
  796. if (options.configuration.hostedDomain) {
  797. additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
  798. }
  799. if (options.claimsAsJSON) {
  800. additionalParameters[kClaimsParameter] = options.claimsAsJSON;
  801. }
  802. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  803. [additionalParameters addEntriesFromDictionary:
  804. [GIDEMMSupport parametersWithParameters:options.extraParams
  805. emmSupport:emmSupport
  806. isPasscodeInfoRequired:NO]];
  807. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  808. [additionalParameters addEntriesFromDictionary:options.extraParams];
  809. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  810. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  811. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  812. return additionalParameters;
  813. }
  814. - (NSURL *)redirectURLWithOptions:(GIDSignInInternalOptions *)options {
  815. GIDSignInCallbackSchemes *schemes =
  816. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  817. NSURL *redirectURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@:%@",
  818. [schemes clientIdentifierScheme],
  819. kBrowserCallbackPath]];
  820. return redirectURL;
  821. }
  822. - (void)processAuthorizationResponse:(OIDAuthorizationResponse *)authorizationResponse
  823. error:(NSError *)error
  824. emmSupport:(NSString *)emmSupport {
  825. if (_restarting) {
  826. // The auth flow is restarting, so the work here would be performed in the next round.
  827. _restarting = NO;
  828. return;
  829. }
  830. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] init];
  831. authFlow.emmSupport = emmSupport;
  832. if (authorizationResponse) {
  833. if (authorizationResponse.authorizationCode.length) {
  834. authFlow.authState = [[OIDAuthState alloc]
  835. initWithAuthorizationResponse:authorizationResponse];
  836. // perform auth code exchange
  837. [self maybeFetchToken:authFlow];
  838. } else {
  839. // There was a failure, convert to appropriate error code.
  840. NSString *errorString;
  841. GIDSignInErrorCode errorCode = kGIDSignInErrorCodeUnknown;
  842. NSDictionary<NSString *, NSObject *> *params = authorizationResponse.additionalParameters;
  843. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  844. if (authFlow.emmSupport) {
  845. [authFlow wait];
  846. BOOL isEMMError = [[GIDEMMErrorHandler sharedInstance]
  847. handleErrorFromResponse:params
  848. completion:^{
  849. [authFlow next];
  850. }];
  851. if (isEMMError) {
  852. errorCode = kGIDSignInErrorCodeEMM;
  853. }
  854. }
  855. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  856. errorString = (NSString *)params[kOAuth2ErrorKeyName];
  857. if ([errorString isEqualToString:kOAuth2AccessDenied]) {
  858. errorCode = kGIDSignInErrorCodeCanceled;
  859. }
  860. authFlow.error = [self errorWithString:errorString code:errorCode];
  861. }
  862. } else {
  863. NSString *errorString = [error localizedDescription];
  864. GIDSignInErrorCode errorCode = kGIDSignInErrorCodeUnknown;
  865. if (error.code == OIDErrorCodeUserCanceledAuthorizationFlow ||
  866. error.code == OIDErrorCodeProgramCanceledAuthorizationFlow) {
  867. // The user has canceled the flow at the iOS modal dialog.
  868. errorString = kUserCanceledError;
  869. errorCode = kGIDSignInErrorCodeCanceled;
  870. }
  871. authFlow.error = [self errorWithString:errorString code:errorCode];
  872. }
  873. [self addDecodeIdTokenCallback:authFlow];
  874. [self addSaveAuthCallback:authFlow];
  875. [self addCompletionCallback:authFlow];
  876. }
  877. // Perform authentication with the provided options.
  878. - (void)authenticateWithOptions:(GIDSignInInternalOptions *)options {
  879. // If this is an interactive flow, we're not going to try to restore any saved auth state.
  880. if (options.interactive) {
  881. [self authenticateInteractivelyWithOptions:options];
  882. return;
  883. }
  884. // Try retrieving an authorization object from the keychain.
  885. OIDAuthState *authState = [self loadAuthState];
  886. if (![authState isAuthorized]) {
  887. // No valid auth in keychain, per documentation/spec, notify callback of failure.
  888. NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  889. code:kGIDSignInErrorCodeHasNoAuthInKeychain
  890. userInfo:nil];
  891. if (options.completion) {
  892. _currentOptions = nil;
  893. dispatch_async(dispatch_get_main_queue(), ^{
  894. options.completion(nil, error);
  895. });
  896. }
  897. return;
  898. }
  899. // Complete the auth flow using saved auth in keychain.
  900. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] init];
  901. authFlow.authState = authState;
  902. [self maybeFetchToken:authFlow];
  903. [self addDecodeIdTokenCallback:authFlow];
  904. [self addSaveAuthCallback:authFlow];
  905. [self addCompletionCallback:authFlow];
  906. }
  907. // Fetches the access token if necessary as part of the auth flow.
  908. - (void)maybeFetchToken:(GIDAuthFlow *)authFlow {
  909. OIDAuthState *authState = authFlow.authState;
  910. // Do nothing if we have an auth flow error or a restored access token that isn't near expiration.
  911. if (authFlow.error ||
  912. (authState.lastTokenResponse.accessToken &&
  913. [authState.lastTokenResponse.accessTokenExpirationDate timeIntervalSinceNow] >
  914. kMinimumRestoredAccessTokenTimeToExpire)) {
  915. return;
  916. }
  917. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  918. if (_currentOptions.configuration.serverClientID) {
  919. additionalParameters[kAudienceParameter] = _currentOptions.configuration.serverClientID;
  920. }
  921. if (_currentOptions.configuration.openIDRealm) {
  922. additionalParameters[kOpenIDRealmParameter] = _currentOptions.configuration.openIDRealm;
  923. }
  924. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  925. NSDictionary<NSString *, NSObject *> *params =
  926. authState.lastAuthorizationResponse.additionalParameters;
  927. NSString *passcodeInfoRequired = (NSString *)params[kEMMPasscodeInfoRequiredKeyName];
  928. [additionalParameters addEntriesFromDictionary:
  929. [GIDEMMSupport parametersWithParameters:@{}
  930. emmSupport:authFlow.emmSupport
  931. isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
  932. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  933. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  934. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  935. OIDTokenRequest *tokenRequest;
  936. if (!authState.lastTokenResponse.accessToken &&
  937. authState.lastAuthorizationResponse.authorizationCode) {
  938. tokenRequest = [authState.lastAuthorizationResponse
  939. tokenExchangeRequestWithAdditionalParameters:additionalParameters];
  940. } else {
  941. [additionalParameters
  942. addEntriesFromDictionary:authState.lastTokenResponse.request.additionalParameters];
  943. tokenRequest = [authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
  944. }
  945. [authFlow wait];
  946. [OIDAuthorizationService performTokenRequest:tokenRequest
  947. originalAuthorizationResponse:authFlow.authState.lastAuthorizationResponse
  948. callback:^(OIDTokenResponse *_Nullable tokenResponse,
  949. NSError *_Nullable error) {
  950. [authState updateWithTokenResponse:tokenResponse error:error];
  951. authFlow.error = error;
  952. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  953. if (authFlow.emmSupport) {
  954. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *error) {
  955. authFlow.error = error;
  956. [authFlow next];
  957. }];
  958. } else {
  959. [authFlow next];
  960. }
  961. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  962. [authFlow next];
  963. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  964. }];
  965. }
  966. // Adds a callback to the auth flow to save the auth object to |self| and the keychain as well.
  967. - (void)addSaveAuthCallback:(GIDAuthFlow *)authFlow {
  968. __weak GIDAuthFlow *weakAuthFlow = authFlow;
  969. [authFlow addCallback:^() {
  970. GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
  971. OIDAuthState *authState = handlerAuthFlow.authState;
  972. if (authState && !handlerAuthFlow.error) {
  973. if (![self saveAuthState:authState]) {
  974. handlerAuthFlow.error = [self errorWithString:kKeychainError
  975. code:kGIDSignInErrorCodeKeychain];
  976. return;
  977. }
  978. if (self->_currentOptions.addScopesFlow) {
  979. [self->_currentUser updateWithTokenResponse:authState.lastTokenResponse
  980. authorizationResponse:authState.lastAuthorizationResponse
  981. profileData:handlerAuthFlow.profileData];
  982. } else {
  983. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState
  984. profileData:handlerAuthFlow.profileData];
  985. self.currentUser = user;
  986. }
  987. }
  988. }];
  989. }
  990. // Adds a callback to the auth flow to extract user data from the ID token where available and
  991. // make a userinfo request if necessary.
  992. - (void)addDecodeIdTokenCallback:(GIDAuthFlow *)authFlow {
  993. __weak GIDAuthFlow *weakAuthFlow = authFlow;
  994. [authFlow addCallback:^() {
  995. GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
  996. OIDAuthState *authState = handlerAuthFlow.authState;
  997. if (!authState || handlerAuthFlow.error) {
  998. return;
  999. }
  1000. OIDIDToken *idToken =
  1001. [[OIDIDToken alloc] initWithIDTokenString: authState.lastTokenResponse.idToken];
  1002. // If the profile data are present in the ID token, use them.
  1003. if (idToken) {
  1004. handlerAuthFlow.profileData = [self profileDataWithIDToken:idToken];
  1005. }
  1006. // If we can't retrieve profile data from the ID token, make a userInfo request to fetch them.
  1007. if (!handlerAuthFlow.profileData) {
  1008. [handlerAuthFlow wait];
  1009. NSURL *infoURL = [NSURL URLWithString:
  1010. [NSString stringWithFormat:kUserInfoURLTemplate,
  1011. [GIDSignInPreferences googleUserInfoServer],
  1012. authState.lastTokenResponse.accessToken]];
  1013. [self startFetchURL:infoURL
  1014. fromAuthState:authState
  1015. withComment:@"GIDSignIn: fetch basic profile info"
  1016. withCompletionHandler:^(NSData *data, NSError *error) {
  1017. if (data && !error) {
  1018. NSError *jsonDeserializationError;
  1019. NSDictionary<NSString *, NSString *> *profileDict =
  1020. [NSJSONSerialization JSONObjectWithData:data
  1021. options:NSJSONReadingMutableContainers
  1022. error:&jsonDeserializationError];
  1023. if (profileDict) {
  1024. handlerAuthFlow.profileData = [[GIDProfileData alloc]
  1025. initWithEmail:idToken.claims[kBasicProfileEmailKey]
  1026. name:profileDict[kBasicProfileNameKey]
  1027. givenName:profileDict[kBasicProfileGivenNameKey]
  1028. familyName:profileDict[kBasicProfileFamilyNameKey]
  1029. imageURL:[NSURL URLWithString:profileDict[kBasicProfilePictureKey]]];
  1030. }
  1031. }
  1032. if (error) {
  1033. handlerAuthFlow.error = error;
  1034. }
  1035. [handlerAuthFlow next];
  1036. }];
  1037. }
  1038. }];
  1039. }
  1040. // Adds a callback to the auth flow to complete the flow by calling the sign-in callback.
  1041. - (void)addCompletionCallback:(GIDAuthFlow *)authFlow {
  1042. __weak GIDAuthFlow *weakAuthFlow = authFlow;
  1043. [authFlow addCallback:^() {
  1044. GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
  1045. if (self->_currentOptions.completion) {
  1046. GIDSignInCompletion completion = self->_currentOptions.completion;
  1047. self->_currentOptions = nil;
  1048. dispatch_async(dispatch_get_main_queue(), ^{
  1049. if (handlerAuthFlow.error) {
  1050. completion(nil, handlerAuthFlow.error);
  1051. } else {
  1052. OIDAuthState *authState = handlerAuthFlow.authState;
  1053. NSString *_Nullable serverAuthCode =
  1054. [authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
  1055. GIDSignInResult *signInResult =
  1056. [[GIDSignInResult alloc] initWithGoogleUser:self->_currentUser
  1057. serverAuthCode:serverAuthCode];
  1058. completion(signInResult, nil);
  1059. }
  1060. });
  1061. }
  1062. }];
  1063. }
  1064. - (void)startFetchURL:(NSURL *)URL
  1065. fromAuthState:(OIDAuthState *)authState
  1066. withComment:(NSString *)comment
  1067. withCompletionHandler:(void (^)(NSData *, NSError *))handler {
  1068. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  1069. GTMSessionFetcher *fetcher;
  1070. GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState];
  1071. id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService;
  1072. if (fetcherService) {
  1073. fetcher = [fetcherService fetcherWithRequest:request];
  1074. } else {
  1075. fetcher = [GTMSessionFetcher fetcherWithRequest:request];
  1076. }
  1077. fetcher.retryEnabled = YES;
  1078. fetcher.maxRetryInterval = kFetcherMaxRetryInterval;
  1079. fetcher.comment = comment;
  1080. [fetcher beginFetchWithCompletionHandler:handler];
  1081. }
  1082. // Parse incoming URL from the Google Device Policy app.
  1083. - (BOOL)handleDevicePolicyAppURL:(NSURL *)url {
  1084. OIDURLQueryComponent *queryComponent = [[OIDURLQueryComponent alloc] initWithURL:url];
  1085. NSDictionary<NSString *, NSObject<NSCopying> *> *params = queryComponent.dictionaryValue;
  1086. NSObject<NSCopying> *actionParam = params[@"action"];
  1087. NSString *actionString =
  1088. [actionParam isKindOfClass:[NSString class]] ? (NSString *)actionParam : nil;
  1089. if (![@"restart_auth" isEqualToString:actionString]) {
  1090. return NO;
  1091. }
  1092. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  1093. if (!_currentOptions.presentingViewController) {
  1094. return NO;
  1095. }
  1096. #elif TARGET_OS_OSX
  1097. if (!_currentOptions.presentingWindow) {
  1098. return NO;
  1099. }
  1100. #endif // TARGET_OS_OSX
  1101. if (!_currentAuthorizationFlow) {
  1102. return NO;
  1103. }
  1104. _restarting = YES;
  1105. [_currentAuthorizationFlow cancel];
  1106. _currentAuthorizationFlow = nil;
  1107. _restarting = NO;
  1108. NSDictionary<NSString *, NSString *> *extraParameters = @{ kEMMRestartAuthParameter : @"1" };
  1109. // In iOS 13 the presentation of ASWebAuthenticationSession needs an anchor window,
  1110. // so we need to wait until the previous presentation is completely gone to ensure the right
  1111. // anchor window is used here.
  1112. dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
  1113. (int64_t)(kPresentationDelayAfterCancel * NSEC_PER_SEC)),
  1114. dispatch_get_main_queue(), ^{
  1115. [self signInWithOptions:[self->_currentOptions optionsWithExtraParameters:extraParameters
  1116. forContinuation:YES]];
  1117. });
  1118. return YES;
  1119. }
  1120. #pragma mark - Helpers
  1121. - (NSError *)errorWithString:(NSString *)errorString code:(GIDSignInErrorCode)code {
  1122. if (errorString == nil) {
  1123. errorString = @"Unknown error";
  1124. }
  1125. NSDictionary<NSString *, NSString *> *errorDict = @{ NSLocalizedDescriptionKey : errorString };
  1126. return [NSError errorWithDomain:kGIDSignInErrorDomain
  1127. code:code
  1128. userInfo:errorDict];
  1129. }
  1130. + (BOOL)isOperatingSystemAtLeast9 {
  1131. NSProcessInfo *processInfo = [NSProcessInfo processInfo];
  1132. return [processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] &&
  1133. [processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){.majorVersion = 9}];
  1134. }
  1135. // Asserts the parameters being valid.
  1136. - (void)assertValidParameters {
  1137. if (![_currentOptions.configuration.clientID length]) {
  1138. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  1139. [NSException raise:NSInvalidArgumentException
  1140. format:@"You must specify |clientID| in |GIDConfiguration|"];
  1141. }
  1142. }
  1143. // Assert that the presenting view controller has been set.
  1144. - (void)assertValidPresentingViewController {
  1145. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  1146. if (!_currentOptions.presentingViewController)
  1147. #elif TARGET_OS_OSX
  1148. if (!_currentOptions.presentingWindow)
  1149. #endif // TARGET_OS_OSX
  1150. {
  1151. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  1152. [NSException raise:NSInvalidArgumentException
  1153. format:@"|presentingViewController| must be set."];
  1154. }
  1155. }
  1156. // Checks whether or not this is the first time the app runs.
  1157. - (BOOL)isFreshInstall {
  1158. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  1159. if ([defaults boolForKey:kAppHasRunBeforeKey]) {
  1160. return NO;
  1161. }
  1162. [defaults setBool:YES forKey:kAppHasRunBeforeKey];
  1163. return YES;
  1164. }
  1165. - (void)removeAllKeychainEntries {
  1166. [_keychainStore removeAuthSessionWithError:nil];
  1167. }
  1168. - (BOOL)saveAuthState:(OIDAuthState *)authState {
  1169. GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState];
  1170. NSError *error;
  1171. [_keychainStore saveAuthSession:authorization error:&error];
  1172. return error == nil;
  1173. }
  1174. - (OIDAuthState *)loadAuthState {
  1175. GTMAuthSession *authorization = [_keychainStore retrieveAuthSessionWithError:nil];
  1176. return authorization.authState;
  1177. }
  1178. // Generates user profile from OIDIDToken.
  1179. - (GIDProfileData *)profileDataWithIDToken:(OIDIDToken *)idToken {
  1180. if (!idToken ||
  1181. !idToken.claims[kBasicProfilePictureKey] ||
  1182. !idToken.claims[kBasicProfileNameKey] ||
  1183. !idToken.claims[kBasicProfileGivenNameKey] ||
  1184. !idToken.claims[kBasicProfileFamilyNameKey]) {
  1185. return nil;
  1186. }
  1187. return [[GIDProfileData alloc]
  1188. initWithEmail:idToken.claims[kBasicProfileEmailKey]
  1189. name:idToken.claims[kBasicProfileNameKey]
  1190. givenName:idToken.claims[kBasicProfileGivenNameKey]
  1191. familyName:idToken.claims[kBasicProfileFamilyNameKey]
  1192. imageURL:[NSURL URLWithString:idToken.claims[kBasicProfilePictureKey]]];
  1193. }
  1194. // Try to retrieve a configuration value from an |NSBundle|'s Info.plist for a given key.
  1195. + (nullable NSString *)configValueFromBundle:(NSBundle *)bundle forKey:(NSString *)key {
  1196. NSString *value;
  1197. id configValue = [bundle objectForInfoDictionaryKey:key];
  1198. if ([configValue isKindOfClass:[NSString class]]) {
  1199. value = configValue;
  1200. }
  1201. return value;
  1202. }
  1203. // Try to generate a |GIDConfiguration| from an |NSBundle|'s Info.plist.
  1204. + (nullable GIDConfiguration *)configurationFromBundle:(NSBundle *)bundle {
  1205. GIDConfiguration *configuration;
  1206. // Retrieve any valid config parameters from the bundle's Info.plist.
  1207. NSString *clientID = [GIDSignIn configValueFromBundle:bundle forKey:kConfigClientIDKey];
  1208. NSString *serverClientID = [GIDSignIn configValueFromBundle:bundle
  1209. forKey:kConfigServerClientIDKey];
  1210. NSString *hostedDomain = [GIDSignIn configValueFromBundle:bundle forKey:kConfigHostedDomainKey];
  1211. NSString *openIDRealm = [GIDSignIn configValueFromBundle:bundle forKey:kConfigOpenIDRealmKey];
  1212. // If we have at least a client ID, try to construct a configuration.
  1213. if (clientID) {
  1214. configuration = [[GIDConfiguration alloc] initWithClientID:clientID
  1215. serverClientID:serverClientID
  1216. hostedDomain:hostedDomain
  1217. openIDRealm:openIDRealm];
  1218. }
  1219. return configuration;
  1220. }
  1221. @end
  1222. NS_ASSUME_NONNULL_END