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

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    --  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    DECLARE @app int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @app OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @app, 'Strict', 1
    EXEC sp_OAMethod @app, 'LoadFile', @success OUT, 'qa_data/cognito/app.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load Cognito app settings.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'region'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid region.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'user_pool_id'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid user_pool_id.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid client_id.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'account_key'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid account_key.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    DECLARE @clientId nvarchar(4000)
    EXEC sp_OAMethod @app, 'StringOf', @clientId OUT, 'client_id'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://cognito-idp.'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com/'
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'domain'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid domain.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        RETURN
      END
    --  This URL comes from trusted configuration, never from a token or user-supplied URL.
    DECLARE @tokenEndpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @tokenEndpoint OUT

    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
    EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, '/oauth2/token'
    DECLARE @oauth int
    EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT

    DECLARE @authorizeEndpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @authorizeEndpoint OUT

    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
    EXEC sp_OAMethod @authorizeEndpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @authorizeEndpoint, 'Append', @success OUT, '/oauth2/authorize'
    EXEC sp_OAMethod @authorizeEndpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OASetProperty @oauth, 'AuthorizationEndpoint', @sTmp0
    EXEC sp_OAMethod @tokenEndpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OASetProperty @oauth, 'TokenEndpoint', @sTmp0
    EXEC sp_OASetProperty @oauth, 'ClientId', @clientId
    --  Register exactly http://localhost:3017/ as an allowed callback for this public client.
    EXEC sp_OASetProperty @oauth, 'LocalHost', 'localhost'
    EXEC sp_OASetProperty @oauth, 'ListenPort', 3017
    EXEC sp_OASetProperty @oauth, 'CodeChallenge', 1
    EXEC sp_OASetProperty @oauth, 'CodeChallengeMethod', 'S256'
    --  Cognito does NOT use an offline_access scope. Code flow returns a refresh token.
    EXEC sp_OASetProperty @oauth, 'Scope', 'openid email aws.cognito.signin.user.admin'
    DECLARE @authUrl nvarchar(4000)
    EXEC sp_OAMethod @oauth, 'StartAuth', @authUrl OUT
    EXEC sp_OAGetProperty @oauth, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Could not start the local OAuth listener.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        RETURN
      END
    EXEC sp_OAMethod @oauth, 'LaunchBrowser', @success OUT, @authUrl
    IF @success <> 1
      BEGIN
        EXEC sp_OAMethod @oauth, 'Cancel', @success OUT

        PRINT 'Could not launch the system browser.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        RETURN
      END
    DECLARE @waited int
    SELECT @waited = 0
    EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
    WHILE ((@iTmp0 < 3) and (@waited < 180000))
      BEGIN
        EXEC sp_OAMethod @oauth, 'SleepMs', NULL, 200
        SELECT @waited = @waited + 200
      END
    EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
    IF @iTmp0 < 3
      BEGIN
        EXEC sp_OAMethod @oauth, 'Cancel', @success OUT

        PRINT 'Authorization timed out.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        RETURN
      END
    EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
    IF @iTmp0 <> 3
      BEGIN

        PRINT 'Authorization did not complete.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        RETURN
      END
    DECLARE @token int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT

    EXEC sp_OASetProperty @token, 'Strict', 1
    EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
    EXEC sp_OAMethod @token, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not parse the token response.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid access_token.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'id_token'
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'id_token'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid id_token.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'expires_in'
    EXEC sp_OAMethod @token, 'IntOf', @iTmp1 OUT, 'expires_in'
    IF (@iTmp0 <> 2) or (@iTmp1 <= 0)
      BEGIN

        PRINT 'Expected a positive token lifetime.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'token_type'
    IF @sTmp0 <> 'Bearer'
      BEGIN

        PRINT 'Expected Bearer tokens.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'refresh_token'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid refresh_token.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'client_id', @sTmp0
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
    EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'user_pool_id', @sTmp0
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
    EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'account_key', @sTmp0
    DECLARE @obtained int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @obtained OUT

    EXEC sp_OAMethod @obtained, 'SetFromCurrentSystemTime', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Could not get the acquisition time.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @obtained
        RETURN
      END
    EXEC sp_OAMethod @obtained, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'obtained_at', @sTmp0
    DECLARE @tokenusertokens int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @tokenusertokens OUT

    EXEC sp_OAMethod @tokenusertokens, 'DirEnsureExists', @success OUT, 'qa_data/tokens'
    IF @success <> 1
      BEGIN

        PRINT 'Could not create the private cache directory.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @obtained
        EXEC @hr = sp_OADestroy @tokenusertokens
        RETURN
      END
    EXEC sp_OAMethod @tokenusertokens, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/cognito-user.json'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @tokenusertokens, 'FileCopy', @success OUT, 'qa_data/tokens/cognito-user.json', 'qa_data/tokens/cognito-user.json.bak', 0
        IF @success <> 1
          BEGIN

            PRINT 'Could not back up the existing cache.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @tokenEndpoint
            EXEC @hr = sp_OADestroy @oauth
            EXEC @hr = sp_OADestroy @authorizeEndpoint
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @obtained
            EXEC @hr = sp_OADestroy @tokenusertokens
            RETURN
          END
      END
    EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'qa_data/tokens/cognito-user.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not save user tokens; preserve any previous backup.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @authorizeEndpoint
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @obtained
        EXEC @hr = sp_OADestroy @tokenusertokens
        RETURN
      END

    PRINT 'Authorization completed; private token cache saved.'
    --  Validate JWT signatures and claims before trusting their contents:
    --  Validate a Cognito JWT.

    EXEC @hr = sp_OADestroy @app
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @tokenEndpoint
    EXEC @hr = sp_OADestroy @oauth
    EXEC @hr = sp_OADestroy @authorizeEndpoint
    EXEC @hr = sp_OADestroy @token
    EXEC @hr = sp_OADestroy @obtained
    EXEC @hr = sp_OADestroy @tokenusertokens


END
GO