将 “通过 Apple 登录” 设置为身份池 IdP - Amazon Cognito
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 “通过 Apple 登录” 设置为身份池 IdP

Amazon Cognito 与 Sign in with Apple 集成,以针对移动应用程序和 Web 应用程序用户提供联合身份验证。本节介绍如何使用 Sign in with Apple(使用苹果账号登录)作为身份提供商 (IdP) 来注册和设置应用程序。

要将 Sign in with Apple 作为身份验证提供商添加到身份池,您必须完成两个过程。首先,在应用程序中集成 Sign in with Apple(使用苹果账号登录),然后在身份池中配置 Sign in with Apple(使用苹果账号登录)。有关设置 “使用 Apple 登录” 的 up-to-date 更多信息,请参阅 Apple 开发者文档中的配置环境以使用 Apple 登录

设置 Sign in with Apple

要将 Sign in with Apple(使用苹果账号登录)配置为 IdP,您必须向 Apple 注册您的应用程序才能接收客户端 ID。

  1. 创建 Apple 开发人员账户

  2. 使用 Apple 凭证登录

  3. 在左侧导航窗格中,选择 Certificates, IDs & Profiles(证书、ID 和配置文件)。

  4. 在左侧导航窗格中,选择Identifiers(标识符)。

  5. Identifiers(标识符)页面上,选择 + 图标。

  6. Register a New Identifier(注册新标识符)页面上,选择 App IDs(应用程序 ID),然后选择 Continue(继续)。

  7. Register an App ID(注册应用程序 ID)页面上,执行以下操作:

    1. Description(描述)下方,键入描述。

    2. Bundle ID(服务包 ID)下,键入标识符。记下此 Bundle ID(捆绑包 ID),因为您需要此值才能将 Apple 配置为身份池中的提供商。

    3. Capabilities(功能)下,选择 Sign In with Apple,然后选择 Edit(编辑)。

    4. 通过 Apple 登录:应用程序 ID 配置页面上,为应用程序选择适当的设置。然后选择 Save(保存)。

    5. 选择 Continue(继续)。

  8. Confirm your App ID(确认您的应用程序 ID)页面上,选择 Register(注册)。

  9. 如果要将 Sign in with Apple 与本机 iOS 应用程序集成,请继续执行步骤 10。步骤 11 适用于您希望与 Sign in with Apple JS 集成的应用程序。

  10. Identifiers(标识符)页面上,选择 App IDs(应用程序 ID)菜单,然后选择 Services IDs(服务 ID)。选择 + 图标。

  11. Register a New Identifier(注册新标识符)页面上,选择 Services IDs(服务 ID),然后选择 Continue(继续)。

  12. Register an Services ID(注册服务 ID)页面上,执行以下操作:

    1. Description(描述)下方,键入描述。

    2. Identifier(标识符)下方,键入标识符。记下服务 ID,因为您需要此值才能将 Apple 配置为身份池中的提供商。

    3. 选择 Sign In with Apple(使用苹果账号登录),然后选择 Configure(配置)。

    4. Web Authentication Configuration(Web 身份验证配置)页面上,选择 Primary App ID(主应用程序 ID)。在 Website URLs(网站 URL)下,选择 + 图标。对于 Domains and Subdomains(域和子域),输入应用程序的域名。在 Return URL(返回 URL)中,输入回调 URL,授权在用户通过 Sign in with Apple(使用苹果账号登录)进行身份验证后重定向用户。

    5. 选择 Next(下一步)。

    6. 选择 Continue(继续),然后选择 Register(注册)。

  13. 在左侧导航窗格中,选择 Keys(密钥)。

  14. Keys(密钥)页面上,选择 + 图标。

  15. Register a New Key(注册新密钥)页面上,执行以下操作:

    1. Key Name(密钥名称)下方,键入密钥名称。

    2. 选择 Sign In with Apple,然后选择 Configure(配置)。

    3. Configure Key(配置密钥)页面上,选择 Primary App ID(主应用程序 ID),然后选择 Save(保存)。

    4. 选择 Continue(继续),然后选择 Register(注册)。

注意

要将 Sign in with Apple 与本机 iOS 应用程序集成,请参阅通过 Sign in with Apple 实施用户身份验证

要在本机 iOS 以外的平台中集成 Sign in with Apple(使用苹果账号登录),请参阅使用 Apple JS 登录

在 Amazon Cognito 联合身份控制台中配置外部提供商

使用以下过程可以配置外部提供商。

添加通过 Apple 登录身份提供者(IdP)
  1. Amazon Cognito 控制台中选择身份池。选择身份池。

  2. 选择用户访问选项卡。

  3. 选择添加身份提供者

  4. 选择通过 Apple 登录

  5. 输入您使用 Apple Developer 创建的 OAuth 项目的服务 ID。有关更多信息,请参阅通过 Apple 登录文档中的使用通过 Apple 登录对用户进行身份验证

  6. 要设置 Amazon Cognito 在向通过该提供者进行身份验证的用户颁发凭证时请求的角色,请配置角色设置

    1. 您可以为该 IdP 中的用户分配您在配置经过身份验证的角色时设置的原定设置角色,也可以使用规则选择角色

      1. 如果您选择使用规则选择角色,请输入用户身份验证中的来源声明、您要用来比较声明的运算符、导致与该角色选择匹配的,以及当角色分配匹配时要分配的角色。选择添加其他,以根据不同的条件创建其他规则。

      2. 选择角色解析。当用户的声明与您的规则不匹配时,您可以拒绝凭证或为经过身份验证的角色颁发凭证。

  7. 要更改 Amazon Cognito 在向通过该提供者进行身份验证的用户颁发凭证时分配的主体标签,请配置访问控制属性

    1. 如果不应用主体标签,请选择非活动

    2. 要基于 subaud 声明应用主体标签,请选择使用原定设置映射

    3. 要为主体标签创建自己的自定义属性模式,请选择使用自定义映射。然后,对于您要在标签中表示的每个声明,输入要从该声明中获取的标签键

  8. 选择保存更改

在 Amazon Cognito 联合身份 CLI 中以 Sign in with Apple 作为提供商的示例

此示例创建一个名为 MyIdentityPool 的身份池,并使用 Sign in with Apple(使用苹果账号登录)作为 IdP。

aws cognito-identity create-identity-pool --identity-pool-name MyIdentityPool --supported-login-providers appleid.apple.com="sameple.apple.clientid"

有关更多信息,请参阅创建身份池

生成 Amazon Cognito 身份 ID

此示例生成(或检索)Amazon Cognito ID。这是一个公有 API,因此您不需要任何凭证即可调用此 API。

aws cognito-identity get-id --identity-pool-id SampleIdentityPoolId --logins appleid.apple.com="SignInWithAppleIdToken"

有关更多信息,请参阅 get-id

获取 Amazon Cognito 身份 ID 的凭证

此示例返回用于提供的身份 ID 和 Sign in with Apple 登录的凭证。这是一个公有 API,因此您不需要任何凭证即可调用此 API。

aws cognito-identity get-credentials-for-identity --identity-id SampleIdentityId --logins appleid.apple.com="SignInWithAppleIdToken"

有关更多信息,请参阅 get-credentials-for-identity

使用 Sign in with Apple:Android

Apple 不提供支持 Sign in with Apple for Android 的开发工具包。您可以改为在 Web 视图中使用 Web 流。

Sign in with Apple 使用会话对象跟踪其状态。Amazon Cognito 使用来自此会话对象的 ID 令牌对用户进行身份验证、生成唯一标识符,并根据需要授予用户对其他Amazon资源的访问权限。

@Override public void onSuccess(Bundle response) { String token = response.getString("id_token"); Map<String, String> logins = new HashMap<String, String>(); logins.put("appleid.apple.com", token); credentialsProvider.setLogins(logins); }

使用 Sign in with Apple:iOS - Objective-C

Apple 为原生 iOS 应用程序中的 Sign in with Apple 提供了开发工具包支持。要在本机 iOS 设备中使用 Sign in with Apple 实施用户身份验证,请按照 Apple 文档中的使用 Sign in with Apple 实施用户身份验证操作。

Amazon Cognito 使用 ID 令牌对用户进行身份验证、生成唯一标识符,并根据需要授予用户对其他 Amazon 资源的访问权限。

(void)finishedWithAuth: (ASAuthorizationAppleIDCredential *)auth error: (NSError *) error { NSString *idToken = [ASAuthorizationAppleIDCredential objectForKey:@"identityToken"]; credentialsProvider.logins = @{ "appleid.apple.com": idToken }; }

所用 Sign in with Apple:iOS - Swift

Apple 为原生 iOS 应用程序中的 Sign in with Apple 提供了开发工具包支持。要在本机 iOS 设备中使用 Sign in with Apple 实施用户身份验证,请按照 Apple 文档中的使用 Sign in with Apple 实施用户身份验证操作。

Amazon Cognito 使用 ID 令牌对用户进行身份验证、生成唯一标识符,并根据需要授予用户对其他 Amazon 资源的访问权限。

有关如何在 iOS 中设置 Sign in with Apple(使用苹果账号登录)的更多信息,请参阅设置 Sign in with Apple(使用苹果账号登录)

func finishedWithAuth(auth: ASAuthorizationAppleIDCredential!, error: NSError!) { if error != nil { print(error.localizedDescription) } else { let idToken = auth.identityToken, credentialsProvider.logins = ["appleid.apple.com": idToken!] } }

使用 “通过 Apple 登录”: JavaScript

Apple 不提供支持 “用苹果登录” 功能的 SDK JavaScript。您可以改为在 Web 视图中使用 Web 流。

Sign in with Apple 使用会话对象跟踪其状态。Amazon Cognito 使用来自此会话对象的 ID 令牌对用户进行身份验证、生成唯一标识符,并根据需要授予用户对其他Amazon资源的访问权限。

function signinCallback(authResult) { // Add the apple's id token to the Amazon Cognito credentials login map. AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'IDENTITY_POOL_ID', Logins: { 'appleid.apple.com': authResult['id_token'] } }); // Obtain AWS credentials AWS.config.credentials.get(function(){ // Access AWS resources here. }); }

使用 Sign in with Apple:Xamarin

我们没有支持 Sign in with Apple for Xamarin 的开发工具包。您可以改为在 Web 视图中使用 Web 流。

Sign in with Apple 使用会话对象跟踪其状态。Amazon Cognito 使用来自此会话对象的 ID 令牌对用户进行身份验证、生成唯一标识符,并根据需要授予用户对其他Amazon资源的访问权限。

拥有令牌后,您可以在 CognitoAWSCredentials 中对其进行设置:

credentials.AddLogin("appleid.apple.com", token);