API-38Required
Authenticate with tokens in headers
Authenticate API calls with short-lived tokens in the Authorization header, issued through OAuth 2 for anything acting for a user.
How to do it
Follow the OAuth security best practice in RFC 9700. Use the authorization code flow with PKCE for apps, and client credentials for servers. Let access tokens expire within the limit in verify every token, and renew them with refresh tokens. RFC 9700 says a public client's refresh token must be rotated on every use, or bound to the client with DPoP or mutual TLS. With rotation, a reused old refresh token revokes the whole chain. Publish protected resource metadata as RFC 9728 describes. Check every token as that rule says. For calls from browsers on other sites, see strict CORS.
Examples
HTTP example
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
Content-Type: application/problem+json
How to check
A call without a token gets 401 with a WWW-Authenticate header. An expired token gets 401 too. Refresh once, then send the old refresh token again. It is refused, and the new refresh token from the first refresh stops working too.
Sources
- RFC 9700 Best Current Practice for OAuth 2.0 Security
- RFC 6750 OAuth 2.0 Bearer Token Usage
- RFC 9728 OAuth 2.0 Protected Resource Metadata
Related rules
- APS-12 Verify every token in Application security
- APS-37 Share data across sites on purpose in Application security
- API-95 Bind high-value tokens to the client that holds them in API design
- INF-37 Authenticate calls between your own services in Infrastructure and environments
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/api-security/token-auth">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.