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

Amazon Cognito: Sign In and Respond to Password or MFA Challenges

See more Amazon Cognito Examples

Enable ALLOW_USER_PASSWORD_AUTH on a public app client without a secret; use a password-based pool with device remembering disabled. Managed login with PKCE is preferable when your application need not handle passwords. This example illustrates the JSON API directly: start with username/password, then respond to each returned challenge with its latest Session. It does not implement SRP, remembered-device authentication, passkeys, custom challenges, or every possible challenge.

Set action=start with a private signin-input.json containing username and password. For respond, supply username plus new_password or code. NEW_PASSWORD_REQUIRED can also require a required_attributes object keyed by the names in ChallengeParameters.requiredAttributes, for example "userAttributes.email". Supply only requested missing attributes; existing required attributes cannot be changed during this challenge. Handles NEW_PASSWORD_REQUIRED, SMS_MFA, SOFTWARE_TOKEN_MFA and EMAIL_OTP. MFA_SETUP is handed to TOTP enrollment. Other challenges stop explicitly.

The latest challenge is saved privately with the canonical username and app/account binding. Only AuthenticationResult constitutes completed sign-in; a Session is not an access token. Confirmation codes remain strings, preserving leading zeroes. On timeout/expired session start again; do not reuse a session across users or parallel flows.

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

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkJsonArray.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpResponse.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)
    ;  "start" begins sign-in. "respond" submits input for the most recent saved challenge.
    action.s = "start"
    If (action <> "start") AND (action <> "respond")
        Debug "Choose start or respond."
        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/signin-input.json")
    If success <> 1
        Debug "Could not load qa_data/cognito/signin-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

    username.s = CkJsonObject::ckStringOf(input,"username")
    CkJsonObject::ckUpdateString(body,"ClientId",clientId)
    If action = "start"
        If (CkJsonObject::ckJsonTypeOf(input,"password") <> 1) OR (CkJsonObject::ckStringOf(input,"password") = "")
            Debug "Missing or invalid 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,"AuthFlow","USER_PASSWORD_AUTH")
        CkJsonObject::ckUpdateString(body,"AuthParameters.USERNAME",username)
        CkJsonObject::ckUpdateString(body,"AuthParameters.PASSWORD",CkJsonObject::ckStringOf(input,"password"))
        CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.InitiateAuth")
        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

    Else
        challenge.i = CkJsonObject::ckCreate()
        If challenge.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkJsonObject::setCkStrict(challenge, 1)
        success = CkJsonObject::ckLoadFile(challenge,"qa_data/tokens/cognito-challenge.json")
        If success <> 1
            Debug "Could not load qa_data/tokens/cognito-challenge.json."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(challenge,"client_id") <> 1) OR (CkJsonObject::ckStringOf(challenge,"client_id") = "")
            Debug "Missing or invalid client_id."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If CkJsonObject::ckStringOf(challenge,"client_id") <> CkJsonObject::ckStringOf(app,"client_id")
            Debug "Saved data belongs to another client, pool, or local account."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(challenge,"user_pool_id") <> 1) OR (CkJsonObject::ckStringOf(challenge,"user_pool_id") = "")
            Debug "Missing or invalid user_pool_id."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If CkJsonObject::ckStringOf(challenge,"user_pool_id") <> CkJsonObject::ckStringOf(app,"user_pool_id")
            Debug "Saved data belongs to another client, pool, or local account."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(challenge,"account_key") <> 1) OR (CkJsonObject::ckStringOf(challenge,"account_key") = "")
            Debug "Missing or invalid account_key."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If CkJsonObject::ckStringOf(challenge,"account_key") <> CkJsonObject::ckStringOf(app,"account_key")
            Debug "Saved data belongs to another client, pool, or local account."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(challenge,"Session") <> 1) OR (CkJsonObject::ckStringOf(challenge,"Session") = "")
            Debug "Missing or invalid Session."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(challenge,"ChallengeName") <> 1) OR (CkJsonObject::ckStringOf(challenge,"ChallengeName") = "")
            Debug "Missing or invalid ChallengeName."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(challenge,"username") <> 1) OR (CkJsonObject::ckStringOf(challenge,"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)
            CkJsonObject::ckDispose(challenge)
            ProcedureReturn
        EndIf

        ;  Use the bound canonical username from the previous response, not a new alias.
        username = CkJsonObject::ckStringOf(challenge,"username")
        challengeName.s = CkJsonObject::ckStringOf(challenge,"ChallengeName")
        CkJsonObject::ckUpdateString(body,"Session",CkJsonObject::ckStringOf(challenge,"Session"))
        CkJsonObject::ckUpdateString(body,"ChallengeName",challengeName)
        CkJsonObject::ckUpdateString(body,"ChallengeResponses.USERNAME",username)
        If challengeName = "NEW_PASSWORD_REQUIRED"
            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)
                CkJsonObject::ckDispose(challenge)
                ProcedureReturn
            EndIf

            CkJsonObject::ckUpdateString(body,"ChallengeResponses.NEW_PASSWORD",CkJsonObject::ckStringOf(input,"new_password"))
            ;  requiredAttributes is a JSON array encoded inside a JSON string.
            requiredAttributes.i = CkJsonArray::ckCreate()
            If requiredAttributes.i = 0
                Debug "Failed to create object."
                ProcedureReturn
            EndIf

            requiredJson.s = CkJsonObject::ckStringOf(challenge,"ChallengeParameters.requiredAttributes")
            If CkJsonObject::ckHasMember(challenge,"ChallengeParameters.requiredAttributes") <> 1
                requiredJson = "[]"
            EndIf

            success = CkJsonArray::ckLoad(requiredAttributes,requiredJson)
            If success <> 1
                Debug "Invalid required-attributes list."
                CkJsonObject::ckDispose(app)
                CkStringBuilder::ckDispose(endpoint)
                CkHttp::ckDispose(http)
                CkHttpResponse::ckDispose(resp)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkJsonObject::ckDispose(input)
                CkJsonObject::ckDispose(challenge)
                CkJsonArray::ckDispose(requiredAttributes)
                ProcedureReturn
            EndIf

            answers.i = CkJsonObject::ckCreate()
            If answers.i = 0
                Debug "Failed to create object."
                ProcedureReturn
            EndIf

            success = CkJsonObject::ckObjectOf2(body,"ChallengeResponses",answers)
            If success <> 1
                Debug "Could not access challenge responses."
                CkJsonObject::ckDispose(app)
                CkStringBuilder::ckDispose(endpoint)
                CkHttp::ckDispose(http)
                CkHttpResponse::ckDispose(resp)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkJsonObject::ckDispose(input)
                CkJsonObject::ckDispose(challenge)
                CkJsonArray::ckDispose(requiredAttributes)
                CkJsonObject::ckDispose(answers)
                ProcedureReturn
            EndIf

            i.i = 0
            count.i = CkJsonArray::ckSize(requiredAttributes)
            attribute.s
            attributeValue.s
            supplied.i = CkJsonObject::ckCreate()
            If supplied.i = 0
                Debug "Failed to create object."
                ProcedureReturn
            EndIf

            If count > 0
                success = CkJsonObject::ckObjectOf2(input,"required_attributes",supplied)
                If success <> 1
                    Debug "Supply all required attributes in required_attributes."
                    CkJsonObject::ckDispose(app)
                    CkStringBuilder::ckDispose(endpoint)
                    CkHttp::ckDispose(http)
                    CkHttpResponse::ckDispose(resp)
                    CkJsonObject::ckDispose(body)
                    CkJsonObject::ckDispose(result)
                    CkJsonObject::ckDispose(input)
                    CkJsonObject::ckDispose(challenge)
                    CkJsonArray::ckDispose(requiredAttributes)
                    CkJsonObject::ckDispose(answers)
                    CkJsonObject::ckDispose(supplied)
                    ProcedureReturn
                EndIf

            EndIf

            While i < count
                attribute = CkJsonArray::ckStringAt(requiredAttributes,i)
                ;  Keys are returned as userAttributes.email, userAttributes.name, etc.
                ;  StringOf with a quoted path treats the whole key literally.
                attributePath.i = CkStringBuilder::ckCreate()
                If attributePath.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                CkStringBuilder::ckAppend(attributePath,Chr(34))
                CkStringBuilder::ckAppend(attributePath,attribute)
                CkStringBuilder::ckAppend(attributePath,Chr(34))
                attributeValue = CkJsonObject::ckStringOf(supplied,CkStringBuilder::ckGetAsString(attributePath))
                If (CkJsonObject::ckJsonTypeOf(supplied,CkStringBuilder::ckGetAsString(attributePath)) <> 1) OR (attributeValue = "")
                    Debug "A required user attribute is missing."
                    CkJsonObject::ckDispose(app)
                    CkStringBuilder::ckDispose(endpoint)
                    CkHttp::ckDispose(http)
                    CkHttpResponse::ckDispose(resp)
                    CkJsonObject::ckDispose(body)
                    CkJsonObject::ckDispose(result)
                    CkJsonObject::ckDispose(input)
                    CkJsonObject::ckDispose(challenge)
                    CkJsonArray::ckDispose(requiredAttributes)
                    CkJsonObject::ckDispose(answers)
                    CkJsonObject::ckDispose(supplied)
                    CkStringBuilder::ckDispose(attributePath)
                    ProcedureReturn
                EndIf

                CkJsonObject::ckAppendString(answers,attribute,attributeValue)
                i = i + 1
            Wend
        Else
            If (challengeName <> "SMS_MFA") AND (challengeName <> "SOFTWARE_TOKEN_MFA") AND (challengeName <> "EMAIL_OTP")
                Debug "This challenge needs a different handler: " + challengeName
                ;  MFA_SETUP is demonstrated in the TOTP enrollment example. Do not assume sign-in succeeded.
                CkJsonObject::ckDispose(app)
                CkStringBuilder::ckDispose(endpoint)
                CkHttp::ckDispose(http)
                CkHttpResponse::ckDispose(resp)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkJsonObject::ckDispose(input)
                CkJsonObject::ckDispose(challenge)
                CkJsonArray::ckDispose(requiredAttributes)
                CkJsonObject::ckDispose(answers)
                CkJsonObject::ckDispose(supplied)
                CkStringBuilder::ckDispose(attributePath)
                ProcedureReturn
            EndIf

            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)
                CkJsonObject::ckDispose(challenge)
                CkJsonArray::ckDispose(requiredAttributes)
                CkJsonObject::ckDispose(answers)
                CkJsonObject::ckDispose(supplied)
                CkStringBuilder::ckDispose(attributePath)
                ProcedureReturn
            EndIf

            If challengeName = "SMS_MFA"
                CkJsonObject::ckUpdateString(body,"ChallengeResponses.SMS_MFA_CODE",CkJsonObject::ckStringOf(input,"code"))
            EndIf

            If challengeName = "SOFTWARE_TOKEN_MFA"
                CkJsonObject::ckUpdateString(body,"ChallengeResponses.SOFTWARE_TOKEN_MFA_CODE",CkJsonObject::ckStringOf(input,"code"))
            EndIf

            If challengeName = "EMAIL_OTP"
                CkJsonObject::ckUpdateString(body,"ChallengeResponses.EMAIL_OTP_CODE",CkJsonObject::ckStringOf(input,"code"))
            EndIf

        EndIf

        CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.RespondToAuthChallenge")
        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)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            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)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            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)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            ProcedureReturn
        EndIf

    EndIf

    If CkJsonObject::ckJsonTypeOf(result,"AuthenticationResult") = 3
        token.i = CkJsonObject::ckCreate()
        If token.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkJsonObject::ckUpdateString(token,"access_token",CkJsonObject::ckStringOf(result,"AuthenticationResult.AccessToken"))
        CkJsonObject::ckUpdateString(token,"id_token",CkJsonObject::ckStringOf(result,"AuthenticationResult.IdToken"))
        CkJsonObject::ckUpdateString(token,"token_type",CkJsonObject::ckStringOf(result,"AuthenticationResult.TokenType"))
        CkJsonObject::ckUpdateInt(token,"expires_in",CkJsonObject::ckIntOf(result,"AuthenticationResult.ExpiresIn"))
        If CkJsonObject::ckHasMember(result,"AuthenticationResult.RefreshToken") = 1
            CkJsonObject::ckUpdateString(token,"refresh_token",CkJsonObject::ckStringOf(result,"AuthenticationResult.RefreshToken"))
        EndIf

        If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOf(token,"access_token") = "")
            Debug "Missing or invalid access_token."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(token,"id_token") <> 1) OR (CkJsonObject::ckStringOf(token,"id_token") = "")
            Debug "Missing or invalid id_token."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(token,"expires_in") <> 2) OR (CkJsonObject::ckIntOf(token,"expires_in") <= 0)
            Debug "Expected a positive token lifetime."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            ProcedureReturn
        EndIf

        If CkJsonObject::ckStringOf(token,"token_type") <> "Bearer"
            Debug "Expected Bearer tokens."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(token,"refresh_token") <> 1) OR (CkJsonObject::ckStringOf(token,"refresh_token") = "")
            Debug "Missing or invalid refresh_token."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            ProcedureReturn
        EndIf

        CkJsonObject::ckUpdateString(token,"client_id",CkJsonObject::ckStringOf(app,"client_id"))
        CkJsonObject::ckUpdateString(token,"user_pool_id",CkJsonObject::ckStringOf(app,"user_pool_id"))
        CkJsonObject::ckUpdateString(token,"account_key",CkJsonObject::ckStringOf(app,"account_key"))
        obtained.i = CkDateTime::ckCreate()
        If obtained.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkDateTime::ckSetFromCurrentSystemTime(obtained)
        If success <> 1
            Debug "Could not get the acquisition time."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            CkDateTime::ckDispose(obtained)
            ProcedureReturn
        EndIf

        CkJsonObject::ckUpdateString(token,"obtained_at",CkDateTime::ckGetAsTimestamp(obtained,0))
        tokenusertokens.i = CkFileAccess::ckCreate()
        If tokenusertokens.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkFileAccess::ckDirEnsureExists(tokenusertokens,"qa_data/tokens")
        If success <> 1
            Debug "Could not create the private cache directory."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            CkDateTime::ckDispose(obtained)
            CkFileAccess::ckDispose(tokenusertokens)
            ProcedureReturn
        EndIf

        If CkFileAccess::ckFileExists(tokenusertokens,"qa_data/tokens/cognito-user.json") = 1
            success = CkFileAccess::ckFileCopy(tokenusertokens,"qa_data/tokens/cognito-user.json","qa_data/tokens/cognito-user.json.bak",0)
            If success <> 1
                Debug "Could not back up the existing cache."
                CkJsonObject::ckDispose(app)
                CkStringBuilder::ckDispose(endpoint)
                CkHttp::ckDispose(http)
                CkHttpResponse::ckDispose(resp)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkJsonObject::ckDispose(input)
                CkJsonObject::ckDispose(challenge)
                CkJsonArray::ckDispose(requiredAttributes)
                CkJsonObject::ckDispose(answers)
                CkJsonObject::ckDispose(supplied)
                CkStringBuilder::ckDispose(attributePath)
                CkJsonObject::ckDispose(token)
                CkDateTime::ckDispose(obtained)
                CkFileAccess::ckDispose(tokenusertokens)
                ProcedureReturn
            EndIf

        EndIf

        success = CkJsonObject::ckWriteFile(token,"qa_data/tokens/cognito-user.json")
        If success <> 1
            Debug "Could not save user tokens; preserve any previous backup."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            CkDateTime::ckDispose(obtained)
            CkFileAccess::ckDispose(tokenusertokens)
            ProcedureReturn
        EndIf

        ;  A completed flow no longer needs its short-lived challenge session.
        cleanup.i = CkFileAccess::ckCreate()
        If cleanup.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        If CkFileAccess::ckFileExists(cleanup,"qa_data/tokens/cognito-challenge.json") = 1
            CkFileAccess::ckFileDelete(cleanup,"qa_data/tokens/cognito-challenge.json")
        EndIf

        Debug "Sign-in completed; private token cache saved."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        CkJsonObject::ckDispose(challenge)
        CkJsonArray::ckDispose(requiredAttributes)
        CkJsonObject::ckDispose(answers)
        CkJsonObject::ckDispose(supplied)
        CkStringBuilder::ckDispose(attributePath)
        CkJsonObject::ckDispose(token)
        CkDateTime::ckDispose(obtained)
        CkFileAccess::ckDispose(tokenusertokens)
        CkFileAccess::ckDispose(cleanup)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(result,"ChallengeName") <> 1) OR (CkJsonObject::ckStringOf(result,"ChallengeName") = "")
        Debug "Missing or invalid ChallengeName."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        CkJsonObject::ckDispose(challenge)
        CkJsonArray::ckDispose(requiredAttributes)
        CkJsonObject::ckDispose(answers)
        CkJsonObject::ckDispose(supplied)
        CkStringBuilder::ckDispose(attributePath)
        CkJsonObject::ckDispose(token)
        CkDateTime::ckDispose(obtained)
        CkFileAccess::ckDispose(tokenusertokens)
        CkFileAccess::ckDispose(cleanup)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(result,"Session") <> 1) OR (CkJsonObject::ckStringOf(result,"Session") = "")
        Debug "Missing or invalid Session."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        CkJsonObject::ckDispose(challenge)
        CkJsonArray::ckDispose(requiredAttributes)
        CkJsonObject::ckDispose(answers)
        CkJsonObject::ckDispose(supplied)
        CkStringBuilder::ckDispose(attributePath)
        CkJsonObject::ckDispose(token)
        CkDateTime::ckDispose(obtained)
        CkFileAccess::ckDispose(tokenusertokens)
        CkFileAccess::ckDispose(cleanup)
        ProcedureReturn
    EndIf

    ;  ChallengeParameters may provide the canonical username after alias-based sign-in.
    If (CkJsonObject::ckJsonTypeOf(result,"ChallengeParameters.USER_ID_FOR_SRP") = 1) AND (CkJsonObject::ckStringOf(result,"ChallengeParameters.USER_ID_FOR_SRP") <> "")
        username = CkJsonObject::ckStringOf(result,"ChallengeParameters.USER_ID_FOR_SRP")
    EndIf

    If (CkJsonObject::ckJsonTypeOf(result,"ChallengeParameters.USERNAME") = 1) AND (CkJsonObject::ckStringOf(result,"ChallengeParameters.USERNAME") <> "")
        username = CkJsonObject::ckStringOf(result,"ChallengeParameters.USERNAME")
    EndIf

    CkJsonObject::ckUpdateString(result,"username",username)
    CkJsonObject::ckUpdateString(result,"client_id",CkJsonObject::ckStringOf(app,"client_id"))
    CkJsonObject::ckUpdateString(result,"user_pool_id",CkJsonObject::ckStringOf(app,"user_pool_id"))
    CkJsonObject::ckUpdateString(result,"account_key",CkJsonObject::ckStringOf(app,"account_key"))
    resultauthenticationchallenge.i = CkFileAccess::ckCreate()
    If resultauthenticationchallenge.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFileAccess::ckDirEnsureExists(resultauthenticationchallenge,"qa_data/tokens")
    If success <> 1
        Debug "Could not create the private cache directory."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        CkJsonObject::ckDispose(challenge)
        CkJsonArray::ckDispose(requiredAttributes)
        CkJsonObject::ckDispose(answers)
        CkJsonObject::ckDispose(supplied)
        CkStringBuilder::ckDispose(attributePath)
        CkJsonObject::ckDispose(token)
        CkDateTime::ckDispose(obtained)
        CkFileAccess::ckDispose(tokenusertokens)
        CkFileAccess::ckDispose(cleanup)
        CkFileAccess::ckDispose(resultauthenticationchallenge)
        ProcedureReturn
    EndIf

    If CkFileAccess::ckFileExists(resultauthenticationchallenge,"qa_data/tokens/cognito-challenge.json") = 1
        success = CkFileAccess::ckFileCopy(resultauthenticationchallenge,"qa_data/tokens/cognito-challenge.json","qa_data/tokens/cognito-challenge.json.bak",0)
        If success <> 1
            Debug "Could not back up the existing cache."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            CkJsonObject::ckDispose(challenge)
            CkJsonArray::ckDispose(requiredAttributes)
            CkJsonObject::ckDispose(answers)
            CkJsonObject::ckDispose(supplied)
            CkStringBuilder::ckDispose(attributePath)
            CkJsonObject::ckDispose(token)
            CkDateTime::ckDispose(obtained)
            CkFileAccess::ckDispose(tokenusertokens)
            CkFileAccess::ckDispose(cleanup)
            CkFileAccess::ckDispose(resultauthenticationchallenge)
            ProcedureReturn
        EndIf

    EndIf

    success = CkJsonObject::ckWriteFile(result,"qa_data/tokens/cognito-challenge.json")
    If success <> 1
        Debug "Could not save authentication challenge; preserve any previous backup."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        CkJsonObject::ckDispose(challenge)
        CkJsonArray::ckDispose(requiredAttributes)
        CkJsonObject::ckDispose(answers)
        CkJsonObject::ckDispose(supplied)
        CkStringBuilder::ckDispose(attributePath)
        CkJsonObject::ckDispose(token)
        CkDateTime::ckDispose(obtained)
        CkFileAccess::ckDispose(tokenusertokens)
        CkFileAccess::ckDispose(cleanup)
        CkFileAccess::ckDispose(resultauthenticationchallenge)
        ProcedureReturn
    EndIf

    Debug "Further authentication required: " + CkJsonObject::ckStringOf(result,"ChallengeName")
    ;  Collect the requested input in your application's UI; never print the Session value.


    CkJsonObject::ckDispose(app)
    CkStringBuilder::ckDispose(endpoint)
    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(body)
    CkJsonObject::ckDispose(result)
    CkJsonObject::ckDispose(input)
    CkJsonObject::ckDispose(challenge)
    CkJsonArray::ckDispose(requiredAttributes)
    CkJsonObject::ckDispose(answers)
    CkJsonObject::ckDispose(supplied)
    CkStringBuilder::ckDispose(attributePath)
    CkJsonObject::ckDispose(token)
    CkDateTime::ckDispose(obtained)
    CkFileAccess::ckDispose(tokenusertokens)
    CkFileAccess::ckDispose(cleanup)
    CkFileAccess::ckDispose(resultauthenticationchallenge)


    ProcedureReturn
EndProcedure