Skip to content

Commit ae5276c

Browse files
authored
Add a configuration property populated via Info.plist (#228)
1 parent b2f2360 commit ae5276c

File tree

12 files changed

+271
-182
lines changed

12 files changed

+271
-182
lines changed

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@
135135
// Minimum time to expiration for a restored access token.
136136
static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
137137

138+
// Info.plist config keys
139+
static NSString *const kConfigClientIDKey = @"GIDClientID";
140+
static NSString *const kConfigServerClientIDKey = @"GIDServerClientID";
141+
static NSString *const kConfigHostedDomainKey = @"GIDHostedDomain";
142+
static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
143+
138144
// The callback queue used for authentication flow.
139145
@interface GIDAuthFlow : GIDCallbackQueue
140146

@@ -214,26 +220,24 @@ - (BOOL)restorePreviousSignInNoRefresh {
214220

215221
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
216222

217-
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
218-
presentingViewController:(UIViewController *)presentingViewController
219-
hint:(nullable NSString *)hint
220-
completion:(nullable GIDSignInCompletion)completion {
223+
- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
224+
hint:(nullable NSString *)hint
225+
completion:(nullable GIDSignInCompletion)completion {
221226
GIDSignInInternalOptions *options =
222-
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
227+
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
223228
presentingViewController:presentingViewController
224229
loginHint:hint
225230
addScopesFlow:NO
226231
completion:completion];
227232
[self signInWithOptions:options];
228233
}
229234

230-
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
231-
presentingViewController:(UIViewController *)presentingViewController
232-
hint:(nullable NSString *)hint
233-
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
234-
completion:(nullable GIDSignInCompletion)completion {
235+
- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
236+
hint:(nullable NSString *)hint
237+
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
238+
completion:(nullable GIDSignInCompletion)completion {
235239
GIDSignInInternalOptions *options =
236-
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
240+
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
237241
presentingViewController:presentingViewController
238242
loginHint:hint
239243
addScopesFlow:NO
@@ -242,13 +246,11 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration
242246
[self signInWithOptions:options];
243247
}
244248

245-
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
246-
presentingViewController:(UIViewController *)presentingViewController
247-
completion:(nullable GIDSignInCompletion)completion {
248-
[self signInWithConfiguration:configuration
249-
presentingViewController:presentingViewController
250-
hint:nil
251-
completion:completion];
249+
- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
250+
completion:(nullable GIDSignInCompletion)completion {
251+
[self signInWithPresentingViewController:presentingViewController
252+
hint:nil
253+
completion:completion];
252254
}
253255

254256
- (void)addScopes:(NSArray<NSString *> *)scopes
@@ -307,35 +309,31 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
307309

308310
#elif TARGET_OS_OSX
309311

310-
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
311-
presentingWindow:(NSWindow *)presentingWindow
312-
hint:(nullable NSString *)hint
313-
completion:(nullable GIDSignInCompletion)completion {
312+
- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
313+
hint:(nullable NSString *)hint
314+
completion:(nullable GIDSignInCompletion)completion {
314315
GIDSignInInternalOptions *options =
315-
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
316+
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
316317
presentingWindow:presentingWindow
317318
loginHint:hint
318319
addScopesFlow:NO
319320
completion:completion];
320321
[self signInWithOptions:options];
321322
}
322323

323-
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
324-
presentingWindow:(NSWindow *)presentingWindow
325-
completion:(nullable GIDSignInCompletion)completion {
326-
[self signInWithConfiguration:configuration
327-
presentingWindow:presentingWindow
328-
hint:nil
329-
completion:completion];
324+
- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
325+
completion:(nullable GIDSignInCompletion)completion {
326+
[self signInWithPresentingWindow:presentingWindow
327+
hint:nil
328+
completion:completion];
330329
}
331330

332-
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
333-
presentingWindow:(NSWindow *)presentingWindow
334-
hint:(nullable NSString *)hint
335-
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
336-
completion:(nullable GIDSignInCompletion)completion {
331+
- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
332+
hint:(nullable NSString *)hint
333+
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
334+
completion:(nullable GIDSignInCompletion)completion {
337335
GIDSignInInternalOptions *options =
338-
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
336+
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
339337
presentingWindow:presentingWindow
340338
loginHint:hint
341339
addScopesFlow:NO
@@ -477,6 +475,14 @@ + (GIDSignIn *)sharedInstance {
477475
- (id)initPrivate {
478476
self = [super init];
479477
if (self) {
478+
// Get the bundle of the current executable.
479+
NSBundle *bundle = NSBundle.mainBundle;
480+
481+
// If we have a bundle, try to set the active configuration from the bundle's Info.plist.
482+
if (bundle) {
483+
_configuration = [GIDSignIn configurationFromBundle:bundle];
484+
}
485+
480486
// Check to see if the 3P app is being run for the first time after a fresh install.
481487
BOOL isFreshInstall = [self isFreshInstall];
482488

@@ -514,6 +520,14 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
514520
}
515521

516522
if (options.interactive) {
523+
// Ensure that a configuration has been provided.
524+
if (!_configuration) {
525+
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
526+
[NSException raise:NSInvalidArgumentException
527+
format:@"No active configuration. Make sure GIDClientID is set in Info.plist."];
528+
return;
529+
}
530+
517531
// Explicitly throw exception for missing client ID here. This must come before
518532
// scheme check because schemes rely on reverse client IDs.
519533
[self assertValidParameters];
@@ -962,10 +976,11 @@ - (void)assertValidParameters {
962976
// Assert that the presenting view controller has been set.
963977
- (void)assertValidPresentingViewController {
964978
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
965-
if (!_currentOptions.presentingViewController) {
979+
if (!_currentOptions.presentingViewController)
966980
#elif TARGET_OS_OSX
967-
if (!_currentOptions.presentingWindow) {
981+
if (!_currentOptions.presentingWindow)
968982
#endif // TARGET_OS_OSX
983+
{
969984
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
970985
[NSException raise:NSInvalidArgumentException
971986
format:@"|presentingViewController| must be set."];
@@ -1027,6 +1042,38 @@ - (void)setCurrentUserWithKVO:(GIDGoogleUser *_Nullable)user {
10271042
[self didChangeValueForKey:NSStringFromSelector(@selector(currentUser))];
10281043
}
10291044

1045+
// Try to retrieve a configuration value from an |NSBundle|'s Info.plist for a given key.
1046+
+ (nullable NSString *)configValueFromBundle:(NSBundle *)bundle forKey:(NSString *)key {
1047+
NSString *value;
1048+
id configValue = [bundle objectForInfoDictionaryKey:key];
1049+
if ([configValue isKindOfClass:[NSString class]]) {
1050+
value = configValue;
1051+
}
1052+
return value;
1053+
}
1054+
1055+
// Try to generate a |GIDConfiguration| from an |NSBundle|'s Info.plist.
1056+
+ (nullable GIDConfiguration *)configurationFromBundle:(NSBundle *)bundle {
1057+
GIDConfiguration *configuration;
1058+
1059+
// Retrieve any valid config parameters from the bundle's Info.plist.
1060+
NSString *clientID = [GIDSignIn configValueFromBundle:bundle forKey:kConfigClientIDKey];
1061+
NSString *serverClientID = [GIDSignIn configValueFromBundle:bundle
1062+
forKey:kConfigServerClientIDKey];
1063+
NSString *hostedDomain = [GIDSignIn configValueFromBundle:bundle forKey:kConfigHostedDomainKey];
1064+
NSString *openIDRealm = [GIDSignIn configValueFromBundle:bundle forKey:kConfigOpenIDRealmKey];
1065+
1066+
// If we have at least a client ID, try to construct a configuration.
1067+
if (clientID) {
1068+
configuration = [[GIDConfiguration alloc] initWithClientID:clientID
1069+
serverClientID:serverClientID
1070+
hostedDomain:hostedDomain
1071+
openIDRealm:openIDRealm];
1072+
}
1073+
1074+
return configuration;
1075+
}
1076+
10301077
@end
10311078

10321079
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)