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 VB.NET Downloads
Dim success As Boolean = 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.
Dim app As New Chilkat.JsonObject
app.Strict = True
success = app.LoadFile("qa_data/cognito/app.json")
If (success <> True) Then
Debug.WriteLine("Could not load Cognito app settings.")
Exit Sub
End If
If ((app.JsonTypeOf("region") <> 1) Or (app.StringOf("region") = "")) Then
Debug.WriteLine("Missing or invalid region.")
Exit Sub
End If
If ((app.JsonTypeOf("user_pool_id") <> 1) Or (app.StringOf("user_pool_id") = "")) Then
Debug.WriteLine("Missing or invalid user_pool_id.")
Exit Sub
End If
If ((app.JsonTypeOf("client_id") <> 1) Or (app.StringOf("client_id") = "")) Then
Debug.WriteLine("Missing or invalid client_id.")
Exit Sub
End If
If ((app.JsonTypeOf("account_key") <> 1) Or (app.StringOf("account_key") = "")) Then
Debug.WriteLine("Missing or invalid account_key.")
Exit Sub
End If
Dim clientId As String = app.StringOf("client_id")
Dim endpoint As New Chilkat.StringBuilder
endpoint.Append("https://cognito-idp.")
endpoint.Append(app.StringOf("region"))
endpoint.Append(".amazonaws.com/")
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 20
http.ReadTimeout = 60
Dim resp As New Chilkat.HttpResponse
Dim body As New Chilkat.JsonObject
Dim result As New Chilkat.JsonObject
result.Strict = True
' "revoke" ends the selected refresh-token family; "global" signs out all this user's sessions.
Dim action As String = "revoke"
If ((action <> "revoke") And (action <> "global")) Then
Debug.WriteLine("Choose revoke or global.")
Exit Sub
End If
Dim cached As New Chilkat.JsonObject
cached.Strict = True
success = cached.LoadFile("qa_data/tokens/cognito-user.json")
If (success <> True) Then
Debug.WriteLine("Could not load qa_data/tokens/cognito-user.json.")
Exit Sub
End If
If ((cached.JsonTypeOf("client_id") <> 1) Or (cached.StringOf("client_id") = "")) Then
Debug.WriteLine("Missing or invalid client_id.")
Exit Sub
End If
If (cached.StringOf("client_id") <> app.StringOf("client_id")) Then
Debug.WriteLine("Saved data belongs to another client, pool, or local account.")
Exit Sub
End If
If ((cached.JsonTypeOf("user_pool_id") <> 1) Or (cached.StringOf("user_pool_id") = "")) Then
Debug.WriteLine("Missing or invalid user_pool_id.")
Exit Sub
End If
If (cached.StringOf("user_pool_id") <> app.StringOf("user_pool_id")) Then
Debug.WriteLine("Saved data belongs to another client, pool, or local account.")
Exit Sub
End If
If ((cached.JsonTypeOf("account_key") <> 1) Or (cached.StringOf("account_key") = "")) Then
Debug.WriteLine("Missing or invalid account_key.")
Exit Sub
End If
If (cached.StringOf("account_key") <> app.StringOf("account_key")) Then
Debug.WriteLine("Saved data belongs to another client, pool, or local account.")
Exit Sub
End If
If (action = "revoke") Then
If ((cached.JsonTypeOf("refresh_token") <> 1) Or (cached.StringOf("refresh_token") = "")) Then
Debug.WriteLine("Missing or invalid refresh_token.")
Exit Sub
End If
If ((app.JsonTypeOf("domain") <> 1) Or (app.StringOf("domain") = "")) Then
Debug.WriteLine("Missing or invalid domain.")
Exit Sub
End If
Dim revokeUrl As New Chilkat.StringBuilder
revokeUrl.Append(app.StringOf("domain"))
revokeUrl.Append("/oauth2/revoke")
Dim req As New 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
Debug.WriteLine("HTTP status: " & resp.StatusCode)
success = result.Load(resp.BodyStr)
If (success = True) Then
Debug.WriteLine("Error type: " & result.StringOf("__type"))
Debug.WriteLine("OAuth error: " & result.StringOf("error"))
End If
' Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
Exit Sub
End If
If (success <> True) Then
Debug.WriteLine("The HTTP exchange did not complete.")
Exit Sub
End If
Else
If ((cached.JsonTypeOf("access_token") <> 1) Or (cached.StringOf("access_token") = "")) Then
Debug.WriteLine("Missing or invalid access_token.")
Exit Sub
End If
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
Debug.WriteLine("HTTP status: " & resp.StatusCode)
success = result.Load(resp.BodyStr)
If (success = True) Then
Debug.WriteLine("Error type: " & result.StringOf("__type"))
Debug.WriteLine("OAuth error: " & result.StringOf("error"))
End If
' Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
Exit Sub
End If
If (success <> True) Then
Debug.WriteLine("The HTTP exchange did not complete.")
Exit Sub
End If
End If
' Remove this application's cache only after a successful server response.
Dim cleanup As New Chilkat.FileAccess
success = cleanup.FileDelete("qa_data/tokens/cognito-user.json")
If (success <> True) Then
Debug.WriteLine("Server sign-out succeeded, but local token-cache deletion failed.")
Exit Sub
End If
If (cleanup.FileExists("qa_data/tokens/cognito-user.json.bak") = True) Then
success = cleanup.FileDelete("qa_data/tokens/cognito-user.json.bak")
If (success <> True) Then
Debug.WriteLine("Sign-out succeeded, but private token-backup cleanup failed.")
Exit Sub
End If
End If
Debug.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
Debug.WriteLine("Missing or invalid domain.")
Exit Sub
End If
Dim logoutUrl As New Chilkat.StringBuilder
logoutUrl.Append(app.StringOf("domain"))
logoutUrl.Append("/logout?client_id=")
logoutUrl.Append(http.UrlEncode(clientId))
logoutUrl.Append("&logout_uri=")
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.
Debug.WriteLine("Browser logout URL: " & logoutUrl.GetAsString())
End If