Sub topic 17
Monitoring OAuth usage and managing sessions
This chapter assumes you can use Setup, run curl, and run basic SOQL queries. It also assumes you know what access tokens and refresh tokens are from the earlier flow chapters. Here you will see who is using your apps, how to end that access, and what happens to tokens that were already issued.
Where to look
Each place answers a different question.
| Tool | Question it answers |
|---|---|
| Connected Apps OAuth Usage (Setup) | Which apps hold tokens, how many users, how often used |
| Session Management (Setup) | Which sessions are active now, and which came from OAuth |
| Login History (Setup) | When and how each user authenticated |
| `OauthToken` object (SOQL) | Which user has which token, with use count and last used date |
The names of these pages can vary slightly by release. Check how your org labels them, especially for external client apps (ECAs).
Review usage
Open Connected Apps OAuth Usage and sort by user count and use count. Look for three things:
- Apps nobody recognizes.
- Apps with many users but no owner on your team.
- Apps with high use counts from a single user. This is often an integration user, so confirm it is expected.
From this page you can Block an app, which stops new tokens from being issued. You can also Revoke the tokens it already holds.
Query tokens with SOQL
The OauthToken object lets you audit tokens in bulk. This example uses the ci-org alias from the JWT chapter.
sf data query \
--target-org ci-org \
--query "SELECT AppName, UserId, UseCount, LastUsedDate FROM OauthToken ORDER BY LastUsedDate ASC"Tokens with an old LastUsedDate are good candidates for cleanup.
Revoke access
You can revoke access at several levels. They differ in how much they affect.
flowchart TDUse the diagram below to pick the level.
A client can also revoke its own token through the revoke endpoint:
curl -X POST https://MyDomain.my.salesforce.com/services/oauth2/revoke \
-d token="$REFRESH_TOKEN"Revoking a refresh token also invalidates the access tokens issued from it. Revoking only an access token leaves the refresh token working, so the client can get a new access token.
Sessions and timing
An access token maps to a session, and the session timeout in your org or app policy controls its lifetime. Two points matter:
- Deleting a session in Session Management ends it immediately. Use this when you need access to stop now.
- Changing a policy, such as permitted users or IP relaxation, applies to new token requests. Confirm in a test that already-issued tokens behave the way you expect before you rely on it.
Build a routine
- Review OAuth usage monthly.
- Revoke tokens that have not been used for a long time.
- Review integration users after staff changes.
- After any incident, rotate the consumer secret or certificate, then revoke tokens.
Exercise
A refresh token for a contractor's app may have leaked. You want the app to stop working for that user without affecting other users. What do you do, and why check Session Management afterward?
Show answer
Revoke that user's token for the app, either in the usage page or by deleting the OauthToken record. Revoking the refresh token also invalidates the access tokens issued from it. Then check Session Management because a session that is still active can keep working until it ends, so you may need to end it manually.