Sub topic 16
Revoking tokens and ending sessions
This chapter assumes you can already get an access token with at least one of the three flows (web server, JWT bearer, client credentials) and can call the REST API with it. You should know what a refresh token is and where a connected app's policies live in Setup.
Issuing tokens is half of the job. You also need to end access when a user logs out, a device is lost, a secret leaks, or an integration is retired. Salesforce gives you several ways to do this, and what each one removes depends on the flow.
What revocation removes
Salesforce tokens fall into two groups with different lifetimes.
- An access token is tied to a session. It expires when the session times out.
- A refresh token is long-lived. It can mint new access tokens until it expires, is revoked, or the user's authorization is removed.
Revoking a refresh token invalidates that refresh token and the access tokens issued under that authorization. Revoking an access token ends that session only. A refresh token can still obtain a new access token afterward.
For this reason, a client that wants to fully sign a user out revokes the refresh token when it has one.
| Flow | Refresh token issued? | What to revoke |
|---|---|---|
| Web server | Yes, if `refresh_token` scope was granted | The refresh token |
| JWT bearer | No | The access token, or remove the user's pre-authorization |
| Client credentials | No | The access token, or rotate the consumer secret |
Revoking from the client
The revocation endpoint is /services/oauth2/revoke on the login host or your My Domain. You post the token as a form parameter. No client secret is needed, because possession of the token is the credential.
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/revoke \
-d token=<REFRESH_TOKEN>A successful revocation returns HTTP 200 with an empty body. If the request is malformed or the token type is not supported, the response is HTTP 400 with an error such as unsupported_token_type.
The same call works for an access token. Pass the access token as token, and that session ends immediately.
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/revoke \
-d token=<ACCESS_TOKEN>Here is a small helper that revokes a token and confirms the result.
import requests
MY_DOMAIN = "https://MyDomainName.my.salesforce.com"
def revoke(token: str) -> None:
"""Revoke an access or refresh token. Raises on a non-2xx response."""
resp = requests.post(
f"{MY_DOMAIN}/services/oauth2/revoke",
data={"token": token},
timeout=10,
)
resp.raise_for_status()
if __name__ == "__main__":
revoke("<REFRESH_TOKEN>")
print("revoked")Use the same host that issued the token. A token from a sandbox must be revoked against https://test.salesforce.com or the sandbox's My Domain, not the production login host.
Checking whether a token is still active
After revoking, you can confirm the state with the token introspection endpoint. It takes the connected app's credentials along with the token.
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/introspect \
-d token=<ACCESS_TOKEN> \
-d token_type_hint=access_token \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET>The JSON response contains "active": true for a live token and "active": false for a revoked or expired one. Introspection is useful in tests and in a service that receives tokens from other callers.
If your connected app does not allow introspection, the call fails. Check that the app is configured for it before relying on this in a pipeline.
Revoking from the Salesforce side
Not every revocation starts with the client. Administrators and users can end access without the client's cooperation.
- A user can open their personal settings, find the connected apps or OAuth usage list, and revoke an app. This removes that user's authorization for the app.
- An administrator can open Connected Apps OAuth Usage in Setup. It lists each app with its user count and token count. The administrator can revoke one user's tokens, revoke all tokens for the app, or block the app.
- An administrator can end individual user sessions from the Session Management page.
Blocking an app is stronger than revoking its tokens. Revocation lets the same client request a new authorization. Blocking stops new authorizations until the block is lifted.
The diagram below shows who can end access and what each action stops.
Policies that end access automatically
You can limit how long tokens live by setting policies on the connected app, so that revocation is not the only control.
- Refresh Token Policy sets how long a refresh token stays valid. The options are valid until revoked, expire immediately (so no durable refresh token is kept), expire if not used for a period, or expire after a fixed period.
- Session Timeout sets how long an access token's session lasts without activity.
- IP Relaxation and Permitted Users decide whether a client from an unexpected address or an unapproved user is accepted at all.
For a high-risk integration, choose "expire if not used" or a fixed period. A forgotten refresh token then stops working without anyone acting.
Revoking access for server-to-server flows
The JWT bearer and client credentials flows do not issue refresh tokens, so there is nothing long-lived to revoke on the client. Access is controlled by the configuration that permits the request.
For the JWT bearer flow, any of these stops new tokens from being issued:
- Remove the user's profile or permission set from the connected app's pre-authorization.
- Deactivate or freeze the integration user.
- Delete the certificate from the connected app, or replace it with a new one.
For the client credentials flow, stop new tokens by rotating the consumer secret, changing the run-as user, or deactivating the run-as user.
Each of these stops new tokens. A token already issued stays valid until its session ends. To cut off access immediately, also revoke the access token or end the session in Session Management.
Common mistakes
- Revoking only the access token and expecting a full logout. The refresh token remains. Revoke the refresh token as well.
- Using the wrong host. A revoke call sent to the production login host will not find a sandbox token.
- Logging tokens before revoking them. A token in a log file is a credential. Remove it from logs and rotate it if it leaked.
- Treating a 200 as proof the client is clean. The client may still hold a copy in a cache or database. Delete stored tokens when you revoke them.
- Rotating a secret without deploying it. Rotating the consumer secret breaks every client that still uses the old one. Plan the order of changes.
Exercise
A web server flow app signs a user out by calling the revoke endpoint with the user's access token. An hour later the app uses the stored refresh token and receives a new, working access token. Why did this happen, and what should the app do?
Show answer
Revoking an access token ends only that session. The refresh token was not revoked, so it could still mint a new access token. On logout the app should revoke the refresh token, which also invalidates the access tokens issued under that authorization, and then delete both tokens from its storage.
Exercise
An integration uses the JWT bearer flow, and its private key has leaked. List the steps that stop an attacker from getting new tokens, and name what still needs separate action.
Show answer
Replace the certificate on the connected app, or remove the pre-authorization for the integration user, so JWT assertions signed with the old key are refused. Create a new key pair and certificate for the legitimate integration. Tokens already issued remain valid until their sessions end, so revoke active access tokens or end the user's sessions in Session Management.