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

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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 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.
set app = CreateObject("Chilkat.JsonObject")
app.Strict = 1
success = app.LoadFile("qa_data/cognito/app.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load Cognito app settings.")
    WScript.Quit
End If

If ((app.JsonTypeOf("region") <> 1) Or (app.StringOf("region") = "")) Then
    outFile.WriteLine("Missing or invalid region.")
    WScript.Quit
End If

If ((app.JsonTypeOf("user_pool_id") <> 1) Or (app.StringOf("user_pool_id") = "")) Then
    outFile.WriteLine("Missing or invalid user_pool_id.")
    WScript.Quit
End If

If ((app.JsonTypeOf("client_id") <> 1) Or (app.StringOf("client_id") = "")) Then
    outFile.WriteLine("Missing or invalid client_id.")
    WScript.Quit
End If

If ((app.JsonTypeOf("account_key") <> 1) Or (app.StringOf("account_key") = "")) Then
    outFile.WriteLine("Missing or invalid account_key.")
    WScript.Quit
End If

clientId = app.StringOf("client_id")
set endpoint = CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("https://cognito-idp.")
success = endpoint.Append(app.StringOf("region"))
success = endpoint.Append(".amazonaws.com/")
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 60
set resp = CreateObject("Chilkat.HttpResponse")
set body = CreateObject("Chilkat.JsonObject")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
'  "revoke" ends the selected refresh-token family; "global" signs out all this user's sessions.
action = "revoke"
If ((action <> "revoke") And (action <> "global")) Then
    outFile.WriteLine("Choose revoke or global.")
    WScript.Quit
End If

set cached = CreateObject("Chilkat.JsonObject")
cached.Strict = 1
success = cached.LoadFile("qa_data/tokens/cognito-user.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load qa_data/tokens/cognito-user.json.")
    WScript.Quit
End If

If ((cached.JsonTypeOf("client_id") <> 1) Or (cached.StringOf("client_id") = "")) Then
    outFile.WriteLine("Missing or invalid client_id.")
    WScript.Quit
End If

If (cached.StringOf("client_id") <> app.StringOf("client_id")) Then
    outFile.WriteLine("Saved data belongs to another client, pool, or local account.")
    WScript.Quit
End If

If ((cached.JsonTypeOf("user_pool_id") <> 1) Or (cached.StringOf("user_pool_id") = "")) Then
    outFile.WriteLine("Missing or invalid user_pool_id.")
    WScript.Quit
End If

If (cached.StringOf("user_pool_id") <> app.StringOf("user_pool_id")) Then
    outFile.WriteLine("Saved data belongs to another client, pool, or local account.")
    WScript.Quit
End If

If ((cached.JsonTypeOf("account_key") <> 1) Or (cached.StringOf("account_key") = "")) Then
    outFile.WriteLine("Missing or invalid account_key.")
    WScript.Quit
End If

If (cached.StringOf("account_key") <> app.StringOf("account_key")) Then
    outFile.WriteLine("Saved data belongs to another client, pool, or local account.")
    WScript.Quit
End If

If (action = "revoke") Then
    If ((cached.JsonTypeOf("refresh_token") <> 1) Or (cached.StringOf("refresh_token") = "")) Then
        outFile.WriteLine("Missing or invalid refresh_token.")
        WScript.Quit
    End If

    If ((app.JsonTypeOf("domain") <> 1) Or (app.StringOf("domain") = "")) Then
        outFile.WriteLine("Missing or invalid domain.")
        WScript.Quit
    End If

    set revokeUrl = CreateObject("Chilkat.StringBuilder")
    success = revokeUrl.Append(app.StringOf("domain"))
    success = revokeUrl.Append("/oauth2/revoke")
    set req = CreateObject("Chilkat.HttpRequest")
    req.ContentType = "application/x-www-form-urlencoded"
    req.HttpVerb = "POST"
    req.AddParam "client_id",clientId
    req.AddParam "token",cached.StringOf("refresh_token")
    success = http.HttpReq(revokeUrl.GetAsString(),req,resp)
    If (resp.StatusCode <> 200) Then
        outFile.WriteLine("HTTP status: " & resp.StatusCode)
        success = result.Load(resp.BodyStr)
        If (success = 1) Then
            outFile.WriteLine("Error type: " & result.StringOf("__type"))
            outFile.WriteLine("OAuth error: " & result.StringOf("error"))
        End If

        '  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
        WScript.Quit
    End If

    If (success <> 1) Then
        outFile.WriteLine("The HTTP exchange did not complete.")
        WScript.Quit
    End If

Else
    If ((cached.JsonTypeOf("access_token") <> 1) Or (cached.StringOf("access_token") = "")) Then
        outFile.WriteLine("Missing or invalid access_token.")
        WScript.Quit
    End If

    success = body.UpdateString("AccessToken",cached.StringOf("access_token"))
    http.SetRequestHeader "X-Amz-Target","AWSCognitoIdentityProviderService.GlobalSignOut"
    success = http.HttpJson("POST",endpoint.GetAsString(),body,"application/x-amz-json-1.0",resp)
    If (resp.StatusCode <> 200) Then
        outFile.WriteLine("HTTP status: " & resp.StatusCode)
        success = result.Load(resp.BodyStr)
        If (success = 1) Then
            outFile.WriteLine("Error type: " & result.StringOf("__type"))
            outFile.WriteLine("OAuth error: " & result.StringOf("error"))
        End If

        '  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
        WScript.Quit
    End If

    If (success <> 1) Then
        outFile.WriteLine("The HTTP exchange did not complete.")
        WScript.Quit
    End If

End If

'  Remove this application's cache only after a successful server response.
set cleanup = CreateObject("Chilkat.FileAccess")
success = cleanup.FileDelete("qa_data/tokens/cognito-user.json")
If (success <> 1) Then
    outFile.WriteLine("Server sign-out succeeded, but local token-cache deletion failed.")
    WScript.Quit
End If

If (cleanup.FileExists("qa_data/tokens/cognito-user.json.bak") = 1) Then
    success = cleanup.FileDelete("qa_data/tokens/cognito-user.json.bak")
    If (success <> 1) Then
        outFile.WriteLine("Sign-out succeeded, but private token-backup cleanup failed.")
        WScript.Quit
    End If

End If

outFile.WriteLine("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.
If ((app.JsonTypeOf("logout_uri") = 1) And (app.StringOf("logout_uri") <> "")) Then
    If ((app.JsonTypeOf("domain") <> 1) Or (app.StringOf("domain") = "")) Then
        outFile.WriteLine("Missing or invalid domain.")
        WScript.Quit
    End If

    set logoutUrl = CreateObject("Chilkat.StringBuilder")
    success = logoutUrl.Append(app.StringOf("domain"))
    success = logoutUrl.Append("/logout?client_id=")
    success = logoutUrl.Append(http.UrlEncode(clientId))
    success = logoutUrl.Append("&logout_uri=")
    success = logoutUrl.Append(http.UrlEncode(app.StringOf("logout_uri")))
    '  This public URL has no token. Navigate to it in the same system browser used for sign-in.
    outFile.WriteLine("Browser logout URL: " & logoutUrl.GetAsString())
End If


outFile.Close