如何使用 AWS CLI 在 Amazon Cognito 中重設使用者密碼?

2 分的閱讀內容
0

我需要學習如何使用 AWS Command Line Interface (AWS CLI),來協助使用者在 Amazon Cognito 中重設或變更密碼。

解決方案

您可以使用 AWS CLI 重設或變更 Amazon Cognito 密碼。Amazon Cognito 管理員可以啟動重設密碼流程來重設使用者密碼。Amazon Cognito 使用者可以自行變更密碼,或 Amazon Cognito 管理員可以暫時或永久地設定使用者密碼。

重設使用者密碼

若要啟動重設密碼流程,管理員和使用者必須執行下列步驟:

1.    管理員叫用 AdminResetUserPassword API。

**重要提示:**在這些範例 AWS Command Line Interface (AWS CLI) 命令中,用您的值取代範例字串的所有執行個體。(例如,用使用者集區識別碼取代 "example_user_pool_id"。)

範例 admin-reset-user-password 命令:

aws cognito-idp admin-reset-user-password --user-pool-id example_user_pool_id --username example_user_name

2.    當使用者的密碼重設且使用者嘗試登入時,他們會收到 PasswordResetRequiredException 例外狀況。然後,系統會將使用者重新導向至重設密碼流程。

**注意:**重設密碼流程與忘記密碼流程相同。

3.    擁有已驗證電子郵件地址或電話號碼的使用者會收到一則訊息,其中包含重設密碼所需的確認代碼。

使用未驗證的使用者屬性:

1.    如果使用者沒有經過驗證的電子郵件地址或電話號碼,則管理員會在叫用 AdminResetUserPassword API 時收到下列錯誤訊息:

An error occurred (InvalidParameterException) when calling the AdminResetUserPassword operation: Cannot reset password for the user as there is no registered/verified email or phone_number

2.    若要修正這個問題,管理員會叫用 AdminUpdateUserAttributes API,並將 email_verifiedphone_number_verified 的屬性設定為 true

範例 admin-update-user-attributes 命令:

aws cognito-idp admin-update-user-attributes --user-pool-id example_user_pool_id --username example_user_name --user-attributes Name="email_verified",Value="true"

3.    如果使用者沒有收到確認代碼,請遵循疑難排解問題的最佳實務

使用者完成忘記密碼流程:

1.    收到確認代碼後,使用者可以透過叫用 ConfirmForgotPassword API 建立新密碼。

範例 confirm-forgot-password 命令:

aws cognito-idp confirm-forgot-password --client-id example_client_id --username example_user_name --confirmation-code example_code --password example_new password

變更使用者密碼

使用者可以自行變更密碼,管理員可以暫時或永久設定使用者密碼。若要變更使用者密碼,使用者或管理員會執行下列步驟:

使用者變更使用者密碼:

1.    使用者會叫用 ChangePassword API。使用者必須擁有由 Amazon Cognito 核發的有效存取權杖,才能叫用 ChangePassword API。

範例 change-password 命令:

aws cognito-idp change-password --previous-password example_old_password --proposed-password example_new_password --access-token valid_access_token

管理員會建立永久性的新使用者密碼:

1.    管理員叫用 AdminSetUserPassword API 來建立新的永久性密碼。

範例 admin-set-user-password 以永久變更使用者密碼:

aws cognito-idp admin-set-user-password --user-pool-id example_user_pool_id --username example_user_name --password example_new_password --permanent

2.    管理員永久變更使用者密碼後,使用者就可以使用新密碼登入該應用程式。

管理員建立新的臨時使用者密碼:

1.    管理員叫用 AdminSetUserPassword API 來建立新的臨時密碼。

範例 admin-set-user-password 以暫時變更使用者密碼:

aws cognito-idp admin-set-user-password --user-pool-id example_user_pool_id --username example_user_name --password example_temporary_password --no-permanent

如果管理員將使用者密碼變更為暫時密碼,則使用者必須採取其他步驟。

2.    使用臨時密碼叫用 AdminSetUserPassword API 後,使用者的狀態會變更為「FORCE_CHANGE_PASSWORD」。嘗試使用臨時密碼叫用 InitiateAuth API 來登入的使用者會收到「NEW_PASSWORD_REQUIRED」驗證挑戰。

範例 initiate-auth 命令:

aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH --auth-parameters USERNAME=example_user_name,PASSWORD=example_temporary_password --client-id example_client_id

輸出:

{
  "ChallengeName": "NEW_PASSWORD_REQUIRED",
  "Session": "AYA......",
  "ChallengeParameters": {
    "USER_ID_FOR_SRP": "544.....",
    "requiredAttributes": "[]",
    "userAttributes": "{\"email_verified\":\"true\",\"email\":\"user@example.com\"}"
  }
}

注意:工作階段權杖的有效性取決於應用程式用戶端的身份驗證流程工作階段持續時間設定。根據預設,此工作階段權杖的工作階段持續時間為 3 分鐘。如需修改權杖的有效性,請參閱設定應用程式用戶端身份驗證流程工作階段持續時間的步驟。

3.    使用者回應「NEW_PASSWORD_REQUIRED」的挑戰,方法是使用他們在上一個步驟中收到的工作階段權杖叫用 RespondToAuthChallenge API。

範例 respond-to-auth-challenge 命令:

aws cognito-idp respond-to-auth-challenge --client-id example_client_id --challenge-name NEW_PASSWORD_REQUIRED --challenge-responses USERNAME=example_username,NEW_PASSWORD="example_new_password" --session "example_session_token"

4.    使用者接收 ID、存取和重新整理權杖並登入應用程式。

**重要提示:**如果應用程式用戶端在使用者集區中設定了用戶端密碼,則必須提供密碼雜湊。若要進一步了解,請參閱如何對 Amazon Cognito 使用者集區 API 的 "Unable to verify secret hash for client <client-id>"(無法驗證用戶端 <client-id> 的機密雜湊)錯誤進行疑難排解?


AWS 官方
AWS 官方已更新 1 年前