

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定使用 Apple 登入做為身分集區 IdP
<a name="apple"></a>

Amazon Cognito 身分集區可與 Sign in with Apple 搭配使用，為您的行動應用程式和 Web 應用程式使用者提供聯合身分驗證。本節說明如何使用「使用 Apple 登入」做為身分提供者 (IdP) 來註冊及設定您的應用程式。

若要以身分驗證供應商身分將「使用 Apple 登入」新增至身分集區，您必須完成兩個程序。首先，在應用程式整合「使用 Apple 登入」，然後在身分集區中設定「使用 Apple 登入」。如需設定「使用 Apple 登入」的最新資訊，請參閱《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>

若要將「使用 Apple 登入」設定為 IdP，請向 Apple 註冊您的應用程式，以接收用戶端 ID。

1. [向 Apple 建立開發人員帳戶](https://developer.apple.com/programs/enroll/)。

1. 使用您的 Apple 憑證[登入](https://developer.apple.com/account/#/welcome)。

1. 在左側導覽窗格中，選擇 **Certificates, IDs & Profiles (憑證、ID 和描述檔)**。

1. 在左側導覽窗格中，選擇 **Identifiers (識別碼)**。

1. 在 **Identifiers (識別碼)** 頁面上，選擇 **\$1** 圖示。

1. 在 **Register a New Identifier (註冊新的識別碼)** 頁面上，選擇 **App IDs (應用程式 ID)**，然後選擇 **Continue (繼續)**。

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. 選擇**繼續**。

1. 在 **Confirm your App ID** (確認您的應用程式 ID) 頁面上，選擇 **Register** (註冊)。

1. 如果您想要將 Sign in with Apple 與原生 iOS 應用程式整合，請繼續進行步驟 10。步驟 11 適用於您想要與 Sign in wih Apple JS 整合的應用程式。

1. 在 **Identifiers** (識別碼) 頁面中，選擇 **App IDs** (應用程式 ID) 選單，然後選擇 **Services IDs** (服務 ID)。選擇 **\$1** 圖示。

1. 在 **Register a New Identifier** (註冊新的識別碼) 頁面上，選擇 **Services IDs** (服務 ID)，然後選擇 **Continue** (繼續)。

1. 在 **Register a Services ID** (註冊服務 ID) 頁面上，執行下列操作：

   1. 在 **Description** (說明) 下方，輸入說明內容。

   1. 在 **Identifier** (識別碼) 下方，輸入識別碼。記下服務 ID，因為您需要此數值才能將 Apple 設定為身分集區中的供應商。

   1. 選取 **Sign In with Apple**，然後選擇 **Configure (設定)**。

   1. 在 **Web Authentication Configuration (Web 驗證組態)** 頁面上，選擇 **Primary App ID (主要應用程式 ID)**。在 **Website URL’s** (網站 URL) 下方，選擇 **\$1** 圖示。在 **Domains and Subdomains (網域和子網域)** 中，輸入應用程式的網域名稱。在 **Return URLs (傳回 URL)** 中，輸入一個回呼 URL，當使用者在透過「使用 Apple 登入」驗證身分後，該授權會在此 URL 重新導向使用者。

   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，請參閱 [Sign in with 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 Developer](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` 的身分集區，並以「使用 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"`

如需詳細資訊，請參閱[這裡。](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 不提供支援適用於 Android 之 Sign in with Apple 的開發套件。您可以改在 Web 檢視中使用 Web 流程。
+ 若要在您的應用程式中設定 Sign in with Apple，請遵循 Apple 文件中的 [Configuring Your Webpage for Sign In with Apple](https://developer.apple.com/documentation/signinwithapple/configuring-your-webpage-for-sign-in-with-apple)。
+ 若要將 **Sign in with Apple** 按鈕新增至 Android 使用者介面，請遵循 Apple 文件中的在 [Web 上顯示 Sign in with Apple 按鈕](https://developer.apple.com/documentation/signinwithapple/displaying-sign-in-with-apple-buttons-on-the-web)。
+ 若要使用「使用 Apple 登入」安全地對使用者進行身分驗證，請遵循 Apple 文件中的[使用「使用 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 的 SDK 支援。若要在原生 iOS 裝置中使用 Sign in with Apple 來實作使用者身分驗證，請遵循 Apple 文件中的 [Implementing User Authentication with 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 的 SDK 支援。若要在原生 iOS 裝置中使用 Sign in with Apple 來實作使用者身分驗證，請遵循 Apple 文件中的 [Implementing User Authentication with Sign in with Apple](https://developer.apple.com/documentation/authenticationservices/implementing-user-authentication-with-sign-in-with-apple)。

Amazon Cognito 使用 ID 字符來驗證使用者、產生唯一識別符，並視需要授予使用者對其他 AWS 資源的存取權。

如需如何在 iOS 中設定「使用 Apple 登入」的詳細資訊，請參閱[設定「使用 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!]
    }
}
```

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

Apple 不提供支援適用於 JavaScript 之 Sign in with Apple 的開發套件。您可以改在 Web 檢視中使用 Web 流程。
+ 若要在您的應用程式中設定 Sign in with Apple，請遵循 Apple 文件中的 [Configuring Your Webpage for Sign In with Apple](https://developer.apple.com/documentation/signinwithapple/configuring-your-webpage-for-sign-in-with-apple)。
+ 若要將 **Sign in with Apple** 按鈕新增至 JavaScript 使用者介面，請遵循 Apple 文件中的在 [Web 上顯示 Sign in with Apple 按鈕](https://developer.apple.com/documentation/signinwithapple/displaying-sign-in-with-apple-buttons-on-the-web)。
+ 若要使用「使用 Apple 登入」安全地對使用者進行身分驗證，請遵循 Apple 文件中的[使用「使用 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.
     });
}
```