Safeguard
Developer Tools•

How do you declare auth in OpenAPI so a scanner can log in?

Declare bearer, API key or OAuth2 under securitySchemes and reference it in security. What each type needs, and how Spring, FastAPI and NestJS generate it.

Safeguard Security Team
4 min read

Declare each auth method under components.securitySchemes with a type of http, apiKey, oauth2, openIdConnect or mutualTLS, then reference it in a top level or per operation security entry. A scanner reads those entries to know what credential to send. Without them it only sees the login wall.

The five scheme types

The OpenAPI Specification lists the valid type values as apiKey, http, mutualTLS, oauth2 and openIdConnect.

TypeRequired fieldsTypical use
http with scheme bearerscheme, optional bearerFormatToken in the Authorization header
apiKeyname, in (query, header or cookie)Static key sent by the client
oauth2flowsAuthorization code, client credentials and others
openIdConnectdiscovery URLOIDC based login
mutualTLSnone beyond typeClient certificates

For bearer, the spec describes bearerFormat as "a hint to the client to identify how the bearer token is formatted", with JWT as its example. It is documentation, not validation.

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
security:
  - bearerAuth: []

How top level and operation level interact

A root level security field declares which mechanisms can apply across the API, and operations can override it. An operation's security replaces the top level one. Details on the override edge cases are in the empty security array post.

What the frameworks generate

FrameworkWhat you do
FastAPIUsing OAuth2PasswordBearer puts an OAuth2 scheme in the description, and tokenUrl is recorded there
Spring with springdocAdd configuration or annotations that describe your scheme
NestJSAdd auth options on the document builder and decorators on controllers
RailsNo generator in the base framework, so write or maintain the file yourself

The FastAPI behavior is documented: tokenUrl declares where clients get a token and that information goes into OpenAPI and the interactive docs. For Spring, NestJS and Rails, check your library version's documentation, since the specifics vary.

Steps

  1. Pick one scheme per real auth method. Do not declare schemes your API does not accept.
  2. Put the common one in the root security field.
  3. For public routes, override with an empty list on that operation only.
  4. Export the description and read it as a scanner would. Does every protected operation list a scheme?
  5. Give the scanner a test token for each role, or the details to obtain one.
  6. Run one scan and check that protected routes returned 200 with the credential and 401 without it.

Mistakes to avoid

  • Declaring a scheme but never referencing it in security. It appears in components and does nothing.
  • Declaring bearer when the app really uses a cookie.
  • Letting the generator guess. A generated description reflects code annotations, so unannotated routes can show no security at all.

Try it yourself

Click through it at your own pace, or open it full screen. Look for where the credential is supplied to the scan.

Safeguard's SBOM and dependency scanning covers the libraries that generate these descriptions, and a free scan is available.

Quick check

Does bearerFormat enforce JWT?

No. It is a hint to clients about token format.

Where do I put the scheme name for a route?

In a security entry, at the root or on the operation, that names a scheme defined under securitySchemes.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.