Machine-to-Machine Auth: OAuth 2.0 Client Credentials Grant & Microservice Identities
The Client Credentials Grant (RFC 6749 §4.4) enables automated daemon services, background batch jobs, and backend microservices to authenticate autonomously without human interaction. The client authenticates directly using its credentials to receive short-lived access tokens.
Step 1: Background service securely retrieves its client credentials from an environment secret or AWS KMS.
Microservices communicate continually without a logged-in user present (e.g. billing reconciliation, database backups). Hardcoding database passwords or shared API keys across multiple microservices creates security nightmares. Client Credentials provides auditable, scoped machine identities.
The Corporate Service Badge: Delivery drones and automated vacuum robots don't have usernames or fingerprints; they present encrypted RFID service badges to doors to execute maintenance tasks without human accompaniment.
[ CosmosApp Billing Service (Client) ] [ Authorization Server (IdP) ]
│ │
├── (1) POST /oauth/token ────────────────────────►│
│ grant_type=client_credentials │
│ client_id=svc_billing_01 │
│ client_secret=secret_xyz │
│ scope=analytics:write │
│ ├── Verify Client Credentials
│ ├── Issue Scoped Machine Token
│◄── (2) 200 OK { access_token (TTL: 15m) } ───────┤
│
├── (3) POST /api/v1/metrics (Bearer <token>) ────► [ Analytics Microservice ]
│ ├── Validate Token Signature & Scope
│◄── (4) 202 Accepted ─────────────────────────────┤CosmosApp's nightly billing worker needs to trigger usage aggregations on the telemetry microservice. No human is involved. The billing worker submits its client_id and client_secret to the internal identity provider via grant_type=client_credentials, obtaining a 15-minute token with scope usage:aggregate.
Step 1: Background service securely retrieves its client credentials from an environment secret or AWS KMS.
Step 2: Service makes a direct HTTPS POST request to /oauth/token with grant_type=client_credentials.
Step 3: Identity provider verifies client credentials and validates requested scopes.
Step 4: Identity provider returns a signed JWT access token with short lifetime (typically 15–60 minutes).
Step 5: Service caches the token in memory and attaches it as Authorization: Bearer <token> on API requests.
Step 6: Service proactively refreshes the token before it reaches expiration.
// CosmosApp Machine-to-Machine Client Token Fetcher:
export async function getMachineToken(clientId: string, clientSecret: string, scope: string) {
const credentials = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
const res = await fetch('https://auth.cosmos.app/oauth/token', {
method: 'POST',
headers: {
'Authorization': `Basic ${credentials}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'client_credentials',
scope: scope,
}),
});
const data = await res.json();
return {
accessToken: data.access_token,
expiresIn: data.expires_in,
};
}- ✕Client Credentials grant issues Refresh Tokens (RFC 6749 states refresh tokens should not be issued for client credentials; the client simply requests a new token using its credentials).
- ✕Machine-to-machine tokens don't need expiration (Machine tokens must be short-lived to minimize damage if an in-memory token is exfiltrated).
Key Architectural Takeaways
- ✓Use Client Credentials grant strictly for machine-to-machine communication without human users.
- ✓Never issue refresh tokens for the client credentials grant; clients re-authenticate directly.
- ✓Grant minimal, least-privilege scopes to each microservice identity.
Sharing the same client credentials across multiple distinct microservices.
Provision unique client_ids and secrets for each distinct service to maintain isolation and audit logging.
Why does the OAuth 2.0 specification specify that Refresh Tokens should NOT be issued for the Client Credentials Grant?