In an OpenAPI oauth2 security scheme, what do the flows object and its scopes declare?
answer
- One required field names the grant types
- Each carries the URLs its grant needs
- Every one also carries a required map
- That map is the registry operations draw from
- None of it rejects a request
basics
~20 sAn OpenAPI oauth2 scheme's flows object names which grant types the API supports — authorizationCode, clientCredentials, password, implicit — each with its endpoint URLs and a required scopes map of scope name to description. Operations then reference subsets of those scope names.
solid answer
~50 s`type: oauth2` requires a `flows` object whose members are `authorizationCode`, `clientCredentials`, `password` and `implicit`; declare only the ones the API actually supports. Each flow carries the URLs it needs — `authorizationUrl` for implicit and authorization-code, `tokenUrl` for the three that mint tokens, plus an optional `refreshUrl` — and a **required** `scopes` map from scope name to human description. That map is the registry: a security requirement may only reference scope names declared there. An operation then names the scheme with the scope it needs, meaning a token bearing that scope is expected. All of this is **declaration**. The document does not verify a token, check a scope, or configure anything; it exists so documentation shows the right Authorize dialog and generated clients know which scopes to request. Keeping declared scopes aligned with enforced ones is a testing discipline, not a spec feature.
code
yaml · 26 linescomponents:
securitySchemes:
petstoreOauth:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://example.com/oauth/authorize
tokenUrl: https://example.com/oauth/token
refreshUrl: https://example.com/oauth/refresh
scopes:
read:pets: read your pets
write:pets: modify pets on your account
clientCredentials:
tokenUrl: https://example.com/oauth/token
scopes:
admin:pets: administer the pet catalogue
paths:
/pets:
post:
security:
- petstoreOauth:
- write:pets
responses:
'201':
description: Createdgo deeper
Recall that an oauth2 scheme lists the flows the API supports and that each flow declares the scope names available under it.
Name the four flow keys and the URLs each requires, and explain that a requirement may only reference scope names the flow's scopes map declares.
Demonstrate that you treat declared scopes as documentation and can name a concrete way to catch drift between them and what the gateway actually enforces.
Own the scope taxonomy across services and decide whether specs restate oauth2 flows inline or delegate to an openIdConnect discovery URL, weighing self-containment against drift.
## The shape of the object A scheme of `type: oauth2` has one required field, `flows`, whose members are the grant types the API accepts. Each member carries its endpoint URLs and its own `scopes` map. At least one flow must be present. ## The four flows and their required fields - **`authorizationCode`** — requires `authorizationUrl` and `tokenUrl`. The flow for applications acting on a user's behalf. - **`clientCredentials`** — requires `tokenUrl`. Machine-to-machine, no end user present. - **`password`** — requires `tokenUrl`. The client collects the user's credentials directly. - **`implicit`** — requires `authorizationUrl` only, since the token comes back from the authorization endpoint. All four accept an optional `refreshUrl`, and all four require a `scopes` map, which may be an empty object if the API defines no scopes. OpenAPI still defines `implicit` and `password` even though current OAuth security guidance discourages both; the document describes what an API accepts, so if the service still honours them, declaring them is the honest thing to do — and the presence of an `implicit` flow in a document is itself a useful review signal. Note that the flow **names in OpenAPI 3.x differ from Swagger 2.0**, which used `application` and `accessCode` for what are now `clientCredentials` and `authorizationCode`. Converted documents frequently carry the old names, which are invalid in 3.x. ## Scopes: declaration and reference The `scopes` map inside each flow is the authoritative list for that flow: keys are scope names exactly as the authorization server issues them, values are human-readable descriptions that documentation renderers display beside the checkboxes in an Authorize dialog. A security requirement may reference only names declared here — an operation asking for `read:pets` is valid because that key appears in the map; a typo is not caught by JSON-schema validation of the document but is caught by most linters and by any tool that resolves the reference. Because each flow has its own `scopes` map, a scope available under `authorizationCode` need not exist under `clientCredentials`. That is how you say a machine client can administer the catalogue but a user-delegated token cannot, or the reverse. ## openIdConnect as the alternative A scheme of `type: openIdConnect` with an `openIdConnectUrl` delegates all of this to the provider's discovery document instead of restating flows, endpoints and scopes inline. The advantage is that the document cannot drift from the provider's real configuration; the cost is that neither a reader nor an offline tool can tell what the API accepts without fetching that URL. Teams that want their spec to be self-contained — for air-gapped review, or for generators that do not fetch — keep the oauth2 form and accept the maintenance. ## Declaration is not enforcement — the point interviewers push on Nothing in the document verifies a token or checks a scope. Naming a scope on an operation does not cause any request to be rejected. Enforcement lives in the gateway, the framework's filter chain or the handler. The consequences of forgetting this are concrete: - A spec claiming an operation needs a write scope while the service checks nothing produces integrators who believe they are safe and an endpoint that is not. - A spec omitting a scope the service does enforce produces clients that request too little and receive a 403 they cannot diagnose from the documentation. - Scope names drifting from what the authorization server issues produces an Authorize dialog whose checkboxes mint tokens the API rejects. The mitigations are engineering, not syntax: generate the gateway's scope configuration from the same source as the document, or run contract tests that call each operation with a deliberately under-scoped token and assert the rejection. In a senior interview, naming that drift and one concrete way of catching it is the substance of the answer; reciting the four flow names is the preliminary. ## A review checklist - Only the flows the service really accepts are declared; an `implicit` flow is questioned rather than copied forward. - Every flow has the URLs its grant type requires, pointing at the real authorization server. - Scope names match what the authorization server issues, character for character. - Every scope referenced by an operation exists in the flow's `scopes` map. - There is a test, somewhere, that fails when the declared scope and the enforced scope diverge.
- Which fields does each OAuth2 flow object require in OpenAPI 3.x?`authorizationCode` requires `authorizationUrl` and `tokenUrl`; `implicit` requires `authorizationUrl`; `password` and `clientCredentials` require `tokenUrl`. All four require a `scopes` map, which may be empty, and all four accept an optional `refreshUrl`. At least one flow must be present, since `flows` itself is required for a scheme of type oauth2.
- When would you prefer an openIdConnect scheme over declaring oauth2 flows inline?When the provider's discovery document should be the single source of truth. `openIdConnect` needs only `openIdConnectUrl`, so endpoints and supported flows cannot drift out of step with the provider's real configuration. The tradeoff is that the spec is no longer self-contained: any reader or offline tool must fetch that URL to learn what the API actually accepts.
- How do you keep declared scopes in step with what the service enforces?Not through the document — it describes and never enforces. The workable answers are generating the gateway's scope configuration from the same source as the spec, or contract tests that call each protected operation with a deliberately under-scoped token and assert a rejection. Without one of those, a spec can promise a scope check the service never performs.
saying these in an interview costs you the question
- Uses the Swagger 2.0 flow names application or accessCode
- Thinks declaring a scope causes it to be enforced
- Omits the required scopes map from a flow
- Assumes every flow shares one scopes map
- Believes openIdConnect still needs flows declared inline