Преглед изворни кода

Integrate with App Attest

1. Generate keyId
2. Generate attestationObject
3. Generate assertionObject
pinlu пре 3 година
родитељ
комит
e011d0717b

+ 70 - 0
GoogleSignIn/Sources/GIDSignIn.m

@@ -14,6 +14,9 @@
 
 #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
 
+#import <DeviceCheck/DeviceCheck.h>
+#include <CommonCrypto/CommonDigest.h>
+
 #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
 
 #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAuthentication.h"
@@ -245,12 +248,79 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
 - (void)signInWithConfiguration:(GIDConfiguration *)configuration
        presentingViewController:(UIViewController *)presentingViewController
                        callback:(nullable GIDSignInCallback)callback {
+  //Generate keyId and attestation object.
+  if (@available(iOS 14.0, *)) {
+    DCAppAttestService *attestService = DCAppAttestService.sharedService;
+    if (attestService.supported) {
+      // Start generating keyId.
+      NSDate *start = [NSDate date];
+      
+      [attestService generateKeyWithCompletionHandler:^(NSString * _Nullable keyId, NSError * _Nullable error) {
+        if (keyId) {
+          NSDate *keyIdFinish = [NSDate date];
+          NSTimeInterval executionTime = [keyIdFinish timeIntervalSinceDate:start];
+          NSLog(@"keyId generation time = %f seconds", executionTime);
+          NSLog(@"The length of the key Id string = %lu", [keyId length]);
+          
+          // Fake the server side challenge.
+          NSUUID *uuid = [NSUUID UUID];
+          NSString *randomID = [uuid UUIDString];
+          NSData* challenge = [randomID dataUsingEncoding:NSUTF8StringEncoding];
+          NSData* dataHash = [self doSha256:challenge];
+          
+          // Start generating attestation object.
+          NSDate *start = [NSDate date];
+          [attestService attestKey:keyId clientDataHash:dataHash completionHandler:^(NSData * _Nullable attestationObject, NSError * _Nullable error) {
+            if (attestationObject) {
+              NSDate *attestationFinish = [NSDate date];
+              NSTimeInterval executionTime = [attestationFinish timeIntervalSinceDate:start];
+              NSLog(@"The attestation object creation time = %f seconds", executionTime);
+              
+              NSString *attestationString = [attestationObject base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
+              NSLog(@"The number of bytes contained in the attestation object(NSData type) = %lu", [attestationObject length]);
+              NSUInteger length = [attestationString length];
+              NSLog(@"The length of the attestation string = %lu", length);
+              
+              // Start generating assertion object.
+              NSDate *start = [NSDate date];
+              [attestService generateAssertion:keyId clientDataHash:dataHash completionHandler:^(NSData * _Nullable assertionObject, NSError * _Nullable error) {
+                if (assertionObject) {
+                  NSDate *assertionFinish = [NSDate date];
+                  NSTimeInterval executionTime = [assertionFinish timeIntervalSinceDate:start];
+                  NSLog(@"The assertion object creation time = %f seconds", executionTime);
+                  
+                  NSString *assertionString = [assertionObject base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
+                  NSLog(@"The number of bytes contained in the assertion object(NSData type) = %lu", [assertionObject length]);
+                  NSUInteger length = [assertionString length];
+                  NSLog(@"The length of the assertion string = %lu", length);
+                  
+                  [self signInWithConfiguration:configuration
+                       presentingViewController:presentingViewController
+                                           hint:nil
+                                       callback:callback];
+                }
+              }];
+            }
+          }];
+        }
+      }];
+      return;
+    }
+  }
+  
   [self signInWithConfiguration:configuration
        presentingViewController:presentingViewController
                            hint:nil
                        callback:callback];
 }
 
+// Generate SHA256 hash
+- (NSData *)doSha256:(NSData *)dataIn {
+    NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
+    CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes);
+    return macOut;
+}
+
 - (void)addScopes:(NSArray<NSString *> *)scopes
     presentingViewController:(UIViewController *)presentingViewController
                     callback:(nullable GIDSignInCallback)callback {

+ 10 - 4
Samples/Swift/DaysUntilBirthday/DaysUntilBirthday.xcodeproj/project.pbxproj

@@ -486,8 +486,10 @@
 			buildSettings = {
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
-				CODE_SIGN_STYLE = Automatic;
+				CODE_SIGN_IDENTITY = "iPhone Developer";
+				CODE_SIGN_STYLE = Manual;
 				DEVELOPMENT_ASSET_PATHS = "\"iOS/Preview Content\"";
+				DEVELOPMENT_TEAM = EQHXZ8M8AV;
 				ENABLE_PREVIEWS = YES;
 				INFOPLIST_FILE = iOS/Info.plist;
 				IPHONEOS_DEPLOYMENT_TARGET = 14.0;
@@ -495,8 +497,9 @@
 					"$(inherited)",
 					"@executable_path/Frameworks",
 				);
-				PRODUCT_BUNDLE_IDENTIFIER = com.google.DaysUntilBirthday;
+				PRODUCT_BUNDLE_IDENTIFIER = com.google.experimental0.dev;
 				PRODUCT_NAME = "$(TARGET_NAME)";
+				PROVISIONING_PROFILE_SPECIFIER = "Experimental App 0 Dev";
 				SWIFT_VERSION = 5.0;
 				TARGETED_DEVICE_FAMILY = "1,2";
 			};
@@ -507,8 +510,10 @@
 			buildSettings = {
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
-				CODE_SIGN_STYLE = Automatic;
+				CODE_SIGN_IDENTITY = "iPhone Developer";
+				CODE_SIGN_STYLE = Manual;
 				DEVELOPMENT_ASSET_PATHS = "\"iOS/Preview Content\"";
+				DEVELOPMENT_TEAM = EQHXZ8M8AV;
 				ENABLE_PREVIEWS = YES;
 				INFOPLIST_FILE = iOS/Info.plist;
 				IPHONEOS_DEPLOYMENT_TARGET = 14.0;
@@ -516,8 +521,9 @@
 					"$(inherited)",
 					"@executable_path/Frameworks",
 				);
-				PRODUCT_BUNDLE_IDENTIFIER = com.google.DaysUntilBirthday;
+				PRODUCT_BUNDLE_IDENTIFIER = com.google.experimental0.dev;
 				PRODUCT_NAME = "$(TARGET_NAME)";
+				PROVISIONING_PROFILE_SPECIFIER = "Experimental App 0 Dev";
 				SWIFT_VERSION = 5.0;
 				TARGETED_DEVICE_FAMILY = "1,2";
 			};