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

7
我正在开发一个iOS应用程序,需要使用下面链接中概述的OAuth 2.0 / JWT工作流程来验证Google身份。

Google OAuth 2.0 JWT example

我拥有私钥文件和客户端ID。我正在尝试使用Google API Objective-C Client库进行认证,但是我不知道如何继续下一步。
是否可能使用iOS客户端库执行此身份验证工作流程?

你有没有运气完成这个任务? - Stavash
1
不行!我最终不得不在服务器端进行身份验证,然后使用 Web 服务将其公开,以供应用程序使用。 - sherlock
1个回答

0

我刚刚完成了类似于你的问题的事情。我需要使用JWT将数据发布到GCP IoT。以下是我使用RSAWithSHA256所做的:

使用库MIHCrypto(在Podfile中pod“MIHCrypto”,“〜> 0.4.1”

#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;
}

你把 'myPrivateKey.pem' 文件放在哪里了? - Argus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接