Amazon Cognito: Sign In with Managed Login and PKCE
See more Amazon Cognito Examples
Configure a User Pool domain and a public app client with no secret. Enable the authorization-code grant, the Cognito identity provider (or your configured IdP), and the requested openid, email, and aws.cognito.signin.user.admin scopes. Register http://localhost:3017/ exactly as an allowed callback URL. Chilkat receives the local callback and manages state and S256 PKCE; no hosted callback service is needed. Use the system browser, not a password form embedded in your application.
The self-service admin scope permits user operations such as TOTP enrollment; it does not grant IAM administration. Cognito code flow returns refresh tokens without offline_access. A public desktop client must not contain a client secret. Web/mobile applications need their own appropriate callback handling. Tokens are saved privately; decoding their claims is not validation.
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
; App settings are trusted configuration. Keep a separate account_key/cache per local user.
$oApp = ObjCreate("Chilkat.JsonObject")
$oApp.Strict = True
$bSuccess = $oApp.LoadFile("qa_data/cognito/app.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load Cognito app settings." & @CRLF)
Exit
EndIf
If (($oApp.JsonTypeOf("region") <> 1) Or ($oApp.StringOf("region") = "")) Then
ConsoleWrite("Missing or invalid region." & @CRLF)
Exit
EndIf
If (($oApp.JsonTypeOf("user_pool_id") <> 1) Or ($oApp.StringOf("user_pool_id") = "")) Then
ConsoleWrite("Missing or invalid user_pool_id." & @CRLF)
Exit
EndIf
If (($oApp.JsonTypeOf("client_id") <> 1) Or ($oApp.StringOf("client_id") = "")) Then
ConsoleWrite("Missing or invalid client_id." & @CRLF)
Exit
EndIf
If (($oApp.JsonTypeOf("account_key") <> 1) Or ($oApp.StringOf("account_key") = "")) Then
ConsoleWrite("Missing or invalid account_key." & @CRLF)
Exit
EndIf
Local $sClientId = $oApp.StringOf("client_id")
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("https://cognito-idp.")
$oEndpoint.Append($oApp.StringOf("region"))
$oEndpoint.Append(".amazonaws.com/")
If (($oApp.JsonTypeOf("domain") <> 1) Or ($oApp.StringOf("domain") = "")) Then
ConsoleWrite("Missing or invalid domain." & @CRLF)
Exit
EndIf
; This URL comes from trusted configuration, never from a token or user-supplied URL.
$oTokenEndpoint = ObjCreate("Chilkat.StringBuilder")
$oTokenEndpoint.Append($oApp.StringOf("domain"))
$oTokenEndpoint.Append("/oauth2/token")
$oOauth = ObjCreate("Chilkat.OAuth2")
$oAuthorizeEndpoint = ObjCreate("Chilkat.StringBuilder")
$oAuthorizeEndpoint.Append($oApp.StringOf("domain"))
$oAuthorizeEndpoint.Append("/oauth2/authorize")
$oOauth.AuthorizationEndpoint = $oAuthorizeEndpoint.GetAsString()
$oOauth.TokenEndpoint = $oTokenEndpoint.GetAsString()
$oOauth.ClientId = $sClientId
; Register exactly http://localhost:3017/ as an allowed callback for this public client.
$oOauth.LocalHost = "localhost"
$oOauth.ListenPort = 3017
$oOauth.CodeChallenge = True
$oOauth.CodeChallengeMethod = "S256"
; Cognito does NOT use an offline_access scope. Code flow returns a refresh token.
$oOauth.Scope = "openid email aws.cognito.signin.user.admin"
Local $sAuthUrl = $oOauth.StartAuth()
If ($oOauth.LastMethodSuccess <> True) Then
ConsoleWrite("Could not start the local OAuth listener." & @CRLF)
Exit
EndIf
$bSuccess = $oOauth.LaunchBrowser($sAuthUrl)
If ($bSuccess <> True) Then
$oOauth.Cancel()
ConsoleWrite("Could not launch the system browser." & @CRLF)
Exit
EndIf
Local $iWaited = 0
While (($oOauth.AuthFlowState < 3) And ($iWaited < 180000))
$oOauth.SleepMs 200
$iWaited = $iWaited + 200
Wend
If ($oOauth.AuthFlowState < 3) Then
$oOauth.Cancel()
ConsoleWrite("Authorization timed out." & @CRLF)
Exit
EndIf
If ($oOauth.AuthFlowState <> 3) Then
ConsoleWrite("Authorization did not complete." & @CRLF)
Exit
EndIf
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.Load($oOauth.AccessTokenResponse)
If ($bSuccess <> True) Then
ConsoleWrite("Could not parse the token response." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOf("access_token") = "")) Then
ConsoleWrite("Missing or invalid access_token." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("id_token") <> 1) Or ($oToken.StringOf("id_token") = "")) Then
ConsoleWrite("Missing or invalid id_token." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("expires_in") <> 2) Or ($oToken.IntOf("expires_in") <= 0)) Then
ConsoleWrite("Expected a positive token lifetime." & @CRLF)
Exit
EndIf
If ($oToken.StringOf("token_type") <> "Bearer") Then
ConsoleWrite("Expected Bearer tokens." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("refresh_token") <> 1) Or ($oToken.StringOf("refresh_token") = "")) Then
ConsoleWrite("Missing or invalid refresh_token." & @CRLF)
Exit
EndIf
$oToken.UpdateString("client_id",$oApp.StringOf("client_id"))
$oToken.UpdateString("user_pool_id",$oApp.StringOf("user_pool_id"))
$oToken.UpdateString("account_key",$oApp.StringOf("account_key"))
$oObtained = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oObtained.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
ConsoleWrite("Could not get the acquisition time." & @CRLF)
Exit
EndIf
$oToken.UpdateString("obtained_at",$oObtained.GetAsTimestamp(False))
$oTokenusertokens = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oTokenusertokens.DirEnsureExists("qa_data/tokens")
If ($bSuccess <> True) Then
ConsoleWrite("Could not create the private cache directory." & @CRLF)
Exit
EndIf
If ($oTokenusertokens.FileExists("qa_data/tokens/cognito-user.json") = True) Then
$bSuccess = $oTokenusertokens.FileCopy("qa_data/tokens/cognito-user.json","qa_data/tokens/cognito-user.json.bak",False)
If ($bSuccess <> True) Then
ConsoleWrite("Could not back up the existing cache." & @CRLF)
Exit
EndIf
EndIf
$bSuccess = $oToken.WriteFile("qa_data/tokens/cognito-user.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not save user tokens; preserve any previous backup." & @CRLF)
Exit
EndIf
ConsoleWrite("Authorization completed; private token cache saved." & @CRLF)
; Validate JWT signatures and claims before trusting their contents:
; Validate a Cognito JWT.