فهرست منبع

Merge updates from GIDProfileDataFetcher implementation. Merge branch 'pin-GIDProfileDataFetcher-new' into pin-GIDProfileDataFetcher-fake

pinlu 3 سال پیش
والد
کامیت
f6beda215f

+ 4 - 1
GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h

@@ -1,5 +1,5 @@
 /*
- * Copyright 2022 Google LLC
+ * Copyright 2023 Google LLC
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,9 @@ NS_ASSUME_NONNULL_BEGIN
 
 /// Fetches the latest @GIDProfileData object.
 ///
+/// "This method either extracts profile data from `OIDIDToken` in `OIDAuthState` or fetches it
+/// from the UserInfo endpoint."
+///
 /// @param authState The state of the current OAuth session.
 /// @param completion The block that is called on completion asynchronously.
 - (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState

+ 3 - 3
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h

@@ -1,5 +1,5 @@
 /*
- * Copyright 2022 Google LLC
+ * Copyright 2023 Google LLC
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,13 +22,13 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-@interface GIDProfileDataFetcher : NSObject<GIDProfileDataFetcher>
+@interface GIDProfileDataFetcher : NSObject <GIDProfileDataFetcher>
 
 /// The convenience initializer.
 - (instancetype)init;
 
 /// The initializer for unit test.
-- (instancetype)initWithDataFetcher:(id<GIDHTTPFetcher>)httpFetcher NS_DESIGNATED_INITIALIZER;
+- (instancetype)initWithHTTPFetcher:(id<GIDHTTPFetcher>)httpFetcher NS_DESIGNATED_INITIALIZER;
 
 @end
 

+ 21 - 4
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.m

@@ -1,3 +1,19 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h"
 
 #import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h"
@@ -24,10 +40,10 @@ static NSString *const kUserInfoURLTemplate = @"https://%@/oauth2/v3/userinfo";
 
 - (instancetype)init {
   GIDHTTPFetcher *httpFetcher = [[GIDHTTPFetcher alloc] init];
-  return [self initWithDataFetcher:httpFetcher];
+  return [self initWithHTTPFetcher:httpFetcher];
 }
 
-- (instancetype)initWithDataFetcher:(id<GIDHTTPFetcher>)httpFetcher {
+- (instancetype)initWithHTTPFetcher:(id<GIDHTTPFetcher>)httpFetcher {
   self = [super init];
   if (self) {
     _httpFetcher = httpFetcher;
@@ -40,14 +56,15 @@ static NSString *const kUserInfoURLTemplate = @"https://%@/oauth2/v3/userinfo";
                                                 NSError *_Nullable error))completion {
   OIDIDToken *idToken =
       [[OIDIDToken alloc] initWithIDTokenString:authState.lastTokenResponse.idToken];
-  // If the profile data are present in the ID token, use them.
+  // If profile data is present in the ID token, use it.
   if (idToken) {
     GIDProfileData *profileData = [[GIDProfileData alloc] initWithIDToken:idToken];
     completion(profileData, nil);
     return;
   }
   
-  // If we can't retrieve profile data from the ID token, make a userInfo request to fetch them.
+  // If we can't retrieve profile data from the ID token, make a UserInfo endpoint request to
+  // fetch it.
   NSString *infoString = [NSString stringWithFormat:kUserInfoURLTemplate,
                              [GIDSignInPreferences googleUserInfoServer]];
   NSURL *infoURL = [NSURL URLWithString:infoString];

+ 1 - 3
GoogleSignIn/Sources/GIDProfileData_Private.h

@@ -36,11 +36,9 @@ extern NSString *const kBasicProfileFamilyNameKey;
                    familyName:(nullable NSString *)familyName
                      imageURL:(nullable NSURL *)imageURL NS_DESIGNATED_INITIALIZER;
 
-/// Initialize with id token.
+/// Initialize with ID token.
 - (nullable instancetype)initWithIDToken:(OIDIDToken *)idToken;
 
-- (instancetype)init NS_UNAVAILABLE; 
-
 @end
 
 NS_ASSUME_NONNULL_END

+ 12 - 20
GoogleSignIn/Sources/GIDSignIn.m

@@ -1,4 +1,4 @@
-// Copyright 2021 Google LLC
+// Copyright 2023 Google LLC
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -76,12 +76,6 @@ NS_ASSUME_NONNULL_BEGIN
 // The name of the query parameter used for logging the restart of auth from EMM callback.
 static NSString *const kEMMRestartAuthParameter = @"emmres";
 
-// The URL template for the authorization endpoint.
-static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";
-
-// The URL template for the token endpoint.
-static NSString *const kTokenURLTemplate = @"https://%@/token";
-
 // The URL template for the URL to revoke the token.
 static NSString *const kRevokeTokenURLTemplate = @"https://%@/o/oauth2/revoke";
 
@@ -153,8 +147,7 @@ static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
   // set when a sign-in flow is begun via |signInWithOptions:| when the options passed don't
   // represent a sign in continuation.
   GIDSignInInternalOptions *_currentOptions;
-  // AppAuth configuration object.
-  OIDServiceConfiguration *_appAuthConfiguration;
+  
   // AppAuth external user-agent session state.
   id<OIDExternalUserAgentSession> _currentAuthorizationFlow;
   // Flag to indicate that the auth flow is restarting.
@@ -466,7 +459,7 @@ static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
 
 - (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler
                             httpFetcher:(id<GIDHTTPFetcher>)httpFetcher
-                     profileDataFetcher:(id<GIDProfileDataFetcher>)profileDataFetcher{
+                     profileDataFetcher:(id<GIDProfileDataFetcher>)profileDataFetcher {
   self = [super init];
   if (self) {
     // Get the bundle of the current executable.
@@ -485,17 +478,10 @@ static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
       [_keychainHandler removeAllKeychainEntries];
     }
 
-    NSString *authorizationEnpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
-        [GIDSignInPreferences googleAuthorizationServer]];
-    NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
-        [GIDSignInPreferences googleTokenServer]];
-    _appAuthConfiguration = [[OIDServiceConfiguration alloc]
-        initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEnpointURL]
-                        tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]];
-
 #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
     // Perform migration of auth state from old (before 5.0) versions of the SDK if needed.
-    [GIDAuthStateMigration migrateIfNeededWithTokenURL:_appAuthConfiguration.tokenEndpoint
+    NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
+    [GIDAuthStateMigration migrateIfNeededWithTokenURL:tokenEndpointURL
                                           callbackPath:kBrowserCallbackPath
                                           keychainName:kGTMAppAuthKeychainName
                                         isFreshInstall:isFreshInstall];
@@ -602,9 +588,15 @@ static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
 #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
   additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
   additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
+  
+  NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL];
+  NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
+  OIDServiceConfiguration *appAuthConfiguration =
+      [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL
+                                                       tokenEndpoint:tokenEndpointURL];
 
   OIDAuthorizationRequest *request =
-      [[OIDAuthorizationRequest alloc] initWithConfiguration:_appAuthConfiguration
+      [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration
                                                     clientId:options.configuration.clientID
                                                       scopes:options.scopes
                                                  redirectURL:redirectURL

+ 6 - 0
GoogleSignIn/Sources/GIDSignInPreferences.h

@@ -28,9 +28,15 @@ NSString* GIDEnvironment(void);
 @interface GIDSignInPreferences : NSObject
 
 + (NSString *)googleAuthorizationServer;
+
 + (NSString *)googleTokenServer;
+
 + (NSString *)googleUserInfoServer;
 
++ (NSURL *)authorizationEndpointURL;
+
++ (NSURL *)tokenEndpointURL;
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 18 - 0
GoogleSignIn/Sources/GIDSignInPreferences.m

@@ -34,6 +34,12 @@ static NSString *const kAppleEnvironmentMacOS = @"macos";
 static NSString *const kAppleEnvironmentMacOSIOSOnMac = @"macos-ios";
 static NSString *const kAppleEnvironmentMacOSMacCatalyst = @"macos-cat";
 
+// The URL template for the authorization endpoint.
+static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";
+
+// The URL template for the token endpoint.
+static NSString *const kTokenURLTemplate = @"https://%@/token";
+
 #ifndef GID_SDK_VERSION
 #error "GID_SDK_VERSION is not defined: add -DGID_SDK_VERSION=x.x.x to the build invocation."
 #endif
@@ -94,6 +100,18 @@ NSString* GIDEnvironment(void) {
   return kUserInfoServer;
 }
 
++ (NSURL *)authorizationEndpointURL {
+  NSString *authorizationEnpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
+      [self googleAuthorizationServer]];
+  return [NSURL URLWithString:authorizationEnpointURL];
+}
+
++ (NSURL *)tokenEndpointURL {
+  NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
+      [self googleTokenServer]];
+  return [NSURL URLWithString:tokenEndpointURL];
+}
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 4 - 0
GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h

@@ -42,6 +42,10 @@ NS_ASSUME_NONNULL_BEGIN
 /// @return The URL of the user's profile image.
 - (nullable NSURL *)imageURLWithDimension:(NSUInteger)dimension;
 
+- (instancetype)init NS_UNAVAILABLE;
+
++ (instancetype)new NS_UNAVAILABLE;
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 4 - 4
GoogleSignIn/Tests/Unit/GIDProfileDataFetcherTest.m

@@ -36,7 +36,7 @@ static NSInteger const kErrorCode = 400;
 - (void)setUp {
   [super setUp];
   _httpFetcher = [[GIDFakeHTTPFetcher alloc] init];
-  _profileDataFetcher = [[GIDProfileDataFetcher alloc] initWithDataFetcher:_httpFetcher];
+  _profileDataFetcher = [[GIDProfileDataFetcher alloc] initWithHTTPFetcher:_httpFetcher];
 }
 
 - (void)testFetchProfileData_outOfAuthState_success {
@@ -55,7 +55,7 @@ static NSInteger const kErrorCode = 400;
   [_httpFetcher setTestBlock:testBlock];
   
   XCTestExpectation *expectation =
-      [self expectationWithDescription:@"Callback called with no error"];
+      [self expectationWithDescription:@"completion is invoked"];
   
   [_profileDataFetcher
       fetchProfileDataWithAuthState:authState
@@ -91,7 +91,7 @@ static NSInteger const kErrorCode = 400;
   [_httpFetcher setTestBlock:testBlock];
   
   XCTestExpectation *completionExpectation =
-      [self expectationWithDescription:@"Callback called with error"];
+      [self expectationWithDescription:@"completion is invoked"];
   
   [_profileDataFetcher
       fetchProfileDataWithAuthState:authState
@@ -127,7 +127,7 @@ static NSInteger const kErrorCode = 400;
   [_httpFetcher setTestBlock:testBlock];
   
   XCTestExpectation *completionExpectation =
-      [self expectationWithDescription:@"Callback called with error"];
+      [self expectationWithDescription:@"completion is invoked"];
   
   [_profileDataFetcher
       fetchProfileDataWithAuthState:authState