从 Google 登录 SDK 获取 AWS 的跨客户端 ID 令牌

Getting cross client id token for AWS from Google Sign-In SDK

我正在尝试使用 Google 登录 SDK 在 iOS 应用程序中将 Google 与 Amazon Cognito 集成,但我似乎无法弄清楚如何获取JWT id 令牌正确。我相信一切都设置正确,因为 Google 登录和 Cognito 都独立工作。

我正在这样设置 GIDSignIn。

[GIDSignIn sharedInstance].scopes = @[kGTLAuthScopePlusLogin, kGTLAuthScopeDrive];
[[GIDSignIn sharedInstance] setClientID:kClientID];
[GIDSignIn sharedInstance] setServerClientID:kServerClientId];

然后按照指定 here 获取 id_token,例外情况是我使用的是 Google 登录而不是 Google+ 登录,这没有 GTMOAuth2Authentication。

- (void)googleSignedIn:(GIDGoogleUser *) user
    {
    NSLog(@"AWSManager: Google signed in, id token = %@", user.authentication.idToken);
    NSString *idToken = user.authentication.idToken;
    self.credentialsProvider.logins = @{ @(AWSCognitoLoginProviderKeyGoogle): idToken};

但 idtoken 不是 json 格式的网络令牌,它只是一大堆字符。 AWS 抛出此错误 --

AWSiOSSDKv2 [Error] AWSIdentityProvider.m line:185 
| __51-[AWSAbstractCognitoIdentityProvider getIdentityId]_block_invoke169 
| GetId failed. 

Error is [Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=9 
"The operation couldn’t be completed. (com.amazonaws.AWSCognitoIdentityErrorDomain error 9.)" 
UserInfo=0x8fa5eb8e4e40{__type=NotAuthorizedException, message=Token is not from a supported provider of this identity pool.}]

我不知道我要做什么。我是 objective-c 的新手,之前在 Android 上做过所有这些。在 android 我做了:

   String mServerClientId = "audience:server:client_id:xxxxxxxxxx.apps.googleusercontent.com"
   String token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, mServerClientId);

检索令牌,但据我所知 iOS 上没有类似的东西。如果需要,我可以提供更多信息。

谢谢!

从错误看来,身份池配置中的 clientId 设置不正确。 Google 每个平台都有不同的客户端 ID,要支持多个客户端 ID,您应该使用 Cognito 对通用 OpenID Connect 身份提供者的支持。请按照以下步骤操作:

  1. 转到 AWS IAM 控制台的身份提供商部分。
  2. 创建一个 OpenId Connect 身份提供者,提供者 URL 作为 https://accounts.google.com,受众作为客户端 ID 之一。
  3. 按照步骤创建身份提供者,稍后您将可以选择添加其他客户端 ID。
  4. 转到 Amazon Cognito 控制台。
  5. 创建或编辑身份池并将 OpenID 连接身份提供商添加到池中。这将允许您信任多个客户端 ID。

您可以按照 Cognito 文档进行 Google 登录 here and OpenID connect providers here

此外,您获得的令牌实际上是 Base64 编码的。它分为三个部分,中间用句点分隔。

  1. 使用的算法。
  2. 负载。
  3. Cognito 验证的签名。

您可以使用 this 很酷的工具来解码令牌。

谢谢,
拉吉特