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
46 changes: 32 additions & 14 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDUserAuth.h"

#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
Expand All @@ -34,6 +35,7 @@
#import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
#import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
#import "GoogleSignIn/Sources/GIDProfileData_Private.h"
#import "GoogleSignIn/Sources/GIDUserAuth_Private.h"

#ifdef SWIFT_PACKAGE
@import AppAuth;
Expand Down Expand Up @@ -187,8 +189,16 @@ - (BOOL)hasPreviousSignIn {
return [authState isAuthorized];
}

- (void)restorePreviousSignInWithCallback:(nullable GIDSignInCallback)callback {
[self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:callback]];
- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user,
NSError *_Nullable error))callback {
[self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:
^(GIDUserAuth *userAuth, NSError *error) {
if (userAuth) {
callback(userAuth.user, nil);
} else {
callback(nil, error);
}
}]];
}

- (BOOL)restorePreviousSignInNoRefresh {
Expand Down Expand Up @@ -217,7 +227,7 @@ - (BOOL)restorePreviousSignInNoRefresh {
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingViewController:presentingViewController
Expand All @@ -231,7 +241,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingViewController:presentingViewController
Expand All @@ -244,7 +254,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
[self signInWithConfiguration:configuration
presentingViewController:presentingViewController
hint:nil
Expand All @@ -253,7 +263,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration

- (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:(UIViewController *)presentingViewController
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
// A currentUser must be available in order to complete this flow.
if (!self.currentUser) {
// No currentUser is set, notify callback of failure.
Expand Down Expand Up @@ -310,7 +320,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingWindow:presentingWindow
Expand All @@ -322,7 +332,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
[self signInWithConfiguration:configuration
presentingWindow:presentingWindow
hint:nil
Expand All @@ -333,7 +343,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingWindow:presentingWindow
Expand All @@ -346,7 +356,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration

- (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:(NSWindow *)presentingWindow
callback:(nullable GIDSignInCallback)callback {
callback:(nullable GIDUserAuthCallback)callback {
// A currentUser must be available in order to complete this flow.
if (!self.currentUser) {
// No currentUser is set, notify callback of failure.
Expand Down Expand Up @@ -541,7 +551,8 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
if (options.callback) {
self->_currentOptions = nil;
dispatch_async(dispatch_get_main_queue(), ^{
options.callback(self->_currentUser, nil);
GIDUserAuth *userAuth = [[GIDUserAuth alloc] initWithGoogleUser:self->_currentUser serverAuthCode:nil];
options.callback(userAuth, nil);
});
}
}
Expand Down Expand Up @@ -601,7 +612,7 @@ - (void)authenticateInteractivelyWithOptions:(GIDSignInInternalOptions *)options
_currentAuthorizationFlow = [OIDAuthorizationService
presentAuthorizationRequest:request
presentingViewController:options.presentingViewController
callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
NSError *_Nullable error) {
[self processAuthorizationResponse:authorizationResponse
error:error
Expand Down Expand Up @@ -882,10 +893,17 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow {
[authFlow addCallback:^() {
GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
if (self->_currentOptions.callback) {
GIDSignInCallback callback = self->_currentOptions.callback;
GIDUserAuthCallback callback = self->_currentOptions.callback;
self->_currentOptions = nil;
dispatch_async(dispatch_get_main_queue(), ^{
callback(self->_currentUser, handlerAuthFlow.error);
if (handlerAuthFlow.error) {
callback(nil, handlerAuthFlow.error);
} else {
OIDAuthState *authState = handlerAuthFlow.authState;
NSString *_Nullable serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
GIDUserAuth *userAuth = [[GIDUserAuth alloc] initWithGoogleUser:self->_currentUser serverAuthCode:serverAuthCode];
callback(userAuth, nil);
}
});
}
}];
Expand Down
21 changes: 13 additions & 8 deletions GoogleSignIn/Sources/GIDSignInInternalOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
#import <AppKit/AppKit.h>
#endif

#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"

@class GIDConfiguration;
@class GIDUserAuth;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -55,7 +54,8 @@ NS_ASSUME_NONNULL_BEGIN
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST

/// The callback block to be called at the completion of the flow.
@property(nonatomic, readonly, nullable) GIDSignInCallback callback;
@property(nonatomic, readonly, nullable) void (^callback)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error);

/// The scopes to be used during the flow.
@property(nonatomic, copy, nullable) NSArray<NSString *> *scopes;
Expand All @@ -69,32 +69,37 @@ NS_ASSUME_NONNULL_BEGIN
presentingViewController:(nullable UIViewController *)presentingViewController
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
callback:(nullable GIDSignInCallback)callback;
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback;

+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingViewController:(nullable UIViewController *)presentingViewController
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
scopes:(nullable NSArray *)scopes
callback:(nullable GIDSignInCallback)callback;
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback;

#elif TARGET_OS_OSX
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingWindow:(nullable NSWindow *)presentingWindow
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
callback:(nullable GIDSignInCallback)callback;
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback;

+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingWindow:(nullable NSWindow *)presentingWindow
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
scopes:(nullable NSArray *)scopes
callback:(nullable GIDSignInCallback)callback;
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback;
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST

/// Creates the options to sign in silently.
+ (instancetype)silentOptionsWithCallback:(GIDSignInCallback)callback;
+ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback;

/// Creates options with the same values as the receiver, except for the "extra parameters", and
/// continuation flag, which are replaced by the arguments passed to this method.
Expand Down
17 changes: 11 additions & 6 deletions GoogleSignIn/Sources/GIDSignInInternalOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
scopes:(nullable NSArray *)scopes
callback:(nullable GIDSignInCallback)callback {
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback {
#elif TARGET_OS_OSX
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingWindow:(nullable NSWindow *)presentingWindow
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
scopes:(nullable NSArray *)scopes
callback:(nullable GIDSignInCallback)callback {
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback {
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST

GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
Expand All @@ -64,13 +66,15 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con
presentingViewController:(nullable UIViewController *)presentingViewController
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
callback:(nullable GIDSignInCallback)callback {
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback {
#elif TARGET_OS_OSX
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingWindow:(nullable NSWindow *)presentingWindow
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
callback:(nullable GIDSignInCallback)callback {
callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback {
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
GIDSignInInternalOptions *options = [self defaultOptionsWithConfiguration:configuration
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
Expand All @@ -85,15 +89,16 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con
return options;
}

+ (instancetype)silentOptionsWithCallback:(GIDSignInCallback)callback {
+ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))callback {
GIDSignInInternalOptions *options = [self defaultOptionsWithConfiguration:nil
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
presentingViewController:nil
#elif TARGET_OS_OSX
presentingWindow:nil
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
loginHint:nil
addScopesFlow:NO
addScopesFlow:NO
callback:callback];
if (options) {
options->_interactive = NO;
Expand Down
4 changes: 4 additions & 0 deletions GoogleSignIn/Sources/GIDSignIn_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ NS_ASSUME_NONNULL_BEGIN
@class GIDGoogleUser;
@class GIDSignInInternalOptions;

// Represents a callback block that takes a `GIDUserAuth` on success or an error if the operation
// was unsuccessful.
typedef void (^GIDUserAuthCallback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error);

// Private |GIDSignIn| methods that are used internally in this SDK and other Google SDKs.
@interface GIDSignIn ()

Expand Down
Loading