You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide first-class (generated) code to support authentication/authorization #1524
As part of #1254 I'm looking at documenting the way that securitySchemes works, and it turns out it's a little more lackluster than I remember.
Right now, if you define an endpoint i.e.:
/apiKey:
get:
operationId: apiKeydescription: Perform an authenticated request, using an API Key in the `X-API-Key` headersecurity:
- apiKey: []responses:
# ... snip
The current approach sort of works if you restrict yourself to one security scheme per endpoint. We are currently using a middleware which checks the request against the given authenticators. Using it looks something like this:
For the issue with multiple security schemes per endpoint see #1644.
Our middleware currently resolves this problem by always assuming OR (which is the more common case) and disallowing AND-ing security schemes, but that has other drawbacks.
IMO, putting anything to do with auth scopes into the generated code was a mistake, because it's a runtime behavior handled properly by openapi3filter. I have an API running at my workplace which has a complex specification with multiple schemes and it works ok.
I think we should deprecate authentication logic from the generated code altogether, and I'm happy to clean up the middleware or example to make it clear how to do this. Our middleware was written when openapi3filter was quite more primitive, and we could use it better today.
What is the recommended way of getting the user context from a bearer token if using strict server? Should we just add the Authorization header as a required property in the models of each request?
What is the recommended way of getting the user context from a bearer token if using strict server? Should we just add the Authorization header as a required property in the models of each request?
Just asking because in the strict handlers, there is no access to the raw request, so no way to get the header information...I guess you could always store the token info in the context via middleware (not ideal), but this feels like a missing feature especially when talking about JWT tokens which identify API consumers are can affect the request outcome.
What is the recommended way of getting the user context from a bearer token if using strict server? Should we just add the Authorization header as a required property in the models of each request?
Just asking because in the strict handlers, there is no access to the raw request, so no way to get the header information...I guess you could always store the token info in the context via middleware (not ideal), but this feels like a missing feature especially when talking about JWT tokens which identify API consumers are can affect the request outcome.
Sorry, I am talking about the Golang server implementation, e.g:
Auth should be handled at runtime via openapi3filter, not generated code. The existing scopes constants in generated code were a design misstep and shouldn't be expanded further.
Activity
alehed commentedon Jun 6, 2024
The current approach sort of works if you restrict yourself to one security scheme per endpoint. We are currently using a middleware which checks the request against the given authenticators. Using it looks something like this:
alehed commentedon Jun 6, 2024
For the issue with multiple security schemes per endpoint see #1644.
Our middleware currently resolves this problem by always assuming OR (which is the more common case) and disallowing AND-ing security schemes, but that has other drawbacks.
mromaszewicz commentedon Jun 6, 2024
IMO, putting anything to do with auth scopes into the generated code was a mistake, because it's a runtime behavior handled properly by
openapi3filter. I have an API running at my workplace which has a complex specification with multiple schemes and it works ok.I think we should deprecate authentication logic from the generated code altogether, and I'm happy to clean up the middleware or example to make it clear how to do this. Our middleware was written when
openapi3filterwas quite more primitive, and we could use it better today.juan-carvajal commentedon Aug 4, 2025
What is the recommended way of getting the user context from a bearer token if using strict server? Should we just add the Authorization header as a required property in the models of each request?
juan-carvajal commentedon Aug 4, 2025
Just asking because in the strict handlers, there is no access to the raw request, so no way to get the header information...I guess you could always store the token info in the context via middleware (not ideal), but this feels like a missing feature especially when talking about JWT tokens which identify API consumers are can affect the request outcome.
juan-carvajal commentedon Aug 4, 2025
Sorry, I am talking about the Golang server implementation, e.g:
mromaszewicz commentedon Feb 7, 2026
Auth should be handled at runtime via
openapi3filter, not generated code. The existing scopes constants in generated code were a design misstep and shouldn't be expanded further.Deprecate ctx based security scopes (#2440)