透過使用者集區使用社交身分提供者 - Amazon Cognito

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

透過使用者集區使用社交身分提供者

您的 Web 和行動應用程式使用者可透過社交身分提供者 (IdP,如 Facebook、Google、Amazon 或 Apple) 進行登入。Amazon Cognito 使用內建的託管 Web UI,可以為所有已進行身分驗證的使用者提供字符處理和管理。這樣,您的後端系統可以對一組使用者集區字符進行標準化。您必須啟用託管 UI,才能與支援的社交身分提供者整合。當 Amazon Cognito 建立您的託管使用者介面時,它會建立 OAuth 2.0 端點,供 Amazon Cognito 和您的 OIDC 和社群 IdPs 用來交換資訊。如需詳細資訊,請參閱 Amazon Cognito 使用者集區 Auth API 參考

您可以在中新增社交 IdP AWS Management Console,也可以使用 AWS CLI 或 Amazon Cognito API。

使用社交登入的身分驗證概觀
注意

Amazon Cognito 使用者集區提供透過第三方 (聯合身分) 登入。這項功能與透過 Amazon Cognito 身分集區 (聯合身分) 登入無關。

必要條件

開始之前,您必須準備好以下事項:

  • 具有應用程式用戶端和使用者集區網域的使用者集區。如需詳細資訊,請參閱建立使用者集區

  • 社交 IdP。

步驟 1:使用社交 IdP 註冊

與 Amazon Cognito 建立社交 IdP 之前,您必須將您的應用程式註冊至社交 IdP 以接收用戶端 ID 和用戶端密碼。

  1. 向 Facebook 建立開發人員帳戶

  2. 使用您的 Facebook 憑證登入

  3. My Apps (我的應用程式) 選單中,選擇 Create New App (建立新的應用程式)。

  4. 輸入您 Facebook 應用程式的名稱,然後選擇 Create App ID (建立應用程式 ID)。

  5. 在左側導覽列中,選擇 Settings (設定) 然後 Basic (基本)。

  6. 記下 App ID (應用程式 ID)App Secret (應用程式秘密)。您會在下一節中用到它們。

  7. 從頁面底部選擇 + Add Platform (+ 新增平台)。

  8. 選擇 Website (網站)。

  9. Website (網站) 下,請將您應用程式登入頁面的路徑輸入 Site URL (網站 URL)。

    https://mydomain.us-east-1.amazoncognito.com/login?response_type=code&client_id=1example23456789&redirect_uri=https://www.example.com
  10. 選擇儲存變更

  11. 將您的使用者集區網域的根路徑輸入 App Domains (應用程式網域)。

    https://mydomain.us-east-1.amazoncognito.com
  12. 選擇儲存變更

  13. 從導覽列選擇 Add Product (新增產品),並為 Facebook Login (Facebook 登入) 產品選擇 Set up (設定)。

  14. 從導覽列選擇 Facebook Login (Facebook 登入),然後選擇 Settings (設定)。

    將您使用者集區網域的 /oauth2/idpresponse 端點路徑輸入至 Valid OAuth Redirect URIs (有效的 OAuth 重新導向 URI)

    https://mydomain.us-east-1.amazoncognito.com/oauth2/idpresponse
  15. 選擇 Save changes (儲存變更)

  1. 向 Amazon 建立開發人員帳戶

  2. 使用您的 Amazon 憑證登入

  3. 您需要建立 Amazon 安全性設定檔以便接收 Amazon 用戶端 ID 和用戶端秘密。

    從頁面頂端的導覽列中選擇 Apps and Services (應用程式和服務),然後選擇 Login with Amazon (登入 Amazon)。

  4. 選擇 Create a Security Profile (建立安全性設定檔)。

  5. 輸入 Security Profile Name (安全設定檔名稱)、Security Profile Description (安全設定檔描述) 和 Consent Privacy Notice URL (同意隱私權聲明 URL)。

  6. 選擇 Save (儲存)

  7. 選擇 Client ID (用戶端 ID) 和 Client Secret (用戶端秘密),以顯示用戶端 ID 和秘密。您會在下一節中用到它們。

  8. 將滑鼠移到齒輪圖示上方並選擇 Web Settings (Web 設定),然後選擇 Edit (編輯)。

  9. Allowed Origins (允許的來源) 中輸入您的使用者集區網域。

    https://mydomain.us-east-1.amazoncognito.com
  10. Allowed Return URLs (允許的傳回 URL) 中輸入具有 /oauth2/idpresponse 端點的使用者集區網域。

    https://mydomain.us-east-1.amazoncognito.com/oauth2/idpresponse
  11. 選擇 Save (儲存)

如需 Google Cloud Platform 中 OAuth 2.0 的詳細資訊,請參閱 Google Workspace for Developers 文件中的瞭解身分驗證和授權

  1. 向 Google 建立開發人員帳戶

  2. 登入 Google Cloud Platform 主控台

  3. 在頂端導覽列中,選擇 Select a project (選取專案)。如果您在 Google 平台中已經有專案,則此功能表將改為顯示您的預設專案。

  4. 選擇 NEW PROJECT (新專案)

  5. 輸入專案的名稱,然後選擇 CREATE (建立)

  6. 在左側導覽列中,選擇 APIs and Services (API 和服務),然後選擇 OAuth consent screen (OAuth 同意畫面)。

  7. 輸入應用程式資訊,App domain (應用程式網域)、Authorized domains (授權網域),以及 Developer contact information (開發人員聯絡資訊)。您的 Authorized domains (授權網域) 必須包含您自訂網域的 amazoncognito.com 與根目錄,例如 example.com。選擇 SAVE AND CONTINUE (儲存並繼續)

  8. 1. 在 Scopes (範圍) 下方,選擇 Add or remove scopes (新增或移除範圍),然後至少選擇下列 OAuth 範圍。

    1. .../auth/userinfo.email

    2. .../auth/userinfo.profile

    3. openid

  9. Test users (測試使用者) 下方,選擇 Add users (新增使用者)。輸入您的電子郵件地址和任何其他授權的測試使用者,然後選擇 SAVE AND CONTINUE (儲存並繼續)。

  10. 再次展開左側導覽列,然後依序選擇 APIs and Services (API 和服務)、Credentials (憑證)。

  11. 依序選擇 CREATE CREDENTIALS (建立憑證)、OAuth client ID (OAuth 用戶端 ID)。

  12. 選擇 Application type (應用程式類型),並為您的用戶端取一個 Name (名稱)。

  13. 在 [授權 JavaScript 來源] 下,選擇 [新增 URI]。輸入您的使用者集區網域。

    https://mydomain.us-east-1.amazoncognito.com
  14. Authorized redirect URIs (授權的重新導向 URI) 下方,選擇 ADD URI (新增 URI)。輸入您使用者集區網域的 /oauth2/idpresponse 端點路徑。

    https://mydomain.us-east-1.amazoncognito.com/oauth2/idpresponse
  15. 選擇 CREATE (建立)。

  16. 安全地儲存 Google 在 Your client ID (您的用戶端 ID) 和 Your client secret (您的用戶端密碼) 下顯示的值。當您新增 Google IdP 時,請將這些值提供給 Amazon Cognito。

如需設定「使用 Apple 登入」的最多 up-to-date 資訊,請參閱 Apple 開發人員說明文件中的「設定使用 Apple 登入的環境」。

  1. 向 Apple 建立開發人員帳戶

  2. 使用您的 Apple 憑證登入

  3. 在左側導覽列上,選擇 Certificates, Identifiers & Profiles (憑證、識別碼與設定檔)

  4. 在左側導覽列上,選擇 Identifiers (識別碼)。

  5. Identifiers (識別碼) 頁面上,選擇 + 圖示。

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

  7. Select a type (選取類型) 頁面上,選擇 App (應用程式),然後選擇 Continue (繼續)

  8. Register an App ID (註冊應用程式 ID) 頁面上,執行下列操作:

    1. Description (描述) 下方輸入描述。

    2. App ID Prefix (應用程式 ID 字首) 下方,輸入 Bundle ID (套件 ID)。記下 App ID Prefix (應用程式 ID 字首) 下的值。在 步驟 2:新增社交 IdP 到您的使用者集區 中選擇 Apple 作為您的身分提供者之後,您將會使用此值。

    3. Capabilities (功能) 下方,選擇 Sign In with Apple,然後選擇 Edit (編輯)。

    4. Sign in with Apple: App ID Configuration (Sign in with Apple:應用程式 ID 組態) 頁面上,選擇將應用程式設定為主要應用程式或與其他應用程式 ID 群組,然後選擇 Save (儲存)。

    5. 選擇繼續

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

  10. Identifiers (識別碼) 頁面上,選擇 + 圖示。

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

  12. Register a Services ID (註冊服務 ID) 頁面上,執行下列操作:

    1. Description (說明) 下方,輸入說明內容。

    2. Identifier (識別碼) 下方,輸入識別碼。記下服務 ID,因為在 步驟 2:新增社交 IdP 到您的使用者集區 中選擇 Apple 當作您的身分提供者後,您會需要此值。

    3. 選擇 Continue (繼續),然後選擇 Register (註冊)

  13. 從「Identifiers (識別碼)」頁面中選擇您剛剛建立的服務 ID。

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

    2. Web Authentication Configuration (Web 身分驗證組態) 頁面上,選取您稍早建立的應用程式 ID 作為 Primary App ID (主要應用程式 ID)。

    3. Website URLs (網站 URL) 旁邊選擇 + 圖示。

    4. Domains and subdomains (網域與子網域) 下方,輸入不含 https:// 字首的使用者集區網域。

      mydomain.us-east-1.amazoncognito.com
    5. Return URLs (傳回 URL) 下,輸入您使用者集區網域的 /oauth2/idpresponse 端點路徑。

      https://mydomain.us-east-1.amazoncognito.com/oauth2/idpresponse
    6. 選擇 Next (下一步),然後選擇 Done (完成)。您不須驗證網域。

    7. 選擇 Continue (繼續),然後選擇 Save (儲存)

  14. 在左側導覽列上,選擇 Keys (金鑰)。

  15. Keys (金鑰) 頁面上,選擇 + 圖示。

  16. Register a New Key (註冊新的金鑰) 頁面上,執行下列操作:

    1. Key Name (金鑰名稱) 下,輸入金鑰名稱。

    2. 選擇 Sign In with Apple,然後選擇 Configure (設定)。

    3. Configure Key (設定金鑰) 頁面上,然後選取您稍早建立的應用程式 ID 作為 Primary App ID (主要應用程式 ID)。選擇儲存

    4. 選擇 Continue (繼續),然後選擇 Register (註冊)。

  17. Download Your Key (下載您的金鑰) 頁面,選擇 Download (下載) 以下載私有金鑰,記下顯示的 Key ID (金鑰 ID),接著選擇 Done (完成)。您在 步驟 2:新增社交 IdP 到您的使用者集區 中選擇 Apple 當作身分提供者後,需有此頁面上顯示的私有金鑰和 Key ID (金鑰 ID) 值。

步驟 2:新增社交 IdP 到您的使用者集區

設定使用者集區社交 IdP AWS Management Console
  1. 前往 Amazon Cognito 主控台。如果出現提示,請輸入您的 AWS 認證。

  2. 選擇 User Pools (使用者集區)。

  3. 從清單中選擇現有的使用者集區,或建立使用者集區

  4. 選擇 Sign-in experience (登入體驗) 索引標籤。找到 Federated sign-in (聯合登入),然後選取 Add an identity provider (新增身分提供者)。

  5. 選擇社交 IdP:FacebookGoogleLogin with Amazon (登入 Amazon) 或 Sign in with Apple (使用 Apple 登入)。

  6. 根據您選擇的社交 IdP,從以下步驟中選擇:

    • GoogleLogin with Amazon (登入 Amazon)– 輸入上一節產生的 app client ID (應用程式用戶端 ID) 和 app client secret (應用程式用戶端密碼)。

    • Facebook – 輸入上一節產生的 app client ID (應用程式用戶端 ID) 和 app client secret (應用程式用戶端秘密),然後選擇 API 版本 (例如版本 2.12)。建議您選擇最新可用的版本,因為每個 Facebook API 都有生命週期和停止支援日期。Facebook 範圍和屬性可能隨 API 版本而有所不同。建議您使用 Facebook 測試您的社交身分登入,以確保聯合使用正常運作。

    • Sign In with Apple – 輸入上一節產生的 Services ID (服務 ID)、Team ID (團隊 ID)、Key ID (金鑰 ID) 和 private key (私有金鑰)。

  7. 輸入您要使用的授權範圍名稱。範圍可定義您要透過應用程式來存取的使用者屬性 (例如 nameemail)。若為 Facebook,這些屬性應以逗號分隔。若為 Google 和登入 Amazon,則應以空格分隔。若為 Sign in with Apple,請針對您想要存取的範圍勾選核取方塊。

    社交身分提供者 範例範圍
    Facebook public_profile, email
    Google profile email openid
    登入 Amazon profile postal_code
    使用 Apple 登入 email name

    會向您的應用程式使用者提示同意提供這些屬性給應用程式。如需社交供應商範圍的詳細資訊,請參閱 Google、Facebook、Login with Amazon 或 Sign in with Apple 提供的說明文件。

    透過 Sign in with Apple,以下是可能未傳回範圍的使用者案例:

    • 最終使用者在離開 Apple 的登入頁面之後遇到錯誤 (可能是 Amazon Cognito 的內部錯誤或開發人員撰寫的任何項目)

    • 服務 ID 識別碼在使用者集區和/或其他身分驗證服務之間使用

    • 開發人員在最終使用者先前登入之後新增其他範圍 (未擷取新的資訊)

    • 開發人員刪除使用者,然後使用者再次登入而未從他們的 Apple ID 描述檔移除應用程式

  8. 將 IdP 的屬性對應至您的使用者集區。如需詳細資訊,請參閱為您的使用者集區指定身分提供者屬性對應

  9. 選擇 Create (建立)。

  10. App client integration (應用程式用戶端整合) 標籤,在清單中選擇一個 App clients (應用程式用戶端),以及 Edit hosted UI settings (編輯託管 UI 設定)。將新的社交 IdP 新增至 Identity providers (身分提供者) 下的應用程式用戶端。

  11. 選擇儲存變更

步驟 3:測試社交 IdP 組態

您可以使用前兩節的元素建立登入 URL。用來測試社交 IdP 組態。

https://mydomain.us-east-1.amazoncognito.com/login?response_type=code&client_id=1example23456789&redirect_uri=https://www.example.com

您可以在使用者集區 Domain name (網域名稱) 主控台頁面上尋找您的網域。client_id 位於 App client settings (應用程式用戶端設定) 頁面上。將您的回呼 URL 使用於 redirect_uri 參數。這是您的使用者成功身分驗證後會被重新導向的頁面 URL。

注意

身分驗證請求若未在 5 分鐘內完成,Amazon Cognito 會將該請求取消,並將使用者重新導向至託管的 UI。此頁面人會顯示「Something went wrong」錯誤訊息。