You build or run an app that signs in to Microsoft Entra ID with a client secret. This lab answers one question: after a leak, what actually stops a token someone already took?

The answer: revoking the secret does not. It stops new tokens, and the ones already issued keep reading data from Microsoft Graph. In my runs, disabling the app's service principal stopped them within about two minutes.

Setup

You need a throwaway tenant, the Azure command-line interface (CLI), curl and jq. Never use a work or production tenant. Sign in with az login --tenant "$TENANT_ID" --allow-no-subscriptions and set two variables:

export TENANT_ID=<TENANT_ID> export APP_NAME=lab-revoke-token-test 

Create the app and its service principal:

export APP_ID=$(az ad app create --display-name "$APP_NAME" --sign-in-audience AzureADMyOrg --query appId -o tsv) az ad sp create --id "$APP_ID" --query id -o tsv echo "$APP_ID" 

The app needs one permission for the test to mean anything: User.Read.All, an application permission on Microsoft Graph.

I assigned the role directly to the service principal, because admin consent through the CLI left my tokens without it:

GRAPH_ID=$(az ad sp list --display-name "Microsoft Graph" --query "[0].appId" -o tsv) READ_ROLE=$(az ad sp show --id "$GRAPH_ID" --query "appRoles[?value=='User.Read.All'].id | [0]" -o tsv) az ad app permission add --id "$APP_ID" --api "$GRAPH_ID" --api-permissions "$READ_ROLE=Role" APP_SP=$(az ad sp show --id "$APP_ID" --query id -o tsv) GRAPH_SP=$(az ad sp show --id "$GRAPH_ID" --query id -o tsv) az rest --method post \ --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APP_SP/appRoleAssignments" \ --headers "Content-Type=application/json" \ --body "{\"principalId\":\"$APP_SP\",\"resourceId\":\"$GRAPH_SP\",\"appRoleId\":\"$READ_ROLE\"}" az ad app permission list --id "$APP_ID" --query "[].resourceAccess[].type" -o tsv sleep 60 

Then create a client secret that expires in seven days, written to a file only your user can read.

The date command is macOS syntax; on Linux use date -u -d "+7 days" +%Y-%m-%d:

umask 077 END=$(date -u -v+7d +%Y-%m-%d) az ad app credential reset --id "$APP_ID" \ --append --display-name lab --end-date "$END" \ --query password -o tsv > secret.value printf 'ENTRA_APP_SECRET=%s\n' "$(cat secret.value)" > secret.env rm -f secret.value source ./secret.env 

Get a token

The client credentials flow needs no user sign-in. The token stays in a shell variable, and only a few claims are printed:

TOKEN=$(curl -s -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \ --data-urlencode grant_type=client_credentials \ --data-urlencode client_id="$APP_ID" \ --data-urlencode "client_secret=$ENTRA_APP_SECRET" \ --data-urlencode scope=https://graph.microsoft.com/.default | jq -r .access_token) echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | jq -R '@base64d | fromjson | {aud, idtyp, roles, lifetime_seconds: (.exp - .iat)}' 

The token. App-only, one read role. The lifetime counted from iat includes five minutes of clock allowance.

A token that lasts a day

An app can tell Entra that it handles CAE, by sending the cp1 client capability. The same secret then buys a token that lasts about 24 hours. Whoever holds the secret decides whether to ask for it.

curl -s -o long.json -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \ --data-urlencode grant_type=client_credentials \ --data-urlencode client_id="$APP_ID" \ --data-urlencode "client_secret=$ENTRA_APP_SECRET" \ --data-urlencode scope=https://graph.microsoft.com/.default \ --data-urlencode 'claims={"access_token":{"xms_cc":{"values":["cp1"]}}}' jq '{expires_in}' long.json LONG_TOKEN=$(jq -r .access_token long.json); rm -f long.json 

Read users before the revoke

I called Graph with both tokens. A small function prints the status and the number of users, so no names or IDs leave the terminal:

users() { curl -s -o resp.json -w "$1 HTTP %{http_code} " \ -H "Authorization: Bearer $2" \ "https://graph.microsoft.com/v1.0/users?\$top=5&\$select=id" jq -r '.error.code // "users: \(.value | length)"' resp.json; } users short "$TOKEN" users long "$LONG_TOKEN" 

Both returned HTTP 200 and five users.

Revoke the secret

This is the step a team takes after a leak:

KEY_ID=$(az ad app show --id "$APP_ID" --query "passwordCredentials[0].keyId" -o tsv) az ad app credential delete --id "$APP_ID" --key-id "$KEY_ID" echo "secrets left: $(az ad app show --id "$APP_ID" --query "length(passwordCredentials)" -o tsv)" 

After the revoke

First a new token request with the old secret, then both old tokens:

curl -s -o new.json -w "HTTP %{http_code}\n" -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \ --data-urlencode grant_type=client_credentials \ --data-urlencode client_id="$APP_ID" \ --data-urlencode "client_secret=$ENTRA_APP_SECRET" \ --data-urlencode scope=https://graph.microsoft.com/.defaultjq -r '"error=\(.error)"' new.jsonusers short "$TOKEN"users long "$LONG_TOKEN" 

The revoke. No new tokens, but both old ones still work.

The new request got HTTP 401 with invalid_client. Both old tokens got HTTP 200.

Disable the service principal

Disabling sign-in for the service principal is a different switch. I set it and read users with both tokens every 30 seconds:

APP_SP=$(az ad sp show --id "$APP_ID" --query id -o tsv) az rest --method patch --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APP_SP" \ --headers "Content-Type=application/json" --body '{"accountEnabled":false}' for i in $(seq 1 10); do date -u +%H:%M:%S users short "$TOKEN" users long "$LONG_TOKEN" sleep 30 done 

The cut-off. The ordinary token was refused after about one minute, the long-lived one after about two.

Graph refused the ordinary token after about one minute and the long-lived token after about two, with Authorization_IdentityDisabled. An earlier run refused both after about 90 seconds. Microsoft documents this revocation for CAE tokens on Microsoft Graph; in my runs Graph refused the ordinary token too. An API that only checks a token's signature and expiry has no way to know the app was disabled, so test your own APIs.

Limit the risk

A leak does not need a mistake in a repository: infostealers now copy the tokens and API keys of AI coding tools straight from developer laptops. And removing access in one place does not always remove it everywhere, as this HackerNoon story on SSO offboarding argues. If a secret leaks, do these in order: disable the service principal, remove the secret, then look at the sign-in log before you enable the app again.

  • Disable the service principal. Stops new tokens and, on Microsoft Graph, the issued ones. It also stops the real app, so plan the outage.
  • Remove the secret. Stops new tokens only. Needed anyway, so the leaked value is useless after you re-enable the app.
  • Remove the app's role. An issued token carries its roles inside it. After I removed the assignment, both tokens still read users for the three minutes I watched.
  • Delete the app. Also ends access, though answers alternated for a short while in my run. It is harder to undo than disabling.
  • Short secret expiry. Limits how long a forgotten secret works. A one-day secret behaved like the seven-day one: revoking it left the issued token working.
  • Token lifetime policy. The policy follows the API that receives the token, not the app that asks for it. A ten-minute token lifetime policy on my client app did not change its Graph token. Set it on your own APIs.
  • No secret at all. A federated credential or a managed identity leaves nothing to copy from a file or a repository (for a worked example on HackerNoon, see replacing service principal secrets in Crossplane). A stolen token is still a token.
  • Watch the sign-ins. Service principal sign-ins appear in the Graph sign-in log with the caller's IP address, a few minutes late. Reading it needs Entra ID P1 or higher. Alert on an address the app never used before.
  • Restrict where the app signs in from. Conditional Access for workload identities can block by location. It needs Workload Identities Premium.

Managed identity, with no secret

I repeated the question with a user-assigned managed identity on an Azure Container Instance, with the same read role. A small probe script inside the container kept the first token and used it every 30 seconds.

az group create -n rg-lab-revoke-mi -l <REGION> az identity create -g rg-lab-revoke-mi -n lab-revoke-mi # assign User.Read.All to the identity's principalId, as in the setup above az container create -g rg-lab-revoke-mi -n lab-revoke-probe \ --image mcr.microsoft.com/azurelinux/base/python:3.12 \ --os-type Linux --cpu 1 --memory 1 --restart-policy Never \ --assign-identity <IDENTITY_ID> \ --environment-variables MI_CLIENT_ID=<CLIENT_ID> PROBE="$(base64 < probe.py | tr -d '\n')" \ --command-line "python3 -c 'import os,base64;exec(base64.b64decode(os.environ[\"PROBE\"]))'" az identity delete -g rg-lab-revoke-mi -n lab-revoke-mi az container logs -g rg-lab-revoke-mi -n lab-revoke-probe az group delete -n rg-lab-revoke-mi --yes 

Managed identity. A day-long token, refused soon after the identity was deleted.

The token's lifetime was 86,700 seconds. Graph refused it right after the deletion, let one more call through about a minute later, and refused every call from about 95 seconds on. For several minutes the identity endpoint kept returning the same cached token, then it started returning errors. Microsoft states that CAE for workload identities does not cover managed identities, so treat this as what I observed on Graph, not as a promise.

Cleanup

Delete the app and its service principal, then check with separate queries:

az ad app delete --id "$APP_ID" az ad app list --display-name "$APP_NAME" --query "length(@)" -o tsv az ad sp list --display-name "$APP_NAME" --query "length(@)" -o tsv rm -f secret.env new.json resp.json unset TOKEN LONG_TOKEN ENTRA_APP_SECRET 

Removed. No app and no service principal left.

Entra keeps a deleted app for 30 days, so it can still be restored from the portal.