

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

# 使用 Apple 作为身份池 IdP 来设置登录
<a name="apple"></a>

Amazon Cognito 身份池与“通过 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 登录](https://developer.apple.com/documentation/signinwithapple/configuring-your-environment-for-sign-in-with-apple)。

## 设置 Sign in with Apple
<a name="login-with-apple-setup"></a>

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

1. 创建 [Apple 开发人员账户](https://developer.apple.com/programs/enroll/)。

1. 使用 Apple 凭证[登录](https://developer.apple.com/account/#/welcome)。

1. 在左侧导航窗格中，选择 “**证书 IDs 和配置文件**”。

1. 在左侧导航窗格中，选择**Identifiers**（标识符）。

1. 在 **Identifiers**（标识符）页面上，选择 **\$1** 图标。

1. 在 “**注册新标识符**” 页面上，选择 “**应用程序**” IDs，然后选择 “**继续**”。

1. 在 **Register an App ID**（注册应用程序 ID）页面上，执行以下操作：

   1. 在 **Description**（描述）下方，键入描述。

   1. 在 **Bundle ID**（服务包 ID）下，键入标识符。记下此 **Bundle ID**（捆绑包 ID），因为您需要此值才能将 Apple 配置为身份池中的提供商。

   1. 在 **Capabilities**（功能）下，选择 **Sign In with Apple**，然后选择 **Edit**（编辑）。

   1. 在**通过 Apple 登录：应用程序 ID 配置**页面上，为应用程序选择适当的设置。然后选择 **Save**（保存）。

   1. 选择 **Continue**（继续）。

1. 在 **Confirm your App ID**（确认您的应用程序 ID）页面上，选择 **Register**（注册）。

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

1. 在标**识符**页面上，选择**应用程序 IDs**菜单，然后选择**服务 IDs**。选择 **\$1** 图标。

1. 在 “**注册新标识符**” 页上，选择 “**服务**” IDs，然后选择 “**继续**”。

1. 在 **Register an Services ID**（注册服务 ID）页面上，执行以下操作：

   1. 在 **Description**（描述）下方，键入描述。

   1. 在 **Identifier**（标识符）下方，键入标识符。记下服务 ID，因为您需要此值才能将 Apple 配置为身份池中的提供商。

   1. 选择 **Sign In with Apple**（使用苹果账号登录），然后选择 **Configure**（配置）。

   1. 在 **Web Authentication Configuration**（Web 身份验证配置）页面上，选择 **Primary App ID**（主应用程序 ID）。在 “**网站**” 下 URLs，选择 **\$1** 图标。对于 **Domains and Subdomains**（域和子域），输入应用程序的域名。在 R **eturn 中 URLs，**输入回传 URL，在用户通过 “使用 Apple 登录” 进行身份验证后，授权会将用户重定向到该网址。

   1. 选择**下一步**。

   1. 选择 **Continue**（继续），然后选择 **Register**（注册）。

1. 在左侧导航窗格中，选择 **Keys**（密钥）。

1. 在 **Keys**（密钥）页面上，选择 **\$1** 图标。

1. 在 **Register a New Key**（注册新密钥）页面上，执行以下操作：

   1. 在 **Key Name**（密钥名称）下方，键入密钥名称。

   1. 选择 **Sign In with Apple**，然后选择 **Configure**（配置）。

   1. 在 **Configure Key**（配置密钥）页面上，选择 **Primary App ID**（主应用程序 ID），然后选择 **Save**（保存）。

   1. 选择 **Continue**（继续），然后选择 **Register**（注册）。

**注意**  
要将 Sign in with Apple 与本机 iOS 应用程序集成，请参阅[通过 Sign in with Apple 实施用户身份验证](https://developer.apple.com/documentation/authenticationservices/implementing-user-authentication-with-sign-in-with-apple)。  
要在本机 iOS 以外的平台中集成 Sign in with Apple（使用苹果账号登录），请参阅[使用 Apple JS 登录](https://developer.apple.com/documentation/signinwithapplejs/)。

## 在 Amazon Cognito 联合身份控制台中配置外部提供商
<a name="login-with-apple-configure-provider"></a>

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

**添加通过 Apple 登录身份提供者（IdP）**

1. 从 [Amazon Cognito 控制台](https://console.aws.amazon.com/cognito/home)中选择**身份池**。选择身份池。

1. 选择**用户访问**选项卡。

1. 选择**添加身份提供者**。

1. 选择**通过 Apple 登录**。

1. 输入你使用 [Apple 开发者](https://developer.apple.com)创建的 OAuth 项目的**服务 ID**。有关更多信息，请参阅*通过 Apple 登录文档*中的[使用通过 Apple 登录对用户进行身份验证](https://developer.apple.com/documentation/signinwithapple/authenticating-users-with-sign-in-with-apple)。

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

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

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

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

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

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

   1. 要基于 `sub` 和 `aud` 声明应用主体标签，请选择**使用原定设置映射**。

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

1. 选择**保存更改**。

## 在 Amazon Cognito 联合身份 CLI 中以 Sign in with Apple 作为提供商的示例
<a name="sign-in-with-apple-cli-examples"></a>

此示例创建一个名为 `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"`

 有关更多信息，请参阅[创建身份池](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/create-identity-pool.html) 

**生成 Amazon Cognito 身份 ID**  
 此示例生成（或检索）Amazon Cognito ID。这是一个公有 API，因此您不需要任何凭证即可调用此 API。

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

有关更多信息，请参阅 [get-id](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/get-id.html)。

**获取 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](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/get-credentials-for-identity.html)。

## 使用 Sign in with Apple：Android
<a name="set-up-apple-1.android"></a>

Apple 不提供支持 Sign in with Apple for Android 的开发工具包。您可以改为在 Web 视图中使用 Web 流。
+ 要在应用程序中配置 Sign in with Apple，请按照 Apple 文档中的[配置您的 Web 页面以使用 Sign in with Apple](https://developer.apple.com/documentation/signinwithapple/configuring-your-webpage-for-sign-in-with-apple) 操作。
+ 要将**通过 Apple 登录**按钮添加到 Android 用户界面，请按照 Apple 文档中的[在网页上显示“通过 Apple 登录”按钮](https://developer.apple.com/documentation/signinwithapple/displaying-sign-in-with-apple-buttons-on-the-web)操作。
+ 要使用 Sign in with Apple（使用苹果账号登录）安全地对用户进行身份验证，请按照 Apple 文档中的[使用 Sign in with Apple（使用苹果账号登录）对用户进行身份验证](https://developer.apple.com/documentation/signinwithapple/authenticating-users-with-sign-in-with-apple)操作。

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

```
@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
<a name="set-up-apple-1.ios-objc"></a>

Apple 为原生 iOS 应用程序中的 Sign in with Apple 提供了开发工具包支持。要在本机 iOS 设备中使用 Sign in with Apple 实施用户身份验证，请按照 Apple 文档中的[使用 Sign in with Apple 实施用户身份验证](https://developer.apple.com/documentation/authenticationservices/implementing-user-authentication-with-sign-in-with-apple)操作。

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

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

## 所用 Sign in with Apple：iOS - Swift
<a name="set-up-apple-1.ios-swift"></a>

Apple 为原生 iOS 应用程序中的 Sign in with Apple 提供了开发工具包支持。要在本机 iOS 设备中使用 Sign in with Apple 实施用户身份验证，请按照 Apple 文档中的[使用 Sign in with Apple 实施用户身份验证](https://developer.apple.com/documentation/authenticationservices/implementing-user-authentication-with-sign-in-with-apple)操作。

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

有关如何在 iOS 中设置 Sign in with Apple（使用苹果账号登录）的更多信息，请参阅[设置 Sign in with Apple（使用苹果账号登录）](https://docs.amplify.aws/sdk/auth/federated-identities/q/platform/ios#set-up-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
<a name="set-up-apple-1.javascript"></a>

苹果不提供支持 “用苹果登录” 的 SDK JavaScript。您可以改为在 Web 视图中使用 Web 流。
+ 要在应用程序中配置 Sign in with Apple，请按照 Apple 文档中的[配置您的 Web 页面以使用 Sign in with Apple](https://developer.apple.com/documentation/signinwithapple/configuring-your-webpage-for-sign-in-with-apple) 操作。
+ 要在 JavaScript 用户界面**中添加 “使用 Apple 登**[录” 按钮，请按照 Apple 文档中的在网页上显示 “使用 Apple 登录” 按钮](https://developer.apple.com/documentation/signinwithapple/displaying-sign-in-with-apple-buttons-on-the-web)进行操作。
+ 要使用 Sign in with Apple（使用苹果账号登录）安全地对用户进行身份验证，请按照 Apple 文档中的[使用 Sign in with Apple（使用苹果账号登录）对用户进行身份验证](https://developer.apple.com/documentation/signinwithapple/authenticating-users-with-sign-in-with-apple)操作。

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

```
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.
     });
}
```