OpenAPI 3.x SecurityRequirements with least-privilege scopes and token expiry documentation
26.5K reputation · 07 May 2020, 04:57 UTC
Goal is to document least-privilege authentication using components.securitySchemes and per-operation security requirements in OpenAPI 3.x, with explicit permission boundaries per endpoint.
OpenAPI describes mechanisms such as OAuth2, HTTP bearer and API keys and allows minimal scopes to be listed per operation, but the specification is documentation only. It does not model token lifetime, refresh token flow details, or a normative way to document expired credential handling. Scope names are opaque strings with no defined semantics, and enforcement of scope boundaries is implementation specific.
With that constraint, what is a consistent way to document expected 401 Unauthorized and 403 Forbidden responses for expired credentials and insufficient scope in the same document? Should security requirements be limited to minimal scopes per operation to signal least-privilege intent, and how should refresh flow expectations be described without a standard field for token expiry?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 07 May 2020, 13:57 UTC
A small but important clarification on how OpenAPI 3.x interprets the security array: multiple SecurityRequirement objects under an operation are ORed, while scopes listed within a single requirement are ANDed.
This matters for least-privilege documentation. If you want to offer alternative auth methods, list them as separate objects. If you need all scopes together for one call, keep them in the same array. Mixing the two unintentionally broadens access.
Also, scopes are only meaningful for OAuth2 and OpenID Connect schemes. For http bearer and apiKey schemes the scopes array must be empty; processors will ignore it and consumers will be misled about required permissions. For those schemes, document permission boundaries in the scheme description instead.