GIDSignIn.m 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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/GIDEMMSupport.h"
  21. #import "GoogleSignIn/Sources/GIDKeychainHandler/API/GIDKeychainHandler.h"
  22. #import "GoogleSignIn/Sources/GIDKeychainHandler/Implementations/GIDKeychainHandler.h"
  23. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  24. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  25. #import "GoogleSignIn/Sources/GIDCallbackQueue.h"
  26. #import "GoogleSignIn/Sources/GIDScopes.h"
  27. #import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
  28. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  29. #import "GoogleSignIn/Sources/GIDAuthStateMigration.h"
  30. #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
  31. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  32. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  33. #import "GoogleSignIn/Sources/GIDProfileData_Private.h"
  34. #import "GoogleSignIn/Sources/GIDSignInResult_Private.h"
  35. #ifdef SWIFT_PACKAGE
  36. @import AppAuth;
  37. @import GTMAppAuth;
  38. @import GTMSessionFetcherCore;
  39. #else
  40. #import <AppAuth/OIDAuthState.h>
  41. #import <AppAuth/OIDAuthorizationRequest.h>
  42. #import <AppAuth/OIDAuthorizationResponse.h>
  43. #import <AppAuth/OIDAuthorizationService.h>
  44. #import <AppAuth/OIDError.h>
  45. #import <AppAuth/OIDExternalUserAgentSession.h>
  46. #import <AppAuth/OIDIDToken.h>
  47. #import <AppAuth/OIDResponseTypes.h>
  48. #import <AppAuth/OIDServiceConfiguration.h>
  49. #import <AppAuth/OIDTokenRequest.h>
  50. #import <AppAuth/OIDTokenResponse.h>
  51. #import <AppAuth/OIDURLQueryComponent.h>
  52. #import <GTMAppAuth/GTMAppAuthFetcherAuthorization+Keychain.h>
  53. #import <GTMAppAuth/GTMAppAuthFetcherAuthorization.h>
  54. #import <GTMSessionFetcher/GTMSessionFetcher.h>
  55. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  56. #import <AppAuth/OIDAuthorizationService+IOS.h>
  57. #elif TARGET_OS_OSX
  58. #import <AppAuth/OIDAuthorizationService+Mac.h>
  59. #endif
  60. #endif
  61. NS_ASSUME_NONNULL_BEGIN
  62. // The name of the query parameter used for logging the restart of auth from EMM callback.
  63. static NSString *const kEMMRestartAuthParameter = @"emmres";
  64. // The URL template for the authorization endpoint.
  65. static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";
  66. // The URL template for the token endpoint.
  67. static NSString *const kTokenURLTemplate = @"https://%@/token";
  68. // The URL template for the URL to get user info.
  69. static NSString *const kUserInfoURLTemplate = @"https://%@/oauth2/v3/userinfo?access_token=%@";
  70. // The URL template for the URL to revoke the token.
  71. static NSString *const kRevokeTokenURLTemplate = @"https://%@/o/oauth2/revoke?token=%@";
  72. // Expected path in the URL scheme to be handled.
  73. static NSString *const kBrowserCallbackPath = @"/oauth2callback";
  74. // Expected path for EMM callback.
  75. static NSString *const kEMMCallbackPath = @"/emmcallback";
  76. // The EMM support version
  77. static NSString *const kEMMVersion = @"1";
  78. // The error code for Google Identity.
  79. NSErrorDomain const kGIDSignInErrorDomain = @"com.google.GIDSignIn";
  80. // Keychain constants for saving state in the authentication flow.
  81. static NSString *const kGTMAppAuthKeychainName = @"auth";
  82. // Basic profile (Fat ID Token / userinfo endpoint) keys
  83. static NSString *const kBasicProfileEmailKey = @"email";
  84. static NSString *const kBasicProfilePictureKey = @"picture";
  85. static NSString *const kBasicProfileNameKey = @"name";
  86. static NSString *const kBasicProfileGivenNameKey = @"given_name";
  87. static NSString *const kBasicProfileFamilyNameKey = @"family_name";
  88. // Parameters in the callback URL coming back from browser.
  89. static NSString *const kAuthorizationCodeKeyName = @"code";
  90. static NSString *const kOAuth2ErrorKeyName = @"error";
  91. static NSString *const kOAuth2AccessDenied = @"access_denied";
  92. static NSString *const kEMMPasscodeInfoRequiredKeyName = @"emm_passcode_info_required";
  93. // Error string for unavailable keychain.
  94. static NSString *const kKeychainError = @"keychain error";
  95. // Error string for user cancelations.
  96. static NSString *const kUserCanceledError = @"The user canceled the sign-in flow.";
  97. // User preference key to detect fresh install of the app.
  98. static NSString *const kAppHasRunBeforeKey = @"GID_AppHasRunBefore";
  99. // Maximum retry interval in seconds for the fetcher.
  100. static const NSTimeInterval kFetcherMaxRetryInterval = 15.0;
  101. // The delay before the new sign-in flow can be presented after the existing one is cancelled.
  102. static const NSTimeInterval kPresentationDelayAfterCancel = 1.0;
  103. // Parameters for the auth and token exchange endpoints.
  104. static NSString *const kAudienceParameter = @"audience";
  105. // See b/11669751 .
  106. static NSString *const kOpenIDRealmParameter = @"openid.realm";
  107. static NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes";
  108. static NSString *const kLoginHintParameter = @"login_hint";
  109. static NSString *const kHostedDomainParameter = @"hd";
  110. // Minimum time to expiration for a restored access token.
  111. static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
  112. // Info.plist config keys
  113. static NSString *const kConfigClientIDKey = @"GIDClientID";
  114. static NSString *const kConfigServerClientIDKey = @"GIDServerClientID";
  115. static NSString *const kConfigHostedDomainKey = @"GIDHostedDomain";
  116. static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
  117. // The callback queue used for authentication flow.
  118. @interface GIDAuthFlow : GIDCallbackQueue
  119. @property(nonatomic, strong, nullable) OIDAuthState *authState;
  120. @property(nonatomic, strong, nullable) NSError *error;
  121. @property(nonatomic, copy, nullable) NSString *emmSupport;
  122. @property(nonatomic, nullable) GIDProfileData *profileData;
  123. @end
  124. @implementation GIDAuthFlow
  125. @end
  126. @implementation GIDSignIn {
  127. // This value is used when sign-in flows are resumed via the handling of a URL. Its value is
  128. // set when a sign-in flow is begun via |signInWithOptions:| when the options passed don't
  129. // represent a sign in continuation.
  130. GIDSignInInternalOptions *_currentOptions;
  131. // AppAuth configuration object.
  132. OIDServiceConfiguration *_appAuthConfiguration;
  133. // AppAuth external user-agent session state.
  134. id<OIDExternalUserAgentSession> _currentAuthorizationFlow;
  135. // Flag to indicate that the auth flow is restarting.
  136. BOOL _restarting;
  137. id<GIDKeychainHandler> _keychainHandler;
  138. }
  139. #pragma mark - Public methods
  140. // Handles the custom scheme URL opened by SFSafariViewController or the Device Policy App.
  141. //
  142. // For SFSafariViewController invoked via AppAuth, this method is used on iOS 10.
  143. // For the Device Policy App (EMM flow) this method is used on all iOS versions.
  144. - (BOOL)handleURL:(NSURL *)url {
  145. // Check if the callback path matches the expected one for a URL from Safari/Chrome/SafariVC.
  146. if ([url.path isEqual:kBrowserCallbackPath]) {
  147. if ([_currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) {
  148. _currentAuthorizationFlow = nil;
  149. return YES;
  150. }
  151. return NO;
  152. }
  153. // Check if the callback path matches the expected one for a URL from Google Device Policy app.
  154. if ([url.path isEqual:kEMMCallbackPath]) {
  155. return [self handleDevicePolicyAppURL:url];
  156. }
  157. return NO;
  158. }
  159. - (BOOL)hasPreviousSignIn {
  160. if ([_currentUser.authState isAuthorized]) {
  161. return YES;
  162. }
  163. OIDAuthState *authState = [_keychainHandler loadAuthState];
  164. return [authState isAuthorized];
  165. }
  166. - (void)restorePreviousSignInWithCompletion:(nullable void (^)(GIDGoogleUser *_Nullable user,
  167. NSError *_Nullable error))completion {
  168. [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCompletion:
  169. ^(GIDSignInResult *signInResult, NSError *error) {
  170. if (signInResult) {
  171. completion(signInResult.user, nil);
  172. } else {
  173. completion(nil, error);
  174. }
  175. }]];
  176. }
  177. - (BOOL)restorePreviousSignInNoRefresh {
  178. if (_currentUser) {
  179. return YES;
  180. }
  181. // Try retrieving an authorization object from the keychain.
  182. OIDAuthState *authState = [_keychainHandler loadAuthState];
  183. if (!authState) {
  184. return NO;
  185. }
  186. // Restore current user without refreshing the access token.
  187. OIDIDToken *idToken =
  188. [[OIDIDToken alloc] initWithIDTokenString:authState.lastTokenResponse.idToken];
  189. GIDProfileData *profileData = [self profileDataWithIDToken:idToken];
  190. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState profileData:profileData];
  191. self.currentUser = user;
  192. return YES;
  193. }
  194. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  195. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  196. hint:(nullable NSString *)hint
  197. completion:(nullable GIDSignInCompletion)completion {
  198. GIDSignInInternalOptions *options =
  199. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  200. presentingViewController:presentingViewController
  201. loginHint:hint
  202. addScopesFlow:NO
  203. completion:completion];
  204. [self signInWithOptions:options];
  205. }
  206. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  207. hint:(nullable NSString *)hint
  208. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  209. completion:(nullable GIDSignInCompletion)completion {
  210. GIDSignInInternalOptions *options =
  211. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  212. presentingViewController:presentingViewController
  213. loginHint:hint
  214. addScopesFlow:NO
  215. scopes:additionalScopes
  216. completion:completion];
  217. [self signInWithOptions:options];
  218. }
  219. - (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
  220. completion:(nullable GIDSignInCompletion)completion {
  221. [self signInWithPresentingViewController:presentingViewController
  222. hint:nil
  223. completion:completion];
  224. }
  225. - (void)addScopes:(NSArray<NSString *> *)scopes
  226. presentingViewController:(UIViewController *)presentingViewController
  227. completion:(nullable GIDSignInCompletion)completion {
  228. GIDConfiguration *configuration = self.currentUser.configuration;
  229. GIDSignInInternalOptions *options =
  230. [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
  231. presentingViewController:presentingViewController
  232. loginHint:self.currentUser.profile.email
  233. addScopesFlow:YES
  234. completion:completion];
  235. NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
  236. NSMutableSet<NSString *> *grantedScopes =
  237. [NSMutableSet setWithArray:self.currentUser.grantedScopes];
  238. // Check to see if all requested scopes have already been granted.
  239. if ([requestedScopes isSubsetOfSet:grantedScopes]) {
  240. // All requested scopes have already been granted, notify callback of failure.
  241. NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  242. code:kGIDSignInErrorCodeScopesAlreadyGranted
  243. userInfo:nil];
  244. if (completion) {
  245. dispatch_async(dispatch_get_main_queue(), ^{
  246. completion(nil, error);
  247. });
  248. }
  249. return;
  250. }
  251. // Use the union of granted and requested scopes.
  252. [grantedScopes unionSet:requestedScopes];
  253. options.scopes = [grantedScopes allObjects];
  254. [self signInWithOptions:options];
  255. }
  256. #elif TARGET_OS_OSX
  257. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  258. hint:(nullable NSString *)hint
  259. completion:(nullable GIDSignInCompletion)completion {
  260. GIDSignInInternalOptions *options =
  261. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  262. presentingWindow:presentingWindow
  263. loginHint:hint
  264. addScopesFlow:NO
  265. completion:completion];
  266. [self signInWithOptions:options];
  267. }
  268. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  269. completion:(nullable GIDSignInCompletion)completion {
  270. [self signInWithPresentingWindow:presentingWindow
  271. hint:nil
  272. completion:completion];
  273. }
  274. - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
  275. hint:(nullable NSString *)hint
  276. additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
  277. completion:(nullable GIDSignInCompletion)completion {
  278. GIDSignInInternalOptions *options =
  279. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  280. presentingWindow:presentingWindow
  281. loginHint:hint
  282. addScopesFlow:NO
  283. scopes:additionalScopes
  284. completion:completion];
  285. [self signInWithOptions:options];
  286. }
  287. - (void)addScopes:(NSArray<NSString *> *)scopes
  288. presentingWindow:(NSWindow *)presentingWindow
  289. completion:(nullable GIDSignInCompletion)completion {
  290. GIDConfiguration *configuration = self.currentUser.configuration;
  291. GIDSignInInternalOptions *options =
  292. [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
  293. presentingWindow:presentingWindow
  294. loginHint:self.currentUser.profile.email
  295. addScopesFlow:YES
  296. completion:completion];
  297. NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
  298. NSMutableSet<NSString *> *grantedScopes =
  299. [NSMutableSet setWithArray:self.currentUser.grantedScopes];
  300. // Check to see if all requested scopes have already been granted.
  301. if ([requestedScopes isSubsetOfSet:grantedScopes]) {
  302. // All requested scopes have already been granted, notify callback of failure.
  303. NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  304. code:kGIDSignInErrorCodeScopesAlreadyGranted
  305. userInfo:nil];
  306. if (completion) {
  307. dispatch_async(dispatch_get_main_queue(), ^{
  308. completion(nil, error);
  309. });
  310. }
  311. return;
  312. }
  313. // Use the union of granted and requested scopes.
  314. [grantedScopes unionSet:requestedScopes];
  315. options.scopes = [grantedScopes allObjects];
  316. [self signInWithOptions:options];
  317. }
  318. #endif // TARGET_OS_OSX
  319. - (void)signOut {
  320. // Clear the current user if there is one.
  321. if (_currentUser) {
  322. self.currentUser = nil;
  323. }
  324. // Remove all state from the keychain.
  325. [_keychainHandler removeAllKeychainEntries];
  326. }
  327. - (void)disconnectWithCompletion:(nullable GIDDisconnectCompletion)completion {
  328. OIDAuthState *authState = _currentUser.authState;
  329. if (!authState) {
  330. // Even the user is not signed in right now, we still need to remove any token saved in the
  331. // keychain.
  332. authState = [_keychainHandler loadAuthState];
  333. }
  334. // Either access or refresh token would work, but we won't have access token if the auth is
  335. // retrieved from keychain.
  336. NSString *token = authState.lastTokenResponse.accessToken;
  337. if (!token) {
  338. token = authState.lastTokenResponse.refreshToken;
  339. }
  340. if (!token) {
  341. [self signOut];
  342. // Nothing to do here, consider the operation successful.
  343. if (completion) {
  344. dispatch_async(dispatch_get_main_queue(), ^{
  345. completion(nil);
  346. });
  347. }
  348. return;
  349. }
  350. NSString *revokeURLString = [NSString stringWithFormat:kRevokeTokenURLTemplate,
  351. [GIDSignInPreferences googleAuthorizationServer], token];
  352. // Append logging parameter
  353. revokeURLString = [NSString stringWithFormat:@"%@&%@=%@&%@=%@",
  354. revokeURLString,
  355. kSDKVersionLoggingParameter,
  356. GIDVersion(),
  357. kEnvironmentLoggingParameter,
  358. GIDEnvironment()];
  359. NSURL *revokeURL = [NSURL URLWithString:revokeURLString];
  360. [self startFetchURL:revokeURL
  361. fromAuthState:authState
  362. withComment:@"GIDSignIn: revoke tokens"
  363. withCompletionHandler:^(NSData *data, NSError *error) {
  364. // Revoking an already revoked token seems always successful, which helps us here.
  365. if (!error) {
  366. [self signOut];
  367. }
  368. if (completion) {
  369. dispatch_async(dispatch_get_main_queue(), ^{
  370. completion(error);
  371. });
  372. }
  373. }];
  374. }
  375. #pragma mark - Custom getters and setters
  376. + (GIDSignIn *)sharedInstance {
  377. static dispatch_once_t once;
  378. static GIDSignIn *sharedInstance;
  379. dispatch_once(&once, ^{
  380. sharedInstance = [[self alloc] initPrivate];
  381. });
  382. return sharedInstance;
  383. }
  384. #pragma mark - Private methods
  385. - (id)initPrivate {
  386. GIDKeychainHandler *keychainHandler = [[GIDKeychainHandler alloc] init];
  387. return [self initWithKeychainHandler:keychainHandler];
  388. }
  389. - (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler {
  390. self = [super init];
  391. if (self) {
  392. // Get the bundle of the current executable.
  393. NSBundle *bundle = NSBundle.mainBundle;
  394. // If we have a bundle, try to set the active configuration from the bundle's Info.plist.
  395. if (bundle) {
  396. _configuration = [GIDSignIn configurationFromBundle:bundle];
  397. }
  398. // Check to see if the 3P app is being run for the first time after a fresh install.
  399. BOOL isFreshInstall = [self isFreshInstall];
  400. // If this is a fresh install, ensure that any pre-existing keychain data is purged.
  401. if (isFreshInstall) {
  402. [_keychainHandler removeAllKeychainEntries];
  403. }
  404. NSString *authorizationEnpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
  405. [GIDSignInPreferences googleAuthorizationServer]];
  406. NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
  407. [GIDSignInPreferences googleTokenServer]];
  408. _appAuthConfiguration = [[OIDServiceConfiguration alloc]
  409. initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEnpointURL]
  410. tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]];
  411. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  412. // Perform migration of auth state from old (before 5.0) versions of the SDK if needed.
  413. [GIDAuthStateMigration migrateIfNeededWithTokenURL:_appAuthConfiguration.tokenEndpoint
  414. callbackPath:kBrowserCallbackPath
  415. keychainName:kGTMAppAuthKeychainName
  416. isFreshInstall:isFreshInstall];
  417. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  418. _keychainHandler = keychainHandler;
  419. }
  420. return self;
  421. }
  422. // Does sanity check for parameters and then authenticates if necessary.
  423. - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
  424. // Options for continuation are not the options we want to cache. The purpose of caching the
  425. // options in the first place is to provide continuation flows with a starting place from which to
  426. // derive suitable options for the continuation!
  427. if (!options.continuation) {
  428. _currentOptions = options;
  429. }
  430. if (options.interactive) {
  431. // Ensure that a configuration has been provided.
  432. if (!_configuration) {
  433. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  434. [NSException raise:NSInvalidArgumentException
  435. format:@"No active configuration. Make sure GIDClientID is set in Info.plist."];
  436. return;
  437. }
  438. // Explicitly throw exception for missing client ID here. This must come before
  439. // scheme check because schemes rely on reverse client IDs.
  440. [self assertValidParameters];
  441. [self assertValidPresentingViewController];
  442. // If the application does not support the required URL schemes tell the developer so.
  443. GIDSignInCallbackSchemes *schemes =
  444. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  445. NSArray<NSString *> *unsupportedSchemes = [schemes unsupportedSchemes];
  446. if (unsupportedSchemes.count != 0) {
  447. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  448. [NSException raise:NSInvalidArgumentException
  449. format:@"Your app is missing support for the following URL schemes: %@",
  450. [unsupportedSchemes componentsJoinedByString:@", "]];
  451. }
  452. }
  453. // If this is a non-interactive flow, use cached authentication if possible.
  454. if (!options.interactive && _currentUser) {
  455. [_currentUser refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *unused, NSError *error) {
  456. if (error) {
  457. [self authenticateWithOptions:options];
  458. } else {
  459. if (options.completion) {
  460. self->_currentOptions = nil;
  461. dispatch_async(dispatch_get_main_queue(), ^{
  462. GIDSignInResult *signInResult =
  463. [[GIDSignInResult alloc] initWithGoogleUser:self->_currentUser serverAuthCode:nil];
  464. options.completion(signInResult, nil);
  465. });
  466. }
  467. }
  468. }];
  469. } else {
  470. [self authenticateWithOptions:options];
  471. }
  472. }
  473. #pragma mark - Authentication flow
  474. - (void)authenticateInteractivelyWithOptions:(GIDSignInInternalOptions *)options {
  475. GIDSignInCallbackSchemes *schemes =
  476. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  477. NSURL *redirectURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@:%@",
  478. [schemes clientIdentifierScheme],
  479. kBrowserCallbackPath]];
  480. NSString *emmSupport;
  481. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  482. emmSupport = [[self class] isOperatingSystemAtLeast9] ? kEMMVersion : nil;
  483. #elif TARGET_OS_MACCATALYST || TARGET_OS_OSX
  484. emmSupport = nil;
  485. #endif // TARGET_OS_MACCATALYST || TARGET_OS_OSX
  486. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  487. additionalParameters[kIncludeGrantedScopesParameter] = @"true";
  488. if (options.configuration.serverClientID) {
  489. additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
  490. }
  491. if (options.loginHint) {
  492. additionalParameters[kLoginHintParameter] = options.loginHint;
  493. }
  494. if (options.configuration.hostedDomain) {
  495. additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
  496. }
  497. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  498. [additionalParameters addEntriesFromDictionary:
  499. [GIDEMMSupport parametersWithParameters:options.extraParams
  500. emmSupport:emmSupport
  501. isPasscodeInfoRequired:NO]];
  502. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  503. [additionalParameters addEntriesFromDictionary:options.extraParams];
  504. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  505. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  506. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  507. OIDAuthorizationRequest *request =
  508. [[OIDAuthorizationRequest alloc] initWithConfiguration:_appAuthConfiguration
  509. clientId:options.configuration.clientID
  510. scopes:options.scopes
  511. redirectURL:redirectURL
  512. responseType:OIDResponseTypeCode
  513. additionalParameters:additionalParameters];
  514. _currentAuthorizationFlow = [OIDAuthorizationService
  515. presentAuthorizationRequest:request
  516. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  517. presentingViewController:options.presentingViewController
  518. #elif TARGET_OS_OSX
  519. presentingWindow:options.presentingWindow
  520. #endif // TARGET_OS_OSX
  521. callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
  522. NSError *_Nullable error) {
  523. [self processAuthorizationResponse:authorizationResponse
  524. error:error
  525. emmSupport:emmSupport];
  526. }];
  527. }
  528. - (void)processAuthorizationResponse:(OIDAuthorizationResponse *)authorizationResponse
  529. error:(NSError *)error
  530. emmSupport:(NSString *)emmSupport{
  531. if (_restarting) {
  532. // The auth flow is restarting, so the work here would be performed in the next round.
  533. _restarting = NO;
  534. return;
  535. }
  536. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] init];
  537. authFlow.emmSupport = emmSupport;
  538. if (authorizationResponse) {
  539. if (authorizationResponse.authorizationCode.length) {
  540. authFlow.authState = [[OIDAuthState alloc]
  541. initWithAuthorizationResponse:authorizationResponse];
  542. // perform auth code exchange
  543. [self maybeFetchToken:authFlow];
  544. } else {
  545. // There was a failure, convert to appropriate error code.
  546. NSString *errorString;
  547. GIDSignInErrorCode errorCode = kGIDSignInErrorCodeUnknown;
  548. NSDictionary<NSString *, NSObject *> *params = authorizationResponse.additionalParameters;
  549. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  550. if (authFlow.emmSupport) {
  551. [authFlow wait];
  552. BOOL isEMMError = [[GIDEMMErrorHandler sharedInstance]
  553. handleErrorFromResponse:params
  554. completion:^{
  555. [authFlow next];
  556. }];
  557. if (isEMMError) {
  558. errorCode = kGIDSignInErrorCodeEMM;
  559. }
  560. }
  561. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  562. errorString = (NSString *)params[kOAuth2ErrorKeyName];
  563. if ([errorString isEqualToString:kOAuth2AccessDenied]) {
  564. errorCode = kGIDSignInErrorCodeCanceled;
  565. }
  566. authFlow.error = [self errorWithString:errorString code:errorCode];
  567. }
  568. } else {
  569. NSString *errorString = [error localizedDescription];
  570. GIDSignInErrorCode errorCode = kGIDSignInErrorCodeUnknown;
  571. if (error.code == OIDErrorCodeUserCanceledAuthorizationFlow) {
  572. // The user has canceled the flow at the iOS modal dialog.
  573. errorString = kUserCanceledError;
  574. errorCode = kGIDSignInErrorCodeCanceled;
  575. }
  576. authFlow.error = [self errorWithString:errorString code:errorCode];
  577. }
  578. [self addDecodeIdTokenCallback:authFlow];
  579. [self addSaveAuthCallback:authFlow];
  580. [self addCompletionCallback:authFlow];
  581. }
  582. // Perform authentication with the provided options.
  583. - (void)authenticateWithOptions:(GIDSignInInternalOptions *)options {
  584. // If this is an interactive flow, we're not going to try to restore any saved auth state.
  585. if (options.interactive) {
  586. [self authenticateInteractivelyWithOptions:options];
  587. return;
  588. }
  589. // Try retrieving an authorization object from the keychain.
  590. OIDAuthState *authState = [_keychainHandler loadAuthState];
  591. if (![authState isAuthorized]) {
  592. // No valid auth in keychain, per documentation/spec, notify callback of failure.
  593. NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  594. code:kGIDSignInErrorCodeHasNoAuthInKeychain
  595. userInfo:nil];
  596. if (options.completion) {
  597. _currentOptions = nil;
  598. dispatch_async(dispatch_get_main_queue(), ^{
  599. options.completion(nil, error);
  600. });
  601. }
  602. return;
  603. }
  604. // Complete the auth flow using saved auth in keychain.
  605. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] init];
  606. authFlow.authState = authState;
  607. [self maybeFetchToken:authFlow];
  608. [self addDecodeIdTokenCallback:authFlow];
  609. [self addSaveAuthCallback:authFlow];
  610. [self addCompletionCallback:authFlow];
  611. }
  612. // Fetches the access token if necessary as part of the auth flow.
  613. - (void)maybeFetchToken:(GIDAuthFlow *)authFlow {
  614. OIDAuthState *authState = authFlow.authState;
  615. // Do nothing if we have an auth flow error or a restored access token that isn't near expiration.
  616. if (authFlow.error ||
  617. (authState.lastTokenResponse.accessToken &&
  618. [authState.lastTokenResponse.accessTokenExpirationDate timeIntervalSinceNow] >
  619. kMinimumRestoredAccessTokenTimeToExpire)) {
  620. return;
  621. }
  622. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  623. if (_currentOptions.configuration.serverClientID) {
  624. additionalParameters[kAudienceParameter] = _currentOptions.configuration.serverClientID;
  625. }
  626. if (_currentOptions.configuration.openIDRealm) {
  627. additionalParameters[kOpenIDRealmParameter] = _currentOptions.configuration.openIDRealm;
  628. }
  629. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  630. NSDictionary<NSString *, NSObject *> *params =
  631. authState.lastAuthorizationResponse.additionalParameters;
  632. NSString *passcodeInfoRequired = (NSString *)params[kEMMPasscodeInfoRequiredKeyName];
  633. [additionalParameters addEntriesFromDictionary:
  634. [GIDEMMSupport parametersWithParameters:@{}
  635. emmSupport:authFlow.emmSupport
  636. isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
  637. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  638. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  639. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  640. OIDTokenRequest *tokenRequest;
  641. if (!authState.lastTokenResponse.accessToken &&
  642. authState.lastAuthorizationResponse.authorizationCode) {
  643. tokenRequest = [authState.lastAuthorizationResponse
  644. tokenExchangeRequestWithAdditionalParameters:additionalParameters];
  645. } else {
  646. [additionalParameters
  647. addEntriesFromDictionary:authState.lastTokenResponse.request.additionalParameters];
  648. tokenRequest = [authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
  649. }
  650. [authFlow wait];
  651. [OIDAuthorizationService
  652. performTokenRequest:tokenRequest
  653. callback:^(OIDTokenResponse *_Nullable tokenResponse,
  654. NSError *_Nullable error) {
  655. [authState updateWithTokenResponse:tokenResponse error:error];
  656. authFlow.error = error;
  657. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  658. if (authFlow.emmSupport) {
  659. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *error) {
  660. authFlow.error = error;
  661. [authFlow next];
  662. }];
  663. } else {
  664. [authFlow next];
  665. }
  666. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  667. [authFlow next];
  668. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  669. }];
  670. }
  671. // Adds a callback to the auth flow to save the auth object to |self| and the keychain as well.
  672. - (void)addSaveAuthCallback:(GIDAuthFlow *)authFlow {
  673. __weak GIDAuthFlow *weakAuthFlow = authFlow;
  674. [authFlow addCallback:^() {
  675. GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
  676. OIDAuthState *authState = handlerAuthFlow.authState;
  677. if (authState && !handlerAuthFlow.error) {
  678. if (![self->_keychainHandler saveAuthState:authState]) {
  679. handlerAuthFlow.error = [self errorWithString:kKeychainError
  680. code:kGIDSignInErrorCodeKeychain];
  681. return;
  682. }
  683. if (self->_currentOptions.addScopesFlow) {
  684. [self->_currentUser updateWithTokenResponse:authState.lastTokenResponse
  685. authorizationResponse:authState.lastAuthorizationResponse
  686. profileData:handlerAuthFlow.profileData];
  687. } else {
  688. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState
  689. profileData:handlerAuthFlow.profileData];
  690. self.currentUser = user;
  691. }
  692. }
  693. }];
  694. }
  695. // Adds a callback to the auth flow to extract user data from the ID token where available and
  696. // make a userinfo request if necessary.
  697. - (void)addDecodeIdTokenCallback:(GIDAuthFlow *)authFlow {
  698. __weak GIDAuthFlow *weakAuthFlow = authFlow;
  699. [authFlow addCallback:^() {
  700. GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
  701. OIDAuthState *authState = handlerAuthFlow.authState;
  702. if (!authState || handlerAuthFlow.error) {
  703. return;
  704. }
  705. OIDIDToken *idToken =
  706. [[OIDIDToken alloc] initWithIDTokenString: authState.lastTokenResponse.idToken];
  707. // If the profile data are present in the ID token, use them.
  708. if (idToken) {
  709. handlerAuthFlow.profileData = [self profileDataWithIDToken:idToken];
  710. }
  711. // If we can't retrieve profile data from the ID token, make a userInfo request to fetch them.
  712. if (!handlerAuthFlow.profileData) {
  713. [handlerAuthFlow wait];
  714. NSURL *infoURL = [NSURL URLWithString:
  715. [NSString stringWithFormat:kUserInfoURLTemplate,
  716. [GIDSignInPreferences googleUserInfoServer],
  717. authState.lastTokenResponse.accessToken]];
  718. [self startFetchURL:infoURL
  719. fromAuthState:authState
  720. withComment:@"GIDSignIn: fetch basic profile info"
  721. withCompletionHandler:^(NSData *data, NSError *error) {
  722. if (data && !error) {
  723. NSError *jsonDeserializationError;
  724. NSDictionary<NSString *, NSString *> *profileDict =
  725. [NSJSONSerialization JSONObjectWithData:data
  726. options:NSJSONReadingMutableContainers
  727. error:&jsonDeserializationError];
  728. if (profileDict) {
  729. handlerAuthFlow.profileData = [[GIDProfileData alloc]
  730. initWithEmail:idToken.claims[kBasicProfileEmailKey]
  731. name:profileDict[kBasicProfileNameKey]
  732. givenName:profileDict[kBasicProfileGivenNameKey]
  733. familyName:profileDict[kBasicProfileFamilyNameKey]
  734. imageURL:[NSURL URLWithString:profileDict[kBasicProfilePictureKey]]];
  735. }
  736. }
  737. if (error) {
  738. handlerAuthFlow.error = error;
  739. }
  740. [handlerAuthFlow next];
  741. }];
  742. }
  743. }];
  744. }
  745. // Adds a callback to the auth flow to complete the flow by calling the sign-in callback.
  746. - (void)addCompletionCallback:(GIDAuthFlow *)authFlow {
  747. __weak GIDAuthFlow *weakAuthFlow = authFlow;
  748. [authFlow addCallback:^() {
  749. GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
  750. if (self->_currentOptions.completion) {
  751. GIDSignInCompletion completion = self->_currentOptions.completion;
  752. self->_currentOptions = nil;
  753. dispatch_async(dispatch_get_main_queue(), ^{
  754. if (handlerAuthFlow.error) {
  755. completion(nil, handlerAuthFlow.error);
  756. } else {
  757. OIDAuthState *authState = handlerAuthFlow.authState;
  758. NSString *_Nullable serverAuthCode =
  759. [authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
  760. GIDSignInResult *signInResult =
  761. [[GIDSignInResult alloc] initWithGoogleUser:self->_currentUser
  762. serverAuthCode:serverAuthCode];
  763. completion(signInResult, nil);
  764. }
  765. });
  766. }
  767. }];
  768. }
  769. - (void)startFetchURL:(NSURL *)URL
  770. fromAuthState:(OIDAuthState *)authState
  771. withComment:(NSString *)comment
  772. withCompletionHandler:(void (^)(NSData *, NSError *))handler {
  773. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  774. GTMSessionFetcher *fetcher;
  775. GTMAppAuthFetcherAuthorization *authorization =
  776. [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
  777. id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService;
  778. if (fetcherService) {
  779. fetcher = [fetcherService fetcherWithRequest:request];
  780. } else {
  781. fetcher = [GTMSessionFetcher fetcherWithRequest:request];
  782. }
  783. fetcher.retryEnabled = YES;
  784. fetcher.maxRetryInterval = kFetcherMaxRetryInterval;
  785. fetcher.comment = comment;
  786. [fetcher beginFetchWithCompletionHandler:handler];
  787. }
  788. // Parse incoming URL from the Google Device Policy app.
  789. - (BOOL)handleDevicePolicyAppURL:(NSURL *)url {
  790. OIDURLQueryComponent *queryComponent = [[OIDURLQueryComponent alloc] initWithURL:url];
  791. NSDictionary<NSString *, NSObject<NSCopying> *> *params = queryComponent.dictionaryValue;
  792. NSObject<NSCopying> *actionParam = params[@"action"];
  793. NSString *actionString =
  794. [actionParam isKindOfClass:[NSString class]] ? (NSString *)actionParam : nil;
  795. if (![@"restart_auth" isEqualToString:actionString]) {
  796. return NO;
  797. }
  798. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  799. if (!_currentOptions.presentingViewController) {
  800. return NO;
  801. }
  802. #elif TARGET_OS_OSX
  803. if (!_currentOptions.presentingWindow) {
  804. return NO;
  805. }
  806. #endif // TARGET_OS_OSX
  807. if (!_currentAuthorizationFlow) {
  808. return NO;
  809. }
  810. _restarting = YES;
  811. [_currentAuthorizationFlow cancel];
  812. _currentAuthorizationFlow = nil;
  813. _restarting = NO;
  814. NSDictionary<NSString *, NSString *> *extraParameters = @{ kEMMRestartAuthParameter : @"1" };
  815. // In iOS 13 the presentation of ASWebAuthenticationSession needs an anchor window,
  816. // so we need to wait until the previous presentation is completely gone to ensure the right
  817. // anchor window is used here.
  818. dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
  819. (int64_t)(kPresentationDelayAfterCancel * NSEC_PER_SEC)),
  820. dispatch_get_main_queue(), ^{
  821. [self signInWithOptions:[self->_currentOptions optionsWithExtraParameters:extraParameters
  822. forContinuation:YES]];
  823. });
  824. return YES;
  825. }
  826. #pragma mark - Helpers
  827. - (NSError *)errorWithString:(NSString *)errorString code:(GIDSignInErrorCode)code {
  828. if (errorString == nil) {
  829. errorString = @"Unknown error";
  830. }
  831. NSDictionary<NSString *, NSString *> *errorDict = @{ NSLocalizedDescriptionKey : errorString };
  832. return [NSError errorWithDomain:kGIDSignInErrorDomain
  833. code:code
  834. userInfo:errorDict];
  835. }
  836. + (BOOL)isOperatingSystemAtLeast9 {
  837. NSProcessInfo *processInfo = [NSProcessInfo processInfo];
  838. return [processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] &&
  839. [processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){.majorVersion = 9}];
  840. }
  841. // Asserts the parameters being valid.
  842. - (void)assertValidParameters {
  843. if (![_currentOptions.configuration.clientID length]) {
  844. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  845. [NSException raise:NSInvalidArgumentException
  846. format:@"You must specify |clientID| in |GIDConfiguration|"];
  847. }
  848. }
  849. // Assert that the presenting view controller has been set.
  850. - (void)assertValidPresentingViewController {
  851. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  852. if (!_currentOptions.presentingViewController)
  853. #elif TARGET_OS_OSX
  854. if (!_currentOptions.presentingWindow)
  855. #endif // TARGET_OS_OSX
  856. {
  857. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  858. [NSException raise:NSInvalidArgumentException
  859. format:@"|presentingViewController| must be set."];
  860. }
  861. }
  862. // Checks whether or not this is the first time the app runs.
  863. - (BOOL)isFreshInstall {
  864. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  865. if ([defaults boolForKey:kAppHasRunBeforeKey]) {
  866. return NO;
  867. }
  868. [defaults setBool:YES forKey:kAppHasRunBeforeKey];
  869. return YES;
  870. }
  871. // Generates user profile from OIDIDToken.
  872. - (GIDProfileData *)profileDataWithIDToken:(OIDIDToken *)idToken {
  873. if (!idToken ||
  874. !idToken.claims[kBasicProfilePictureKey] ||
  875. !idToken.claims[kBasicProfileNameKey] ||
  876. !idToken.claims[kBasicProfileGivenNameKey] ||
  877. !idToken.claims[kBasicProfileFamilyNameKey]) {
  878. return nil;
  879. }
  880. return [[GIDProfileData alloc]
  881. initWithEmail:idToken.claims[kBasicProfileEmailKey]
  882. name:idToken.claims[kBasicProfileNameKey]
  883. givenName:idToken.claims[kBasicProfileGivenNameKey]
  884. familyName:idToken.claims[kBasicProfileFamilyNameKey]
  885. imageURL:[NSURL URLWithString:idToken.claims[kBasicProfilePictureKey]]];
  886. }
  887. // Try to retrieve a configuration value from an |NSBundle|'s Info.plist for a given key.
  888. + (nullable NSString *)configValueFromBundle:(NSBundle *)bundle forKey:(NSString *)key {
  889. NSString *value;
  890. id configValue = [bundle objectForInfoDictionaryKey:key];
  891. if ([configValue isKindOfClass:[NSString class]]) {
  892. value = configValue;
  893. }
  894. return value;
  895. }
  896. // Try to generate a |GIDConfiguration| from an |NSBundle|'s Info.plist.
  897. + (nullable GIDConfiguration *)configurationFromBundle:(NSBundle *)bundle {
  898. GIDConfiguration *configuration;
  899. // Retrieve any valid config parameters from the bundle's Info.plist.
  900. NSString *clientID = [GIDSignIn configValueFromBundle:bundle forKey:kConfigClientIDKey];
  901. NSString *serverClientID = [GIDSignIn configValueFromBundle:bundle
  902. forKey:kConfigServerClientIDKey];
  903. NSString *hostedDomain = [GIDSignIn configValueFromBundle:bundle forKey:kConfigHostedDomainKey];
  904. NSString *openIDRealm = [GIDSignIn configValueFromBundle:bundle forKey:kConfigOpenIDRealmKey];
  905. // If we have at least a client ID, try to construct a configuration.
  906. if (clientID) {
  907. configuration = [[GIDConfiguration alloc] initWithClientID:clientID
  908. serverClientID:serverClientID
  909. hostedDomain:hostedDomain
  910. openIDRealm:openIDRealm];
  911. }
  912. return configuration;
  913. }
  914. @end
  915. NS_ASSUME_NONNULL_END