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
31 changes: 31 additions & 0 deletions GoogleSignIn/Sources/GIDGoogleUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"

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

#import "GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.h"
#import "GoogleSignIn/Sources/GIDAuthentication.h"
#import "GoogleSignIn/Sources/GIDEMMSupport.h"
#import "GoogleSignIn/Sources/GIDProfileData_Private.h"
#import "GoogleSignIn/Sources/GIDSignIn_Private.h"
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
#import "GoogleSignIn/Sources/GIDToken_Private.h"

Expand Down Expand Up @@ -181,6 +183,35 @@ - (OIDAuthState *) authState{
return ((GTMAppAuthFetcherAuthorization *)self.fetcherAuthorizer).authState;
}

- (void)addScopes:(NSArray<NSString *> *)scopes
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
presentingViewController:(UIViewController *)presentingViewController
#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
presentingWindow:(NSWindow *)presentingWindow
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion {
if (self != GIDSignIn.sharedInstance.currentUser) {
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeMismatchWithCurrentUser
userInfo:nil];
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
return;
}

[GIDSignIn.sharedInstance addScopes:scopes
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
presentingViewController:presentingViewController
#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
presentingWindow:presentingWindow
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
completion:completion];
}

#pragma mark - Private Methods

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
Expand Down
28 changes: 0 additions & 28 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,6 @@ - (void)signInWithPresentingViewController:(UIViewController *)presentingViewCon
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:(UIViewController *)presentingViewController
completion:(nullable GIDUserAuthCompletion)completion {
// A currentUser must be available in order to complete this flow.
if (!self.currentUser) {
// No currentUser is set, notify callback of failure.
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeNoCurrentUser
userInfo:nil];
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
return;
}

GIDConfiguration *configuration = self.currentUser.configuration;
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
Expand Down Expand Up @@ -350,20 +336,6 @@ - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:(NSWindow *)presentingWindow
completion:(nullable GIDUserAuthCompletion)completion {
// A currentUser must be available in order to complete this flow.
if (!self.currentUser) {
// No currentUser is set, notify callback of failure.
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeNoCurrentUser
userInfo:nil];
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
return;
}

GIDConfiguration *configuration = self.currentUser.configuration;
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
Expand Down
67 changes: 57 additions & 10 deletions GoogleSignIn/Sources/GIDSignIn_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,83 @@
* limitations under the License.
*/

#import <TargetConditionals.h>

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

#if __has_include(<UIKit/UIKit.h>)
#import <UIKit/UIKit.h>
#elif __has_include(<AppKit/AppKit.h>)
#import <AppKit/AppKit.h>
#endif

NS_ASSUME_NONNULL_BEGIN

@class GIDGoogleUser;
@class GIDSignInInternalOptions;

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

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

// Redeclare |currentUser| as readwrite for internal use.
/// Redeclare |currentUser| as readwrite for internal use.
@property(nonatomic, readwrite, nullable) GIDGoogleUser *currentUser;

// Private initializer for |GIDSignIn|.
/// Private initializer for |GIDSignIn|.
- (instancetype)initPrivate;

// Authenticates with extra options.
/// Authenticates with extra options.
- (void)signInWithOptions:(GIDSignInInternalOptions *)options;

// Restores a previously authenticated user from the keychain synchronously without refreshing
// the access token or making a userinfo request. The currentUser.profile will be nil unless
// the profile data can be extracted from the ID token.
//
// @return NO if there is no user restored from the keychain.
/// Restores a previously authenticated user from the keychain synchronously without refreshing
/// the access token or making a userinfo request.
///
/// The currentUser.profile will be nil unless the profile data can be extracted from the ID token.
///
/// @return NO if there is no user restored from the keychain.
- (BOOL)restorePreviousSignInNoRefresh;

#if TARGET_OS_IOS || TARGET_OS_MACCATALYST

/// Starts an interactive consent flow on iOS to add scopes to the current user's grants.
///
/// The completion will be called at the end of this process. If successful, a `GIDUserAuth`
/// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
///
/// @param scopes The scopes to ask the user to consent to.
/// @param presentingViewController The view controller used to present `SFSafariViewContoller` on
/// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on
/// iOS 13+.
/// @param completion The block that is called on completion. This block will be called asynchronously
/// on the main queue.
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:(UIViewController *)presentingViewController
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion
NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions.");

#elif TARGET_OS_OSX

/// Starts an interactive consent flow on macOS to add scopes to the current user's grants.
///
/// The completion will be called at the end of this process. If successful, a `GIDUserAuth`
/// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
///
/// @param scopes An array of scopes to ask the user to consent to.
/// @param presentingWindow The window used to supply `presentationContextProvider` for
/// `ASWebAuthenticationSession`.
/// @param completion The block that is called on completion. This block will be called asynchronously
/// on the main queue.
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:(NSWindow *)presentingWindow
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion;

#endif

@end

NS_ASSUME_NONNULL_END
45 changes: 45 additions & 0 deletions GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
*/

#import <Foundation/Foundation.h>
#import <TargetConditionals.h>

#if __has_include(<UIKit/UIKit.h>)
#import <UIKit/UIKit.h>
#elif __has_include(<AppKit/AppKit.h>)
#import <AppKit/AppKit.h>
#endif

// We have to import GTMAppAuth because forward declaring the protocol does
// not generate the `fetcherAuthorizer` property below for Swift.
Expand All @@ -25,6 +32,7 @@
#endif

@class GIDConfiguration;
@class GIDUserAuth;
@class GIDToken;
@class GIDProfileData;

Expand Down Expand Up @@ -71,6 +79,43 @@ NS_ASSUME_NONNULL_BEGIN
- (void)doWithFreshTokens:(void (^)(GIDGoogleUser *_Nullable user,
NSError *_Nullable error))completion;

#if TARGET_OS_IOS || TARGET_OS_MACCATALYST

/// Starts an interactive consent flow on iOS to add scopes to the user's grants.
///
/// The completion will be called at the end of this process. If successful, a `GIDUserAuth`
/// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
///
/// @param scopes The scopes to ask the user to consent to.
/// @param presentingViewController The view controller used to present `SFSafariViewContoller` on
/// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on
/// iOS 13+.
/// @param completion The block that is called on completion. This block will be called asynchronously
/// on the main queue.
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:(UIViewController *)presentingViewController
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion
NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions.");

#elif TARGET_OS_OSX

/// Starts an interactive consent flow on macOS to add scopes to the user's grants.
///
/// The completion will be called at the end of this process. If successful, a `GIDUserAuth`
/// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
///
/// @param scopes An array of scopes to ask the user to consent to.
/// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`.
/// @param completion The block that is called on completion. This block will be called asynchronously
/// on the main queue.
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:(NSWindow *)presentingWindow
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion;

#endif

@end

NS_ASSUME_NONNULL_END
35 changes: 2 additions & 33 deletions GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ typedef NS_ERROR_ENUM(kGIDSignInErrorDomain, GIDSignInErrorCode) {
kGIDSignInErrorCodeCanceled = -5,
/// Indicates an Enterprise Mobility Management related error has occurred.
kGIDSignInErrorCodeEMM = -6,
/// Indicates there is no `currentUser`.
kGIDSignInErrorCodeNoCurrentUser = -7,
/// Indicates the requested scopes have already been granted to the `currentUser`.
kGIDSignInErrorCodeScopesAlreadyGranted = -8,
/// Indicates there is an operation on a previous user.
kGIDSignInErrorCodeMismatchWithCurrentUser = -9,
};

/// Represents a completion block that takes an error if the operation was unsuccessful.
Expand Down Expand Up @@ -165,23 +165,6 @@ typedef void (^GIDDisconnectCompletion)(NSError *_Nullable error);
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion;

/// Starts an interactive consent flow on iOS to add scopes to the current user's grants.
///
/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser`
/// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
///
/// @param scopes The scopes to ask the user to consent to.
/// @param presentingViewController The view controller used to present `SFSafariViewContoller` on
/// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on
/// iOS 13+.
/// @param completion The block that is called on completion. This block will be called asynchronously
/// on the main queue.
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:(UIViewController *)presentingViewController
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion
NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions.");

#elif TARGET_OS_OSX
/// Starts an interactive sign-in flow on macOS.
///
Expand Down Expand Up @@ -233,20 +216,6 @@ typedef void (^GIDDisconnectCompletion)(NSError *_Nullable error);
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion;

/// Starts an interactive consent flow on macOS to add scopes to the current user's grants.
///
/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser`
/// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
///
/// @param scopes An array of scopes to ask the user to consent to.
/// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`.
/// @param completion The block that is called on completion. This block will be called asynchronously
/// on the main queue.
- (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:(NSWindow *)presentingWindow
completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
NSError *_Nullable error))completion;

#endif

@end
Expand Down
Loading