Explorar el Código

Report execution environment via `gidenv` (#113)

* Report execution environment via gidenv.

* Refactor GIDVersion reporting.

* Cleanup.

* Test for the presence of the version and environment params.

* Rename execution environments.

* Add unit tests for GIDSignInPreferences.
Peter Andrews hace 4 años
padre
commit
20b21a1997

+ 11 - 8
GoogleSignIn/Sources/GIDAuthentication.m

@@ -236,16 +236,20 @@ static NSString *const kNewIOSSystemName = @"iOS";
     }
   }
   // This is the first handler in the queue, a fetch is needed.
+  NSMutableDictionary *additionalParameters = [@{} mutableCopy];
 #if TARGET_OS_IOS
-  OIDTokenRequest *tokenRefreshRequest =
-    [_authState tokenRefreshRequestWithAdditionalParameters:
-        [GIDAuthentication updatedEMMParametersWithParameters:
-            _authState.lastTokenResponse.request.additionalParameters]];
+  [additionalParameters addEntriesFromDictionary:
+      [GIDAuthentication updatedEMMParametersWithParameters:
+          _authState.lastTokenResponse.request.additionalParameters]];
 #else // TARGET_OS_OSX or TARGET_OS_MACCATALYST
-  OIDTokenRequest *tokenRefreshRequest =
-    [_authState tokenRefreshRequestWithAdditionalParameters:
-        _authState.lastTokenResponse.request.additionalParameters];
+  [additionalParameters addEntriesFromDictionary:
+      _authState.lastTokenResponse.request.additionalParameters];
 #endif
+  additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
+  additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
+
+  OIDTokenRequest *tokenRefreshRequest =
+      [_authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
   [OIDAuthorizationService performTokenRequest:tokenRefreshRequest
                  originalAuthorizationResponse:_authState.lastAuthorizationResponse
                                       callback:^(OIDTokenResponse *_Nullable tokenResponse,
@@ -314,7 +318,6 @@ static NSString *const kNewIOSSystemName = @"iOS";
   if (isPasscodeInfoRequired) {
     allParameters[kEMMPasscodeInfoParameterName] = [GIDMDMPasscodeState passcodeState].info;
   }
-  allParameters[kSDKVersionLoggingParameter] = GIDVersion();
   return allParameters;
 }
 

+ 9 - 2
GoogleSignIn/Sources/GIDSignIn.m

@@ -438,10 +438,12 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
   NSString *revokeURLString = [NSString stringWithFormat:kRevokeTokenURLTemplate,
       [GIDSignInPreferences googleAuthorizationServer], token];
   // Append logging parameter
-  revokeURLString = [NSString stringWithFormat:@"%@&%@=%@",
+  revokeURLString = [NSString stringWithFormat:@"%@&%@=%@&%@=%@",
                      revokeURLString,
                      kSDKVersionLoggingParameter,
-                     GIDVersion()];
+                     GIDVersion(),
+                     kEnvironmentLoggingParameter,
+                     GIDEnvironment()];
   NSURL *revokeURL = [NSURL URLWithString:revokeURLString];
   [self startFetchURL:revokeURL
               fromAuthState:authState
@@ -582,6 +584,8 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
 #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
   [additionalParameters addEntriesFromDictionary:options.extraParams];
 #endif
+  additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
+  additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
 
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
   OIDAuthorizationRequest *request =
@@ -748,6 +752,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
                                        emmSupport:authFlow.emmSupport
                            isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
 #endif
+  additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
+  additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
+
   OIDTokenRequest *tokenRequest;
   if (!authState.lastTokenResponse.accessToken &&
       authState.lastAuthorizationResponse.authorizationCode) {

+ 3 - 0
GoogleSignIn/Sources/GIDSignInPreferences.h

@@ -19,9 +19,12 @@
 NS_ASSUME_NONNULL_BEGIN
 
 extern NSString *const kSDKVersionLoggingParameter;
+extern NSString *const kEnvironmentLoggingParameter;
 
 NSString* GIDVersion(void);
 
+NSString* GIDEnvironment(void);
+
 @interface GIDSignInPreferences : NSObject
 
 + (NSString *)googleAuthorizationServer;

+ 41 - 0
GoogleSignIn/Sources/GIDSignInPreferences.m

@@ -23,6 +23,17 @@ static NSString *const kUserInfoServer = @"www.googleapis.com";
 // The name of the query parameter used for logging the SDK version.
 NSString *const kSDKVersionLoggingParameter = @"gpsdk";
 
+// The name of the query parameter used for logging the Apple execution environment.
+NSString *const kEnvironmentLoggingParameter = @"gidenv";
+
+// Supported Apple execution environments
+static NSString *const kAppleEnvironmentUnknown = @"unknown";
+static NSString *const kAppleEnvironmentIOS = @"ios";
+static NSString *const kAppleEnvironmentIOSSimulator = @"ios-sim";
+static NSString *const kAppleEnvironmentMacOS = @"macos";
+static NSString *const kAppleEnvironmentMacOSIOSOnMac = @"macos-ios";
+static NSString *const kAppleEnvironmentMacOSMacCatalyst = @"macos-cat";
+
 #ifndef GID_SDK_VERSION
 #error "GID_SDK_VERSION is not defined: add -DGID_SDK_VERSION=x.x.x to the build invocation."
 #endif
@@ -39,6 +50,36 @@ NSString* GIDVersion(void) {
   return [NSString stringWithFormat:@"gid-%@", @STR(GID_SDK_VERSION)];
 }
 
+// Get the current Apple execution environment.
+NSString* GIDEnvironment(void) {
+  NSString *appleEnvironment = kAppleEnvironmentUnknown;
+
+#if TARGET_OS_MACCATALYST
+  appleEnvironment = kAppleEnvironmentMacOSMacCatalyst;
+#elif TARGET_OS_IOS
+#if TARGET_OS_SIMULATOR
+  appleEnvironment = kAppleEnvironmentIOSSimulator;
+#else // TARGET_OS_SIMULATOR
+#if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
+  if (@available(iOS 14.0, *)) {
+    if ([NSProcessInfo.processInfo respondsToSelector:@selector(isiOSAppOnMac)]) {
+      appleEnvironment = NSProcessInfo.processInfo.iOSAppOnMac ? kAppleEnvironmentMacOSIOSOnMac :
+          kAppleEnvironmentIOS;
+    } else {
+      appleEnvironment = kAppleEnvironmentIOS;
+    }
+  }
+#else // defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
+  appleEnvironment = kAppleEnvironmentIOS;
+#endif // defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
+#endif // TARGET_OS_SIMULATOR
+#elif TARGET_OS_OSX
+  appleEnvironment = kAppleEnvironmentMacOS;
+#endif // TARGET_OS_MACCATALYST
+
+  return appleEnvironment;
+}
+
 @implementation GIDSignInPreferences
 
 + (NSString *)googleAuthorizationServer {

+ 4 - 0
GoogleSignIn/Tests/Unit/GIDAuthenticationTest.m

@@ -336,6 +336,7 @@ _Static_assert(kChangeTypeEnd == (sizeof(kObservedProperties) / sizeof(*kObserve
     @"device_os" : [NSString stringWithFormat:@"%@ %@",
         _fakeSystemName, [UIDevice currentDevice].systemVersion],
     kSDKVersionLoggingParameter : GIDVersion(),
+    kEnvironmentLoggingParameter : GIDEnvironment(),
   };
   XCTAssertEqualObjects(auth.authState.lastTokenResponse.request.additionalParameters,
                         expectedParameters);
@@ -355,6 +356,7 @@ _Static_assert(kChangeTypeEnd == (sizeof(kObservedProperties) / sizeof(*kObserve
     @"device_os" : [NSString stringWithFormat:@"%@ %@",
         kNewIOSName, [UIDevice currentDevice].systemVersion],
     kSDKVersionLoggingParameter : GIDVersion(),
+    kEnvironmentLoggingParameter : GIDEnvironment(),
   };
   XCTAssertEqualObjects(auth.authState.lastTokenResponse.request.additionalParameters,
                         expectedParameters);
@@ -376,6 +378,7 @@ _Static_assert(kChangeTypeEnd == (sizeof(kObservedProperties) / sizeof(*kObserve
         _fakeSystemName, [UIDevice currentDevice].systemVersion],
     @"emm_passcode_info" : [GIDMDMPasscodeState passcodeState].info,
     kSDKVersionLoggingParameter : GIDVersion(),
+    kEnvironmentLoggingParameter : GIDEnvironment(),
   };
   XCTAssertEqualObjects(auth.authState.lastTokenResponse.request.additionalParameters,
                         expectedParameters);
@@ -480,6 +483,7 @@ _Static_assert(kChangeTypeEnd == (sizeof(kObservedProperties) / sizeof(*kObserve
         [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion],
     @"emm_passcode_info" : [GIDMDMPasscodeState passcodeState].info,
     kSDKVersionLoggingParameter : GIDVersion(),
+    kEnvironmentLoggingParameter : GIDEnvironment(),
   };
   XCTAssertEqualObjects(auth.authState.lastTokenResponse.request.additionalParameters,
                         expectedParameters);

+ 47 - 0
GoogleSignIn/Tests/Unit/GIDSignInPreferencesTest.m

@@ -0,0 +1,47 @@
+// Copyright 2022 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 <XCTest/XCTest.h>
+
+#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
+
+@interface GIDSignInPreferencesTest : XCTestCase
+@end
+
+@implementation GIDSignInPreferencesTest
+
+- (void)testGIDVersion {
+  NSString *version = GIDVersion();
+  XCTAssertTrue([version hasPrefix:@"gid-"]);
+}
+
+- (void)testGIDEnvironment {
+  NSString *environment = GIDEnvironment();
+
+  NSString *expectedEnvironment;
+#if TARGET_OS_MACCATALYST
+  expectedEnvironment = @"macos-cat";
+#elif TARGET_OS_IOS
+#if TARGET_OS_SIMULATOR
+  expectedEnvironment = @"ios-sim";
+#else
+  expectedEnvironment = @"ios";
+#endif
+#elif TARGET_OS_OSX
+  expectedEnvironment = @"macos";
+#endif
+  XCTAssertEqualObjects(environment, expectedEnvironment);
+}
+
+@end

+ 7 - 0
GoogleSignIn/Tests/Unit/GIDSignInTest.m

@@ -28,6 +28,7 @@
 
 #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
 #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
+#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
 #import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
 
 #if TARGET_OS_IOS
@@ -1122,6 +1123,10 @@ static void *kTestObserverContext = &kTestObserverContext;
   NSDictionary<NSString *, NSObject<NSCopying> *> *params = queryComponent.dictionaryValue;
   XCTAssertEqualObjects([params valueForKey:@"token"], token,
                         @"token parameter should match");
+  XCTAssertEqualObjects([params valueForKey:kSDKVersionLoggingParameter], GIDVersion(),
+                        @"SDK version logging parameter should match");
+  XCTAssertEqualObjects([params valueForKey:kEnvironmentLoggingParameter], GIDEnvironment(),
+                        @"Environment logging parameter should match");
   // Emulate result back from server.
   [self didFetch:nil error:nil];
   if (hasCallback) {
@@ -1254,6 +1259,8 @@ static void *kTestObserverContext = &kTestObserverContext;
     XCTAssertNotNil(_savedAuthorizationRequest);
     NSDictionary<NSString *, NSObject *> *params = _savedAuthorizationRequest.additionalParameters;
     XCTAssertEqualObjects(params[@"include_granted_scopes"], @"true");
+    XCTAssertEqualObjects(params[kSDKVersionLoggingParameter], GIDVersion());
+    XCTAssertEqualObjects(params[kEnvironmentLoggingParameter], GIDEnvironment());
     XCTAssertNotNil(_savedAuthorizationCallback);
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
     XCTAssertEqual(_savedPresentingViewController, _presentingViewController);