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 PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
; App settings are trusted configuration. Keep a separate account_key/cache per local user.
app.i = CkJsonObject::ckCreate()
If app.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(app, 1)
success = CkJsonObject::ckLoadFile(app,"qa_data/cognito/app.json")
If success <> 1
Debug "Could not load Cognito app settings."
CkJsonObject::ckDispose(app)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(app,"region") <> 1) OR (CkJsonObject::ckStringOf(app,"region") = "")
Debug "Missing or invalid region."
CkJsonObject::ckDispose(app)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(app,"user_pool_id") <> 1) OR (CkJsonObject::ckStringOf(app,"user_pool_id") = "")
Debug "Missing or invalid user_pool_id."
CkJsonObject::ckDispose(app)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(app,"client_id") <> 1) OR (CkJsonObject::ckStringOf(app,"client_id") = "")
Debug "Missing or invalid client_id."
CkJsonObject::ckDispose(app)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(app,"account_key") <> 1) OR (CkJsonObject::ckStringOf(app,"account_key") = "")
Debug "Missing or invalid account_key."
CkJsonObject::ckDispose(app)
ProcedureReturn
EndIf
clientId.s = CkJsonObject::ckStringOf(app,"client_id")
endpoint.i = CkStringBuilder::ckCreate()
If endpoint.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(endpoint,"https://cognito-idp.")
CkStringBuilder::ckAppend(endpoint,CkJsonObject::ckStringOf(app,"region"))
CkStringBuilder::ckAppend(endpoint,".amazonaws.com/")
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkFollowRedirects(http, 0)
CkHttp::setCkConnectTimeout(http, 20)
CkHttp::setCkReadTimeout(http, 60)
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
body.i = CkJsonObject::ckCreate()
If body.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
result.i = CkJsonObject::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(result, 1)
action.s = "request"
If (action <> "request") AND (action <> "confirm")
Debug "Choose request or confirm."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndIf
input.i = CkJsonObject::ckCreate()
If input.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(input, 1)
success = CkJsonObject::ckLoadFile(input,"qa_data/cognito/reset-input.json")
If success <> 1
Debug "Could not load qa_data/cognito/reset-input.json."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(input,"username") <> 1) OR (CkJsonObject::ckStringOf(input,"username") = "")
Debug "Missing or invalid username."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(body,"ClientId",clientId)
CkJsonObject::ckUpdateString(body,"Username",CkJsonObject::ckStringOf(input,"username"))
If action = "request"
CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.ForgotPassword")
success = CkHttp::ckHttpJson(http,"POST",CkStringBuilder::ckGetAsString(endpoint),body,"application/x-amz-json-1.0",resp)
If CkHttpResponse::ckStatusCode(resp) <> 200
Debug "HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
If success = 1
Debug "Error type: " + CkJsonObject::ckStringOf(result,"__type")
Debug "OAuth error: " + CkJsonObject::ckStringOf(result,"error")
EndIf
; Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
If success <> 1
Debug "The HTTP exchange did not complete."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
If success <> 1
Debug "Expected a JSON response."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
; A public UI should not disclose whether an account exists.
Debug "If the account is eligible, a recovery code will be sent."
Else
If (CkJsonObject::ckJsonTypeOf(input,"code") <> 1) OR (CkJsonObject::ckStringOf(input,"code") = "")
Debug "Missing or invalid code."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(input,"new_password") <> 1) OR (CkJsonObject::ckStringOf(input,"new_password") = "")
Debug "Missing or invalid new_password."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(body,"ConfirmationCode",CkJsonObject::ckStringOf(input,"code"))
CkJsonObject::ckUpdateString(body,"Password",CkJsonObject::ckStringOf(input,"new_password"))
CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.ConfirmForgotPassword")
success = CkHttp::ckHttpJson(http,"POST",CkStringBuilder::ckGetAsString(endpoint),body,"application/x-amz-json-1.0",resp)
If CkHttpResponse::ckStatusCode(resp) <> 200
Debug "HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
If success = 1
Debug "Error type: " + CkJsonObject::ckStringOf(result,"__type")
Debug "OAuth error: " + CkJsonObject::ckStringOf(result,"error")
EndIf
; Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
If success <> 1
Debug "The HTTP exchange did not complete."
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndIf
Debug "Password reset accepted. Sign in with the new password."
EndIf
CkJsonObject::ckDispose(app)
CkStringBuilder::ckDispose(endpoint)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(input)
ProcedureReturn
EndProcedure