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

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkOAuth2W.h>
#include <C_CkDateTimeW.h>
#include <C_CkFileAccessW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW app;
    const wchar_t *clientId;
    HCkStringBuilderW endpoint;
    HCkStringBuilderW tokenEndpoint;
    HCkOAuth2W oauth;
    HCkStringBuilderW authorizeEndpoint;
    const wchar_t *authUrl;
    int waited;
    HCkJsonObjectW token;
    HCkDateTimeW obtained;
    HCkFileAccessW tokenusertokens;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    //  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    app = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(app,TRUE);
    success = CkJsonObjectW_LoadFile(app,L"qa_data/cognito/app.json");
    if (success != TRUE) {
        wprintf(L"Could not load Cognito app settings.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"region") != 1) || (CkJsonObjectW_stringOf(app,L"region") == L"")) {
        wprintf(L"Missing or invalid region.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"user_pool_id") != 1) || (CkJsonObjectW_stringOf(app,L"user_pool_id") == L"")) {
        wprintf(L"Missing or invalid user_pool_id.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"client_id") != 1) || (CkJsonObjectW_stringOf(app,L"client_id") == L"")) {
        wprintf(L"Missing or invalid client_id.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"account_key") != 1) || (CkJsonObjectW_stringOf(app,L"account_key") == L"")) {
        wprintf(L"Missing or invalid account_key.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    clientId = CkJsonObjectW_stringOf(app,L"client_id");
    endpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(endpoint,L"https://cognito-idp.");
    CkStringBuilderW_Append(endpoint,CkJsonObjectW_stringOf(app,L"region"));
    CkStringBuilderW_Append(endpoint,L".amazonaws.com/");
    if ((CkJsonObjectW_JsonTypeOf(app,L"domain") != 1) || (CkJsonObjectW_stringOf(app,L"domain") == L"")) {
        wprintf(L"Missing or invalid domain.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        return;
    }

    //  This URL comes from trusted configuration, never from a token or user-supplied URL.
    tokenEndpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(tokenEndpoint,CkJsonObjectW_stringOf(app,L"domain"));
    CkStringBuilderW_Append(tokenEndpoint,L"/oauth2/token");
    oauth = CkOAuth2W_Create();
    authorizeEndpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(authorizeEndpoint,CkJsonObjectW_stringOf(app,L"domain"));
    CkStringBuilderW_Append(authorizeEndpoint,L"/oauth2/authorize");
    CkOAuth2W_putAuthorizationEndpoint(oauth,CkStringBuilderW_getAsString(authorizeEndpoint));
    CkOAuth2W_putTokenEndpoint(oauth,CkStringBuilderW_getAsString(tokenEndpoint));
    CkOAuth2W_putClientId(oauth,clientId);
    //  Register exactly http://localhost:3017/ as an allowed callback for this public client.
    CkOAuth2W_putLocalHost(oauth,L"localhost");
    CkOAuth2W_putListenPort(oauth,3017);
    CkOAuth2W_putCodeChallenge(oauth,TRUE);
    CkOAuth2W_putCodeChallengeMethod(oauth,L"S256");
    //  Cognito does NOT use an offline_access scope. Code flow returns a refresh token.
    CkOAuth2W_putScope(oauth,L"openid email aws.cognito.signin.user.admin");
    authUrl = CkOAuth2W_startAuth(oauth);
    if (CkOAuth2W_getLastMethodSuccess(oauth) != TRUE) {
        wprintf(L"Could not start the local OAuth listener.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        return;
    }

    success = CkOAuth2W_LaunchBrowser(oauth,authUrl);
    if (success != TRUE) {
        CkOAuth2W_Cancel(oauth);
        wprintf(L"Could not launch the system browser.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        return;
    }

    waited = 0;
    while (((CkOAuth2W_getAuthFlowState(oauth) < 3) && (waited < 180000))) {
        CkOAuth2W_SleepMs(oauth,200);
        waited = waited + 200;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) < 3) {
        CkOAuth2W_Cancel(oauth);
        wprintf(L"Authorization timed out.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        return;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) != 3) {
        wprintf(L"Authorization did not complete.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        return;
    }

    token = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(token,TRUE);
    success = CkJsonObjectW_Load(token,CkOAuth2W_accessTokenResponse(oauth));
    if (success != TRUE) {
        wprintf(L"Could not parse the token response.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"access_token") != 1) || (CkJsonObjectW_stringOf(token,L"access_token") == L"")) {
        wprintf(L"Missing or invalid access_token.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"id_token") != 1) || (CkJsonObjectW_stringOf(token,L"id_token") == L"")) {
        wprintf(L"Missing or invalid id_token.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"expires_in") != 2) || (CkJsonObjectW_IntOf(token,L"expires_in") <= 0)) {
        wprintf(L"Expected a positive token lifetime.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if (CkJsonObjectW_stringOf(token,L"token_type") != L"Bearer") {
        wprintf(L"Expected Bearer tokens.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"refresh_token") != 1) || (CkJsonObjectW_stringOf(token,L"refresh_token") == L"")) {
        wprintf(L"Missing or invalid refresh_token.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        return;
    }

    CkJsonObjectW_UpdateString(token,L"client_id",CkJsonObjectW_stringOf(app,L"client_id"));
    CkJsonObjectW_UpdateString(token,L"user_pool_id",CkJsonObjectW_stringOf(app,L"user_pool_id"));
    CkJsonObjectW_UpdateString(token,L"account_key",CkJsonObjectW_stringOf(app,L"account_key"));
    obtained = CkDateTimeW_Create();
    success = CkDateTimeW_SetFromCurrentSystemTime(obtained);
    if (success != TRUE) {
        wprintf(L"Could not get the acquisition time.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        CkDateTimeW_Dispose(obtained);
        return;
    }

    CkJsonObjectW_UpdateString(token,L"obtained_at",CkDateTimeW_getAsTimestamp(obtained,FALSE));
    tokenusertokens = CkFileAccessW_Create();
    success = CkFileAccessW_DirEnsureExists(tokenusertokens,L"qa_data/tokens");
    if (success != TRUE) {
        wprintf(L"Could not create the private cache directory.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        CkDateTimeW_Dispose(obtained);
        CkFileAccessW_Dispose(tokenusertokens);
        return;
    }

    if (CkFileAccessW_FileExists(tokenusertokens,L"qa_data/tokens/cognito-user.json") == TRUE) {
        success = CkFileAccessW_FileCopy(tokenusertokens,L"qa_data/tokens/cognito-user.json",L"qa_data/tokens/cognito-user.json.bak",FALSE);
        if (success != TRUE) {
            wprintf(L"Could not back up the existing cache.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkOAuth2W_Dispose(oauth);
            CkStringBuilderW_Dispose(authorizeEndpoint);
            CkJsonObjectW_Dispose(token);
            CkDateTimeW_Dispose(obtained);
            CkFileAccessW_Dispose(tokenusertokens);
            return;
        }

    }

    success = CkJsonObjectW_WriteFile(token,L"qa_data/tokens/cognito-user.json");
    if (success != TRUE) {
        wprintf(L"Could not save user tokens; preserve any previous backup.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkOAuth2W_Dispose(oauth);
        CkStringBuilderW_Dispose(authorizeEndpoint);
        CkJsonObjectW_Dispose(token);
        CkDateTimeW_Dispose(obtained);
        CkFileAccessW_Dispose(tokenusertokens);
        return;
    }

    wprintf(L"Authorization completed; private token cache saved.\n");
    //  Validate JWT signatures and claims before trusting their contents:
    //  Validate a Cognito JWT.


    CkJsonObjectW_Dispose(app);
    CkStringBuilderW_Dispose(endpoint);
    CkStringBuilderW_Dispose(tokenEndpoint);
    CkOAuth2W_Dispose(oauth);
    CkStringBuilderW_Dispose(authorizeEndpoint);
    CkJsonObjectW_Dispose(token);
    CkDateTimeW_Dispose(obtained);
    CkFileAccessW_Dispose(tokenusertokens);

    }