Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 85 additions & 38 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
// Minimum time to expiration for a restored access token.
static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;

// Info.plist config keys
static NSString *const kConfigClientIDKey = @"GIDClientID";
static NSString *const kConfigServerClientIDKey = @"GIDServerClientID";
static NSString *const kConfigHostedDomainKey = @"GIDHostedDomain";
static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";

// The callback queue used for authentication flow.
@interface GIDAuthFlow : GIDCallbackQueue

Expand Down Expand Up @@ -214,26 +220,24 @@ - (BOOL)restorePreviousSignInNoRefresh {

#if TARGET_OS_IOS || TARGET_OS_MACCATALYST

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
completion:(nullable GIDSignInCompletion)completion {
- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
presentingViewController:presentingViewController
loginHint:hint
addScopesFlow:NO
completion:completion];
[self signInWithOptions:options];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
completion:(nullable GIDSignInCompletion)completion {
- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
presentingViewController:presentingViewController
loginHint:hint
addScopesFlow:NO
Expand All @@ -242,13 +246,11 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration
[self signInWithOptions:options];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
completion:(nullable GIDSignInCompletion)completion {
[self signInWithConfiguration:configuration
presentingViewController:presentingViewController
hint:nil
completion:completion];
- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
completion:(nullable GIDSignInCompletion)completion {
[self signInWithPresentingViewController:presentingViewController
hint:nil
completion:completion];
}

- (void)addScopes:(NSArray<NSString *> *)scopes
Expand Down Expand Up @@ -307,35 +309,31 @@ - (void)addScopes:(NSArray<NSString *> *)scopes

#elif TARGET_OS_OSX

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
completion:(nullable GIDSignInCompletion)completion {
- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
presentingWindow:presentingWindow
loginHint:hint
addScopesFlow:NO
completion:completion];
[self signInWithOptions:options];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
completion:(nullable GIDSignInCompletion)completion {
[self signInWithConfiguration:configuration
presentingWindow:presentingWindow
hint:nil
completion:completion];
- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
completion:(nullable GIDSignInCompletion)completion {
[self signInWithPresentingWindow:presentingWindow
hint:nil
completion:completion];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
completion:(nullable GIDSignInCompletion)completion {
- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
presentingWindow:presentingWindow
loginHint:hint
addScopesFlow:NO
Expand Down Expand Up @@ -477,6 +475,14 @@ + (GIDSignIn *)sharedInstance {
- (id)initPrivate {
self = [super init];
if (self) {
// Get the bundle of the current executable.
NSBundle *bundle = NSBundle.mainBundle;

// If we have a bundle, try to set the active configuration from the bundle's Info.plist.
if (bundle) {
_configuration = [GIDSignIn configurationFromBundle:bundle];
}

// Check to see if the 3P app is being run for the first time after a fresh install.
BOOL isFreshInstall = [self isFreshInstall];

Expand Down Expand Up @@ -514,6 +520,14 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
}

if (options.interactive) {
// Ensure that a configuration has been provided.
if (!_configuration) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"No active configuration. Make sure GIDClientID is set in Info.plist."];
return;
}

// Explicitly throw exception for missing client ID here. This must come before
// scheme check because schemes rely on reverse client IDs.
[self assertValidParameters];
Expand Down Expand Up @@ -962,10 +976,11 @@ - (void)assertValidParameters {
// Assert that the presenting view controller has been set.
- (void)assertValidPresentingViewController {
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
if (!_currentOptions.presentingViewController) {
if (!_currentOptions.presentingViewController)
#elif TARGET_OS_OSX
if (!_currentOptions.presentingWindow) {
if (!_currentOptions.presentingWindow)
#endif // TARGET_OS_OSX
{
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"|presentingViewController| must be set."];
Expand Down Expand Up @@ -1027,6 +1042,38 @@ - (void)setCurrentUserWithKVO:(GIDGoogleUser *_Nullable)user {
[self didChangeValueForKey:NSStringFromSelector(@selector(currentUser))];
}

// Try to retrieve a configuration value from an |NSBundle|'s Info.plist for a given key.
+ (nullable NSString *)configValueFromBundle:(NSBundle *)bundle forKey:(NSString *)key {
NSString *value;
id configValue = [bundle objectForInfoDictionaryKey:key];
if ([configValue isKindOfClass:[NSString class]]) {
value = configValue;
}
return value;
}

// Try to generate a |GIDConfiguration| from an |NSBundle|'s Info.plist.
+ (nullable GIDConfiguration *)configurationFromBundle:(NSBundle *)bundle {
GIDConfiguration *configuration;

// Retrieve any valid config parameters from the bundle's Info.plist.
NSString *clientID = [GIDSignIn configValueFromBundle:bundle forKey:kConfigClientIDKey];
NSString *serverClientID = [GIDSignIn configValueFromBundle:bundle
forKey:kConfigServerClientIDKey];
NSString *hostedDomain = [GIDSignIn configValueFromBundle:bundle forKey:kConfigHostedDomainKey];
NSString *openIDRealm = [GIDSignIn configValueFromBundle:bundle forKey:kConfigOpenIDRealmKey];

// If we have at least a client ID, try to construct a configuration.
if (clientID) {
configuration = [[GIDConfiguration alloc] initWithClientID:clientID
serverClientID:serverClientID
hostedDomain:hostedDomain
openIDRealm:openIDRealm];
}

return configuration;
}

@end

NS_ASSUME_NONNULL_END
Loading