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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
-- App settings are trusted configuration. Keep a separate account_key/cache per local user.
local app = chilkat.JsonObject.new()
app.strict = true
success = app:load_file("qa_data/cognito/app.json")
if not success then
print("Could not load Cognito app settings.")
return
end
if (app:json_type_of("region") ~= 1) or (app:string_of("region") == "") then
print("Missing or invalid region.")
return
end
if (app:json_type_of("user_pool_id") ~= 1) or (app:string_of("user_pool_id") == "") then
print("Missing or invalid user_pool_id.")
return
end
if (app:json_type_of("client_id") ~= 1) or (app:string_of("client_id") == "") then
print("Missing or invalid client_id.")
return
end
if (app:json_type_of("account_key") ~= 1) or (app:string_of("account_key") == "") then
print("Missing or invalid account_key.")
return
end
local clientId = app:string_of("client_id")
local endpoint = chilkat.StringBuilder.new()
endpoint:append("https://cognito-idp.")
endpoint:append(app:string_of("region"))
endpoint:append(".amazonaws.com/")
if (app:json_type_of("domain") ~= 1) or (app:string_of("domain") == "") then
print("Missing or invalid domain.")
return
end
-- This URL comes from trusted configuration, never from a token or user-supplied URL.
local tokenEndpoint = chilkat.StringBuilder.new()
tokenEndpoint:append(app:string_of("domain"))
tokenEndpoint:append("/oauth2/token")
local oauth = chilkat.OAuth2.new()
local authorizeEndpoint = chilkat.StringBuilder.new()
authorizeEndpoint:append(app:string_of("domain"))
authorizeEndpoint:append("/oauth2/authorize")
oauth.authorization_endpoint = authorizeEndpoint:get_as_string()
oauth.token_endpoint = tokenEndpoint:get_as_string()
oauth.client_id = clientId
-- Register exactly http://localhost:3017/ as an allowed callback for this public client.
oauth.local_host = "localhost"
oauth.listen_port = 3017
oauth.code_challenge = true
oauth.code_challenge_method = "S256"
-- Cognito does NOT use an offline_access scope. Code flow returns a refresh token.
oauth.scope = "openid email aws.cognito.signin.user.admin"
local authUrl = oauth:start_auth()
if not oauth.last_method_success then
print("Could not start the local OAuth listener.")
return
end
success = oauth:launch_browser(authUrl)
if not success then
oauth:cancel()
print("Could not launch the system browser.")
return
end
local waited = 0
while (oauth.auth_flow_state < 3) and (waited < 180000) do
oauth:sleep_ms(200)
waited = waited + 200
end
if oauth.auth_flow_state < 3 then
oauth:cancel()
print("Authorization timed out.")
return
end
if oauth.auth_flow_state ~= 3 then
print("Authorization did not complete.")
return
end
local token = chilkat.JsonObject.new()
token.strict = true
success = token:load(oauth.access_token_response)
if not success then
print("Could not parse the token response.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of("access_token") == "") then
print("Missing or invalid access_token.")
return
end
if (token:json_type_of("id_token") ~= 1) or (token:string_of("id_token") == "") then
print("Missing or invalid id_token.")
return
end
if (token:json_type_of("expires_in") ~= 2) or (token:int_of("expires_in") <= 0) then
print("Expected a positive token lifetime.")
return
end
if token:string_of("token_type") ~= "Bearer" then
print("Expected Bearer tokens.")
return
end
if (token:json_type_of("refresh_token") ~= 1) or (token:string_of("refresh_token") == "") then
print("Missing or invalid refresh_token.")
return
end
token:update_string("client_id", app:string_of("client_id"))
token:update_string("user_pool_id", app:string_of("user_pool_id"))
token:update_string("account_key", app:string_of("account_key"))
local obtained = chilkat.DateTime.new()
success = obtained:set_from_current_system_time()
if not success then
print("Could not get the acquisition time.")
return
end
token:update_string("obtained_at", obtained:get_as_timestamp(false))
local tokenusertokens = chilkat.FileAccess.new()
success = tokenusertokens:dir_ensure_exists("qa_data/tokens")
if not success then
print("Could not create the private cache directory.")
return
end
if tokenusertokens:file_exists("qa_data/tokens/cognito-user.json") then
success = tokenusertokens:file_copy("qa_data/tokens/cognito-user.json", "qa_data/tokens/cognito-user.json.bak", false)
if not success then
print("Could not back up the existing cache.")
return
end
end
success = token:write_file("qa_data/tokens/cognito-user.json")
if not success then
print("Could not save user tokens; preserve any previous backup.")
return
end
print("Authorization completed; private token cache saved.")
-- Validate JWT signatures and claims before trusting their contents:
-- Validate a Cognito JWT.