Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

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/"
if {expr [[CkJsonObject_JsonTypeOf $app "domain"] != 1]  ||  [[CkJsonObject_stringOf $app "domain"] == ""]} then {
    puts "Missing or invalid domain."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    exit
}

#  This URL comes from trusted configuration, never from a token or user-supplied URL.
set tokenEndpoint [new_CkStringBuilder]

CkStringBuilder_Append $tokenEndpoint [CkJsonObject_stringOf $app "domain"]
CkStringBuilder_Append $tokenEndpoint "/oauth2/token"
set oauth [new_CkOAuth2]

set authorizeEndpoint [new_CkStringBuilder]

CkStringBuilder_Append $authorizeEndpoint [CkJsonObject_stringOf $app "domain"]
CkStringBuilder_Append $authorizeEndpoint "/oauth2/authorize"
CkOAuth2_put_AuthorizationEndpoint $oauth [CkStringBuilder_getAsString $authorizeEndpoint]
CkOAuth2_put_TokenEndpoint $oauth [CkStringBuilder_getAsString $tokenEndpoint]
CkOAuth2_put_ClientId $oauth $clientId
#  Register exactly http://localhost:3017/ as an allowed callback for this public client.
CkOAuth2_put_LocalHost $oauth "localhost"
CkOAuth2_put_ListenPort $oauth 3017
CkOAuth2_put_CodeChallenge $oauth 1
CkOAuth2_put_CodeChallengeMethod $oauth "S256"
#  Cognito does NOT use an offline_access scope. Code flow returns a refresh token.
CkOAuth2_put_Scope $oauth "openid email aws.cognito.signin.user.admin"
set authUrl [CkOAuth2_startAuth $oauth]
if {[CkOAuth2_get_LastMethodSuccess $oauth] != 1} then {
    puts "Could not start the local OAuth listener."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    exit
}

set success [CkOAuth2_LaunchBrowser $oauth $authUrl]
if {$success != 1} then {
    CkOAuth2_Cancel $oauth
    puts "Could not launch the system browser."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    exit
}

set waited 0
while {expr [[CkOAuth2_get_AuthFlowState $oauth] < 3]  &&  [$waited < 180000]} {
    CkOAuth2_SleepMs $oauth 200
    set waited [expr $waited + 200]
}
if {[CkOAuth2_get_AuthFlowState $oauth] < 3} then {
    CkOAuth2_Cancel $oauth
    puts "Authorization timed out."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    exit
}

if {[CkOAuth2_get_AuthFlowState $oauth] != 3} then {
    puts "Authorization did not complete."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    exit
}

set token [new_CkJsonObject]

CkJsonObject_put_Strict $token 1
set success [CkJsonObject_Load $token [CkOAuth2_accessTokenResponse $oauth]]
if {$success != 1} then {
    puts "Could not parse the token response."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_stringOf $token "access_token"] == ""]} then {
    puts "Missing or invalid access_token."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "id_token"] != 1]  ||  [[CkJsonObject_stringOf $token "id_token"] == ""]} then {
    puts "Missing or invalid id_token."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "expires_in"] != 2]  ||  [[CkJsonObject_IntOf $token "expires_in"] <= 0]} then {
    puts "Expected a positive token lifetime."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    exit
}

if {[CkJsonObject_stringOf $token "token_type"] != "Bearer"} then {
    puts "Expected Bearer tokens."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "refresh_token"] != 1]  ||  [[CkJsonObject_stringOf $token "refresh_token"] == ""]} then {
    puts "Missing or invalid refresh_token."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    exit
}

CkJsonObject_UpdateString $token "client_id" [CkJsonObject_stringOf $app "client_id"]
CkJsonObject_UpdateString $token "user_pool_id" [CkJsonObject_stringOf $app "user_pool_id"]
CkJsonObject_UpdateString $token "account_key" [CkJsonObject_stringOf $app "account_key"]
set obtained [new_CkDateTime]

set success [CkDateTime_SetFromCurrentSystemTime $obtained]
if {$success != 1} then {
    puts "Could not get the acquisition time."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    delete_CkDateTime $obtained
    exit
}

CkJsonObject_UpdateString $token "obtained_at" [CkDateTime_getAsTimestamp $obtained 0]
set tokenusertokens [new_CkFileAccess]

set success [CkFileAccess_DirEnsureExists $tokenusertokens "qa_data/tokens"]
if {$success != 1} then {
    puts "Could not create the private cache directory."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    delete_CkDateTime $obtained
    delete_CkFileAccess $tokenusertokens
    exit
}

if {[CkFileAccess_FileExists $tokenusertokens "qa_data/tokens/cognito-user.json"] == 1} then {
    set success [CkFileAccess_FileCopy $tokenusertokens "qa_data/tokens/cognito-user.json" "qa_data/tokens/cognito-user.json.bak" 0]
    if {$success != 1} then {
        puts "Could not back up the existing cache."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkStringBuilder $tokenEndpoint
        delete_CkOAuth2 $oauth
        delete_CkStringBuilder $authorizeEndpoint
        delete_CkJsonObject $token
        delete_CkDateTime $obtained
        delete_CkFileAccess $tokenusertokens
        exit
    }

}

set success [CkJsonObject_WriteFile $token "qa_data/tokens/cognito-user.json"]
if {$success != 1} then {
    puts "Could not save user tokens; preserve any previous backup."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkStringBuilder $tokenEndpoint
    delete_CkOAuth2 $oauth
    delete_CkStringBuilder $authorizeEndpoint
    delete_CkJsonObject $token
    delete_CkDateTime $obtained
    delete_CkFileAccess $tokenusertokens
    exit
}

puts "Authorization completed; private token cache saved."
#  Validate JWT signatures and claims before trusting their contents:
#  Validate a Cognito JWT.

delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $tokenEndpoint
delete_CkOAuth2 $oauth
delete_CkStringBuilder $authorizeEndpoint
delete_CkJsonObject $token
delete_CkDateTime $obtained
delete_CkFileAccess $tokenusertokens