Amazon Cognito: Revoke Tokens or Sign Out a User
See more Amazon Cognito Examples
Choose revoke to revoke the cached refresh token and its token family through /oauth2/revoke, or global to call GlobalSignOut with the user's access token (aws.cognito.signin.user.admin scope). Enable token revocation on the app client. A public client sends client_id with the refresh token; a confidential OAuth client must use its documented client authentication. Revocation returns HTTP 200 even for an already revoked/invalid token and can have an empty body.
GlobalSignOut affects the user's sessions on other devices. Token revocation, clearing this app's private cache, and clearing managed-login browser cookies are distinct actions. To end the browser session, register an allowed sign-out URL as logout_uri in app.json and navigate to the displayed /logout URL in the sign-in browser. An upstream social/OIDC provider may retain its own session. Offline JWT signature validation alone does not discover revoked tokens.
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 | AWS documentation 3
Chilkat SQL Server Downloads
-- 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 @sTmp0 nvarchar(4000)
DECLARE @sTmp1 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/'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 60
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
-- "revoke" ends the selected refresh-token family; "global" signs out all this user's sessions.
DECLARE @action nvarchar(4000)
SELECT @action = 'revoke'
IF (@action <> 'revoke') and (@action <> 'global')
BEGIN
PRINT 'Choose revoke or global.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @cached int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @cached OUT
EXEC sp_OASetProperty @cached, 'Strict', 1
EXEC sp_OAMethod @cached, 'LoadFile', @success OUT, 'qa_data/tokens/cognito-user.json'
IF @success <> 1
BEGIN
PRINT 'Could not load qa_data/tokens/cognito-user.json.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'client_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid client_id.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'client_id'
IF @sTmp0 <> @sTmp1
BEGIN
PRINT 'Saved data belongs to another client, pool, or local account.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'user_pool_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid user_pool_id.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'user_pool_id'
IF @sTmp0 <> @sTmp1
BEGIN
PRINT 'Saved data belongs to another client, pool, or local account.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'account_key'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'account_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid account_key.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'account_key'
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'account_key'
IF @sTmp0 <> @sTmp1
BEGIN
PRINT 'Saved data belongs to another client, pool, or local account.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
IF @action = 'revoke'
BEGIN
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @cached, '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 @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
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
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
DECLARE @revokeUrl int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @revokeUrl OUT
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
EXEC sp_OAMethod @revokeUrl, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @revokeUrl, 'Append', @success OUT, '/oauth2/revoke'
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', @clientId
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'token', @sTmp0
EXEC sp_OAMethod @revokeUrl, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'Error type: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
PRINT 'OAuth error: ' + @sTmp0
END
-- Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP exchange did not complete.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
RETURN
END
END
ELSE
BEGIN
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @cached, '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 @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'AccessToken', @sTmp0
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.GlobalSignOut'
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'Error type: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
PRINT 'OAuth error: ' + @sTmp0
END
-- Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP exchange did not complete.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
RETURN
END
END
-- Remove this application's cache only after a successful server response.
DECLARE @cleanup int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @cleanup OUT
EXEC sp_OAMethod @cleanup, 'FileDelete', @success OUT, 'qa_data/tokens/cognito-user.json'
IF @success <> 1
BEGIN
PRINT 'Server sign-out succeeded, but local token-cache deletion failed.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @cleanup
RETURN
END
EXEC sp_OAMethod @cleanup, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/cognito-user.json.bak'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @cleanup, 'FileDelete', @success OUT, 'qa_data/tokens/cognito-user.json.bak'
IF @success <> 1
BEGIN
PRINT 'Sign-out succeeded, but private token-backup cleanup failed.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @cleanup
RETURN
END
END
PRINT 'Sign-out request accepted; local user tokens removed.'
-- Managed-login cookies require a separate system-browser navigation to /logout.
-- Configure logout_uri as an allowed sign-out URL on the app client.
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'logout_uri'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'logout_uri'
IF (@iTmp0 = 1) and (@sTmp0 <> '')
BEGIN
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
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @cleanup
RETURN
END
DECLARE @logoutUrl int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @logoutUrl OUT
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
EXEC sp_OAMethod @logoutUrl, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @logoutUrl, 'Append', @success OUT, '/logout?client_id='
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @clientId
EXEC sp_OAMethod @logoutUrl, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @logoutUrl, 'Append', @success OUT, '&logout_uri='
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'logout_uri'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @sTmp1
EXEC sp_OAMethod @logoutUrl, 'Append', @success OUT, @sTmp0
-- This public URL has no token. Navigate to it in the same system browser used for sign-in.
EXEC sp_OAMethod @logoutUrl, 'GetAsString', @sTmp0 OUT
PRINT 'Browser logout URL: ' + @sTmp0
END
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @revokeUrl
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @cleanup
EXEC @hr = sp_OADestroy @logoutUrl
END
GO