Sub topic 16
Revoking Tokens and Ending Sessions
This chapter assumes you have completed the web server flow and refresh token chapters. You should know what an access token and a refresh token are, and you should be able to call the token endpoint with curl.
Why revocation matters
An access token works until it expires or is revoked. A refresh token lasts much longer and can mint new access tokens. If either one leaks, the holder can call the API as that user until you cut it off. Revocation is how you cut it off.
Revoking a refresh token also invalidates the access tokens issued from it. Revoking only an access token leaves the refresh token alive, so the app can simply request a new access token.
Revoking a token with the API
Salesforce exposes a revocation endpoint on the same host as the token endpoint. Post the token you want to revoke:
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/revoke \
-d token=<REFRESH_TOKEN>A successful call returns HTTP 200 with an empty body. Pass the refresh token when you want to end the whole grant, for example when a user clicks "Disconnect" in your app. Pass an access token when you only want to drop one session.
Keep these points in mind:
- Revocation is not an error if the token was already invalid. Treat a repeated call as safe.
- Delete the token from your own storage as well. Revoking on the Salesforce side does not clear your database.
- After revocation, a refresh request with that token fails with
invalid_grant.
Revoking from the Salesforce side
Administrators and users can end access without any help from your app.
- A user opens their personal settings and finds the OAuth Connected Apps list. Choosing Revoke removes that app's tokens for that user.
- An administrator opens Setup, then Connected Apps OAuth Usage. From there they can revoke tokens for one user, or block the app altogether.
- An administrator can also end an active session from the Session Management page in Setup.
Ending a session invalidates access tokens tied to that session. It does not always remove the refresh token, so the app may be able to get a new access token. To stop an app for good, revoke the OAuth grant, not just the session.
Which flow leaves what behind
| Flow | Refresh token | How to end access |
|---|---|---|
| Web server | Yes, if the `refresh_token` scope was granted | Revoke the refresh token, or revoke the user's grant |
| JWT bearer | No | Let the token expire, end the session, or remove the user's pre-authorization |
| Client credentials | No | End the session, or change the run-as user or the connected app |
For the JWT bearer flow, no refresh token exists, so the app simply signs a new assertion when it needs a token. To stop it, remove the user's profile or permission set from the connected app's pre-authorization, or delete the uploaded certificate.
Exercise
A user clicks "Disconnect" in your web server flow app. You revoke only the access token. Why can the app still reach Salesforce, and what should you have revoked?
Show answer
The refresh token is still valid, so the app can exchange it for a new access token. You should have revoked the refresh token, which also invalidates the access tokens issued from it, and deleted both tokens from your own storage.