Amazon Cognito: Reset a Forgotten Password
See more Amazon Cognito Examples
Choose request to send a recovery code, then confirm after the user supplies it. Private reset-input.json contains username for request and username, code, new_password for confirm. Keep codes as strings. The user must be eligible under the pool's account-recovery settings and have an available verified delivery attribute. MFA settings can affect which recovery methods are available.
Live operation: sends a recovery message or changes a password. It does not authenticate the user; perform a new sign-in afterward. Enable PreventUserExistenceErrors on the app client and use neutral UI wording on both success and failure. Do not expose detailed AWS errors as an account-enumeration mechanism.
Uses direct Chilkat.Http requests to the regional Cognito User Pools JSON API. Public app-client operations do not use IAM signing. These examples use a public app client without a client secret; do not add administrative AWS credentials. For a confidential app client, implement the operation-specific SecretHash/SECRET_HASH or ClientSecret requirements on a trusted system.
Create qa_data/cognito/app.json with string fields region (for example us-west-2), user_pool_id, client_id, and account_key (your local account identifier). OAuth examples also require domain, the trusted HTTPS base URL of your Cognito domain, without a trailing slash. The region and user pool must match the app client. These examples use commercial AWS endpoints.
Sample cache/input files contain secrets. Use an access-controlled directory outside web content and source control, separate each local user/account, and serialize operations that modify the same cache. In a real application obtain passwords and codes through private UI instead of leaving them in files. Cache writes retain a .bak copy; protect and remove obsolete backups and challenge files after the flow. Production storage should provide atomic replacement and OS-backed secret protection. Never restore an old refresh token automatically after rotation. These examples do not require a hosted web server.
Checks transport completion, HTTP status, and required response fields before using results. Cognito often reports throttling, invalid codes, and authentication failures as HTTP 400. Present generic sign-in/recovery errors to users; keep detailed diagnostics private. Retry throttled reads with bounded backoff, but reconcile uncertain writes and refresh rotations before repeating them.
AWS documentation 1 | AWS documentation 2
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
# App settings are trusted configuration. Keep a separate account_key/cache per local user.
set app [new_CkJsonObject]
CkJsonObject_put_Strict $app 1
set success [CkJsonObject_LoadFile $app "qa_data/cognito/app.json"]
if {$success != 1} then {
puts "Could not load Cognito app settings."
delete_CkJsonObject $app
exit
}
if {expr [[CkJsonObject_JsonTypeOf $app "region"] != 1] || [[CkJsonObject_stringOf $app "region"] == ""]} then {
puts "Missing or invalid region."
delete_CkJsonObject $app
exit
}
if {expr [[CkJsonObject_JsonTypeOf $app "user_pool_id"] != 1] || [[CkJsonObject_stringOf $app "user_pool_id"] == ""]} then {
puts "Missing or invalid user_pool_id."
delete_CkJsonObject $app
exit
}
if {expr [[CkJsonObject_JsonTypeOf $app "client_id"] != 1] || [[CkJsonObject_stringOf $app "client_id"] == ""]} then {
puts "Missing or invalid client_id."
delete_CkJsonObject $app
exit
}
if {expr [[CkJsonObject_JsonTypeOf $app "account_key"] != 1] || [[CkJsonObject_stringOf $app "account_key"] == ""]} then {
puts "Missing or invalid account_key."
delete_CkJsonObject $app
exit
}
set clientId [CkJsonObject_stringOf $app "client_id"]
set endpoint [new_CkStringBuilder]
CkStringBuilder_Append $endpoint "https://cognito-idp."
CkStringBuilder_Append $endpoint [CkJsonObject_stringOf $app "region"]
CkStringBuilder_Append $endpoint ".amazonaws.com/"
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 20
CkHttp_put_ReadTimeout $http 60
set resp [new_CkHttpResponse]
set body [new_CkJsonObject]
set result [new_CkJsonObject]
CkJsonObject_put_Strict $result 1
set action "request"
if {expr [$action != "request"] && [$action != "confirm"]} then {
puts "Choose request or confirm."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
exit
}
set input [new_CkJsonObject]
CkJsonObject_put_Strict $input 1
set success [CkJsonObject_LoadFile $input "qa_data/cognito/reset-input.json"]
if {$success != 1} then {
puts "Could not load qa_data/cognito/reset-input.json."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
if {expr [[CkJsonObject_JsonTypeOf $input "username"] != 1] || [[CkJsonObject_stringOf $input "username"] == ""]} then {
puts "Missing or invalid username."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
CkJsonObject_UpdateString $body "ClientId" $clientId
CkJsonObject_UpdateString $body "Username" [CkJsonObject_stringOf $input "username"]
if {$action == "request"} then {
CkHttp_SetRequestHeader $http "X-Amz-Target" "AWSCognitoIdentityProviderService.ForgotPassword"
set success [CkHttp_HttpJson $http "POST" [CkStringBuilder_getAsString $endpoint] $body "application/x-amz-json-1.0" $resp]
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
puts "HTTP status: [CkHttpResponse_get_StatusCode $resp]"
set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$success == 1} then {
puts "Error type: [CkJsonObject_stringOf $result __type]"
puts "OAuth error: [CkJsonObject_stringOf $result error]"
}
# Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
if {$success != 1} then {
puts "The HTTP exchange did not complete."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
puts "Expected a JSON response."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
# A public UI should not disclose whether an account exists.
puts "If the account is eligible, a recovery code will be sent."
} else {
if {expr [[CkJsonObject_JsonTypeOf $input "code"] != 1] || [[CkJsonObject_stringOf $input "code"] == ""]} then {
puts "Missing or invalid code."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
if {expr [[CkJsonObject_JsonTypeOf $input "new_password"] != 1] || [[CkJsonObject_stringOf $input "new_password"] == ""]} then {
puts "Missing or invalid new_password."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
CkJsonObject_UpdateString $body "ConfirmationCode" [CkJsonObject_stringOf $input "code"]
CkJsonObject_UpdateString $body "Password" [CkJsonObject_stringOf $input "new_password"]
CkHttp_SetRequestHeader $http "X-Amz-Target" "AWSCognitoIdentityProviderService.ConfirmForgotPassword"
set success [CkHttp_HttpJson $http "POST" [CkStringBuilder_getAsString $endpoint] $body "application/x-amz-json-1.0" $resp]
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
puts "HTTP status: [CkHttpResponse_get_StatusCode $resp]"
set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$success == 1} then {
puts "Error type: [CkJsonObject_stringOf $result __type]"
puts "OAuth error: [CkJsonObject_stringOf $result error]"
}
# Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
if {$success != 1} then {
puts "The HTTP exchange did not complete."
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input
exit
}
puts "Password reset accepted. Sign in with the new password."
}
delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input