Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于iOS的Google OAuth 2.0/JWT身份验证示例_Ios_Google Api_Oauth 2.0_Google Oauth - Fatal编程技术网

用于iOS的Google OAuth 2.0/JWT身份验证示例

用于iOS的Google OAuth 2.0/JWT身份验证示例,ios,google-api,oauth-2.0,google-oauth,Ios,Google Api,Oauth 2.0,Google Oauth,我正在构建一个iOS应用程序,它需要使用下面链接中概述的OAuth 2.0/JWT工作流向Google进行身份验证 我有私钥文件和客户端id。我正在使用库尝试验证。但是,我不知道如何进行 是否可以使用iOS客户端库执行此身份验证工作流?我刚刚完成了与您的问题类似的工作。我需要使用JWT将数据发布到GCP IoT。以下是我使用RSAHA256所做的: 使用库(在Podfilepod“MIHCrypto”中,“~>0.4.1”) 完成这件事运气好吗?不!我最后不得不在服务器端进行身份验证,然后使用

我正在构建一个iOS应用程序,它需要使用下面链接中概述的OAuth 2.0/JWT工作流向Google进行身份验证

我有私钥文件和客户端id。我正在使用库尝试验证。但是,我不知道如何进行


是否可以使用iOS客户端库执行此身份验证工作流?

我刚刚完成了与您的问题类似的工作。我需要使用JWT将数据发布到GCP IoT。以下是我使用RSAHA256所做的:

使用库(在Podfilepod“MIHCrypto”中,“~>0.4.1”


完成这件事运气好吗?不!我最后不得不在服务器端进行身份验证,然后使用应用程序使用的web服务将其公开。您将“myPrivateKey.pem”文件放在哪里?
#import "MIHAESKeyFactory.h"
#import "MIHRSAPrivateKey.h"



  (void) createJWT {
  NSMutableDictionary *headerDict = [NSMutableDictionary new];
  headerDict[@"alg"] = @"RS256";
  headerDict[@"typ"] = @"JWT";

  NSMutableDictionary *payloadDict = [NSMutableDictionary new];
  payloadDict[@"aud"] = @"my project";
  NSDate *now = [NSDate new];
  payloadDict[@"iat"] = [NSNumber numberWithInt:[now timeIntervalSince1970]];
  payloadDict[@"exp"] = [NSNumber numberWithInt:[now timeIntervalSince1970] + 20 * 60];

  NSError *error;
  NSData *headerData = [NSJSONSerialization dataWithJSONObject:headerDict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];


  NSData *payloadData = [NSJSONSerialization dataWithJSONObject:payloadDict
                                                        options:NSJSONWritingPrettyPrinted
                                                          error:&error];


  NSString *base64UrlMessage = [NSString stringWithFormat:@"%@.%@", [self base64UrlforData:headerData ], [self base64UrlforData:payloadData]];

  NSData *privateKeyData = [[self pemKeyStringFromFileWithName:@"myPrivateKey" extension:@"pem"] dataUsingEncoding:NSUTF8StringEncoding];
  MIHRSAPrivateKey *privateKey = [[MIHRSAPrivateKey alloc] initWithData:privateKeyData];

  NSError *signingError = nil;
  NSData *signedData = [privateKey signWithSHA256:[base64UrlMessage dataUsingEncoding:NSUTF8StringEncoding] error:&signingError];

  NSString *signedBase64UrlMessage = [self base64UrlforData: signedData];


  NSString *jwt = [NSString stringWithFormat:@"%@.%@", base64UrlMessage, signedBase64UrlMessage];

  NSLog(@"jwt = %@", jwt);
}


 (NSString*)base64UrlforData:(NSData*)data {
  // base64 to base64URL
  // 1 - remove all '='
  // 2 - replace all '+' by '-'
  // 3 - replace all '/' by '_'
  NSString *base64 = [data base64EncodedStringWithOptions:0];
  NSString *base64_1 = [base64 stringByReplacingOccurrencesOfString:@"=" withString:@""];
  NSString *base64_2 = [base64_1 stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
  NSString *base64_3 = [base64_2 stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
  return base64_3;
}

  (NSString *)pemKeyStringFromFileWithName:(NSString *)name extension: (NSString*) extension {
  NSBundle *bundle = [NSBundle mainBundle];
  NSURL *fileURL = [bundle URLForResource:name withExtension:extension];
  NSError *error = nil;
  NSString *fileContent = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:&error];
  if (error) {
    NSLog(@"%@ error: %@", self.debugDescription, error);
    return nil;
  }
  return fileContent;
}