Sample code for 30+ languages & platforms
VBScript Requires Chilkat v11.6.0+

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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 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.
set app = CreateObject("Chilkat.JsonObject")
app.Strict = 1
success = app.LoadFile("qa_data/cognito/app.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load Cognito app settings.")
    WScript.Quit
End If

If ((app.JsonTypeOf("region") <> 1) Or (app.StringOf("region") = "")) Then
    outFile.WriteLine("Missing or invalid region.")
    WScript.Quit
End If

If ((app.JsonTypeOf("user_pool_id") <> 1) Or (app.StringOf("user_pool_id") = "")) Then
    outFile.WriteLine("Missing or invalid user_pool_id.")
    WScript.Quit
End If

If ((app.JsonTypeOf("client_id") <> 1) Or (app.StringOf("client_id") = "")) Then
    outFile.WriteLine("Missing or invalid client_id.")
    WScript.Quit
End If

If ((app.JsonTypeOf("account_key") <> 1) Or (app.StringOf("account_key") = "")) Then
    outFile.WriteLine("Missing or invalid account_key.")
    WScript.Quit
End If

clientId = app.StringOf("client_id")
set endpoint = CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("https://cognito-idp.")
success = endpoint.Append(app.StringOf("region"))
success = endpoint.Append(".amazonaws.com/")
If ((app.JsonTypeOf("domain") <> 1) Or (app.StringOf("domain") = "")) Then
    outFile.WriteLine("Missing or invalid domain.")
    WScript.Quit
End If

'  This URL comes from trusted configuration, never from a token or user-supplied URL.
set tokenEndpoint = CreateObject("Chilkat.StringBuilder")
success = tokenEndpoint.Append(app.StringOf("domain"))
success = tokenEndpoint.Append("/oauth2/token")
set oauth = CreateObject("Chilkat.OAuth2")
set authorizeEndpoint = CreateObject("Chilkat.StringBuilder")
success = authorizeEndpoint.Append(app.StringOf("domain"))
success = authorizeEndpoint.Append("/oauth2/authorize")
oauth.AuthorizationEndpoint = authorizeEndpoint.GetAsString()
oauth.TokenEndpoint = tokenEndpoint.GetAsString()
oauth.ClientId = clientId
'  Register exactly http://localhost:3017/ as an allowed callback for this public client.
oauth.LocalHost = "localhost"
oauth.ListenPort = 3017
oauth.CodeChallenge = 1
oauth.CodeChallengeMethod = "S256"
'  Cognito does NOT use an offline_access scope. Code flow returns a refresh token.
oauth.Scope = "openid email aws.cognito.signin.user.admin"
authUrl = oauth.StartAuth()
If (oauth.LastMethodSuccess <> 1) Then
    outFile.WriteLine("Could not start the local OAuth listener.")
    WScript.Quit
End If

success = oauth.LaunchBrowser(authUrl)
If (success <> 1) Then
    success = oauth.Cancel()
    outFile.WriteLine("Could not launch the system browser.")
    WScript.Quit
End If

waited = 0
Do While ((oauth.AuthFlowState < 3) And (waited < 180000))
    oauth.SleepMs 200
    waited = waited + 200
Loop
If (oauth.AuthFlowState < 3) Then
    success = oauth.Cancel()
    outFile.WriteLine("Authorization timed out.")
    WScript.Quit
End If

If (oauth.AuthFlowState <> 3) Then
    outFile.WriteLine("Authorization did not complete.")
    WScript.Quit
End If

set token = CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.Load(oauth.AccessTokenResponse)
If (success <> 1) Then
    outFile.WriteLine("Could not parse the token response.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOf("access_token") = "")) Then
    outFile.WriteLine("Missing or invalid access_token.")
    WScript.Quit
End If

If ((token.JsonTypeOf("id_token") <> 1) Or (token.StringOf("id_token") = "")) Then
    outFile.WriteLine("Missing or invalid id_token.")
    WScript.Quit
End If

If ((token.JsonTypeOf("expires_in") <> 2) Or (token.IntOf("expires_in") <= 0)) Then
    outFile.WriteLine("Expected a positive token lifetime.")
    WScript.Quit
End If

If (token.StringOf("token_type") <> "Bearer") Then
    outFile.WriteLine("Expected Bearer tokens.")
    WScript.Quit
End If

If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOf("refresh_token") = "")) Then
    outFile.WriteLine("Missing or invalid refresh_token.")
    WScript.Quit
End If

success = token.UpdateString("client_id",app.StringOf("client_id"))
success = token.UpdateString("user_pool_id",app.StringOf("user_pool_id"))
success = token.UpdateString("account_key",app.StringOf("account_key"))
set obtained = CreateObject("Chilkat.CkDateTime")
success = obtained.SetFromCurrentSystemTime()
If (success <> 1) Then
    outFile.WriteLine("Could not get the acquisition time.")
    WScript.Quit
End If

success = token.UpdateString("obtained_at",obtained.GetAsTimestamp(0))
set tokenusertokens = CreateObject("Chilkat.FileAccess")
success = tokenusertokens.DirEnsureExists("qa_data/tokens")
If (success <> 1) Then
    outFile.WriteLine("Could not create the private cache directory.")
    WScript.Quit
End If

If (tokenusertokens.FileExists("qa_data/tokens/cognito-user.json") = 1) Then
    success = tokenusertokens.FileCopy("qa_data/tokens/cognito-user.json","qa_data/tokens/cognito-user.json.bak",0)
    If (success <> 1) Then
        outFile.WriteLine("Could not back up the existing cache.")
        WScript.Quit
    End If

End If

success = token.WriteFile("qa_data/tokens/cognito-user.json")
If (success <> 1) Then
    outFile.WriteLine("Could not save user tokens; preserve any previous backup.")
    WScript.Quit
End If

outFile.WriteLine("Authorization completed; private token cache saved.")
'  Validate JWT signatures and claims before trusting their contents:
'  Validate a Cognito JWT.

outFile.Close