Explorar o código

Move the method `addScopes` into GIDGoogleUser API

pinlu %!s(int64=3) %!d(string=hai) anos
pai
achega
d8e707dc06

+ 25 - 0
GoogleSignIn/Sources/GIDGoogleUser.m

@@ -17,6 +17,7 @@
 #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
 
 #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
+#import "GoogleSignIn/Sources/GIDSignIn_Private.h"
 
 #import "GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.h"
 #import "GoogleSignIn/Sources/GIDAuthentication.h"
@@ -181,6 +182,30 @@ static NSTimeInterval const kMinimalTimeToExpire = 60.0;
   return ((GTMAppAuthFetcherAuthorization *)self.fetcherAuthorizer).authState;
 }
 
+#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
+
+- (void)addScopes:(NSArray<NSString *> *)scopes
+    presentingViewController:(UIViewController *)presentingViewController
+                  completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
+                                                NSError *_Nullable error))completion {
+  [[GIDSignIn sharedInstance] addScopes:scopes
+               presentingViewController:presentingViewController
+                             completion:completion];
+}
+
+#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
+
+- (void)addScopes:(NSArray<NSString *> *)scopes
+ presentingWindow:(NSWindow *)presentingWindow
+       completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
+                                     NSError *_Nullable error))completion {
+  [[GIDSignIn sharedInstance] addScopes:scopes
+               presentingViewController:presentingWindow
+                             completion:completion];
+}
+
+#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
+
 #pragma mark - Private Methods
 
 #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST

+ 45 - 0
GoogleSignIn/Sources/GIDSignIn_Private.h

@@ -14,8 +14,16 @@
  * 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;
@@ -44,6 +52,43 @@ typedef void (^GIDUserAuthCompletion)(GIDUserAuth *_Nullable userAuth, NSError *
 // @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 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 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
 
 NS_ASSUME_NONNULL_END

+ 45 - 0
GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h

@@ -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.
@@ -25,6 +32,7 @@
 #endif
 
 @class GIDConfiguration;
+@class GIDUserAuth;
 @class GIDToken;
 @class GIDProfileData;
 
@@ -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 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 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
 
 NS_ASSUME_NONNULL_END

+ 0 - 31
GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h

@@ -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.
 ///
@@ -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

+ 6 - 5
Samples/ObjC/SignInSample/Source/SignInViewController.m

@@ -248,7 +248,7 @@ static NSString *const kCredentialsButtonAccessibilityIdentifier = @"Credentials
 
 - (IBAction)signIn:(id)sender {
   [GIDSignIn.sharedInstance signInWithPresentingViewController:self
-                                                    completion:^(GIDGoogleUser *user,
+                                                    completion:^(GIDUserAuth *userAuth,
                                                                  NSError *error) {
     if (error) {
       self->_signInAuthStatus.text =
@@ -280,10 +280,11 @@ static NSString *const kCredentialsButtonAccessibilityIdentifier = @"Credentials
 }
 
 - (IBAction)addScopes:(id)sender {
-  [GIDSignIn.sharedInstance addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ]
-             presentingViewController:self
-                           completion:^(GIDUserAuth *_Nullable userAuth,
-                                        NSError *_Nullable error) {
+  GIDGoogleUser *currentUser = GIDSignIn.sharedInstance.currentUser;
+  [currentUser addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ]
+      presentingViewController:self
+                    completion:^(GIDUserAuth *_Nullable userAuth,
+                                 NSError *_Nullable error) {
     if (error) {
       self->_signInAuthStatus.text = [NSString stringWithFormat:@"Status: Failed to add scopes: %@",
                                       error];

+ 3 - 2
Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift

@@ -88,8 +88,9 @@ final class GoogleSignInAuthenticator: ObservableObject {
       fatalError("No root view controller!")
     }
 
-    GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope],
-                                       presenting: rootViewController) { userAuth, error in
+    GIDSignIn.sharedInstance.currentUser?.addScopes(
+      [BirthdayLoader.birthdayReadScope], presenting: rootViewController)
+    { userAuth, error in
       if let error = error {
         print("Found error while adding birthday read scope: \(error).")
         return