GIDGoogleUser.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2022 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import <TargetConditionals.h>
  18. #if __has_include(<UIKit/UIKit.h>)
  19. #import <UIKit/UIKit.h>
  20. #elif __has_include(<AppKit/AppKit.h>)
  21. #import <AppKit/AppKit.h>
  22. #endif
  23. #ifdef SWIFT_PACKAGE
  24. @import GTMSessionFetcherCore;
  25. #else
  26. #import <GTMSessionFetcher/GTMSessionFetcher.h>
  27. #endif
  28. @class GIDConfiguration;
  29. @class GIDSignInResult;
  30. @class GIDToken;
  31. @class GIDProfileData;
  32. NS_ASSUME_NONNULL_BEGIN
  33. /// This class represents a signed-in user.
  34. @interface GIDGoogleUser : NSObject <NSSecureCoding>
  35. /// The Google user ID.
  36. @property(nonatomic, readonly, nullable) NSString *userID;
  37. /// The basic profile data for the user.
  38. @property(nonatomic, readonly, nullable) GIDProfileData *profile;
  39. /// The OAuth2 scopes granted to the app in an array of `NSString`.
  40. @property(nonatomic, readonly, nullable) NSArray<NSString *> *grantedScopes;
  41. /// The configuration that was used to sign in this user.
  42. @property(nonatomic, readonly) GIDConfiguration *configuration;
  43. /// The OAuth2 access token to access Google services.
  44. @property(nonatomic, readonly) GIDToken *accessToken;
  45. /// The OAuth2 refresh token to exchange for new access tokens.
  46. @property(nonatomic, readonly) GIDToken *refreshToken;
  47. /// The OpenID Connect ID token that identifies the user.
  48. ///
  49. /// Send this token to your server to authenticate the user there. For more information on this topic,
  50. /// see https://developers.google.com/identity/sign-in/ios/backend-auth.
  51. @property(nonatomic, readonly, nullable) GIDToken *idToken;
  52. #pragma clang diagnostic push
  53. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  54. /// The authorizer for use with `GTLRService`, `GTMSessionFetcher`, or `GTMHTTPFetcher`.
  55. @property(nonatomic, readonly) id<GTMFetcherAuthorizationProtocol> fetcherAuthorizer;
  56. #pragma clang diagnostic pop
  57. /// Refresh the user's access and ID tokens if they have expired or are about to expire.
  58. ///
  59. /// @param completion A completion block that takes a `GIDGoogleUser` or an error if the attempt to
  60. /// refresh tokens was unsuccessful. The block will be called asynchronously on the main queue.
  61. - (void)refreshTokensIfNeededWithCompletion:(void (^)(GIDGoogleUser *_Nullable user,
  62. NSError *_Nullable error))completion;
  63. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  64. /// Starts an interactive consent flow on iOS to add new scopes to the user's `grantedScopes`.
  65. ///
  66. /// The completion will be called at the end of this process. If successful, a `GIDSignInResult`
  67. /// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
  68. ///
  69. /// @param scopes The scopes to ask the user to consent to.
  70. /// @param presentingViewController The view controller used to present `SFSafariViewController` on
  71. /// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on
  72. /// iOS 13+.
  73. /// @param completion The optional block that is called on completion. This block will be called
  74. /// asynchronously on the main queue.
  75. - (void)addScopes:(NSArray<NSString *> *)scopes
  76. presentingViewController:(UIViewController *)presentingViewController
  77. completion:(nullable void (^)(GIDSignInResult *_Nullable signInResult,
  78. NSError *_Nullable error))completion
  79. NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions.");
  80. #elif TARGET_OS_OSX
  81. /// Starts an interactive consent flow on macOS to add new scopes to the user's `grantedScopes`.
  82. ///
  83. /// The completion will be called at the end of this process. If successful, a `GIDSignInResult`
  84. /// instance will be returned reflecting the new scopes and saved sign-in state will be updated.
  85. ///
  86. /// @param scopes An array of scopes to ask the user to consent to.
  87. /// @param presentingWindow The window used to supply `presentationContextProvider` for
  88. /// `ASWebAuthenticationSession`.
  89. /// @param completion The optional block that is called on completion. This block will be called
  90. /// asynchronously on the main queue.
  91. - (void)addScopes:(NSArray<NSString *> *)scopes
  92. presentingWindow:(NSWindow *)presentingWindow
  93. completion:(nullable void (^)(GIDSignInResult *_Nullable signInResult,
  94. NSError *_Nullable error))completion;
  95. #endif
  96. @end
  97. NS_ASSUME_NONNULL_END