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
I have been using this package for some time on a number of public API with great joy. However, I now have a need to set up JWT Bearer token auth on a number of API endpoints and I am finding that the usage patterns for this arent clear from reading the documentation or the code. I would really love some guidance on how this is meant to be implemented.
As a starting point I have defined the following test api endpoint in my openapi.json spec as follows;
components:
responses:
UnauthorizedError:
description: Acces token is missing or invalid
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
paths:
"/v1/jwt":
get:
security:
- bearerAuth: []
operationId: GetJWT
summary: Get JWT info
description: JWT Test interface
responses:
"200":
description: Return info on the JWT
"401":
$ref: '#/components/responses/UnauthorizedError'
In turn, this generated the following in the codegen (Which for the life of me I can't see how this really archives anything useful?):
// GetJWT converts echo context to params.
func (w *ServerInterfaceWrapper) GetJWT(ctx echo.Context) error {
var err error
ctx.Set("bearerAuth.Scopes", []string{""})
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.GetJWT(ctx)
return err
}
Be great to know what is now required to turn a simple handler (as follows) into one that requires AUTH?
Reacted by Denis Subbotin, Pushkar Gaur, Aliaksandr Tarasevich, Karan Kapoor, Avikash-Mishra, moonlight, Mitar, Jakob, David Mills, zurustar and 7 more
So I made this work by making a custom middleware which validates the access token and checks that it has all the required scopes. And I get required scopes in the context from the code generated by this package.
@mitar that's a good workaround I found myself using, but.. if we want to use OapiRequestValidator as in the examples no matter what we do the call will always fail
So I made this work by making a custom middleware which validates the access token and checks that it has all the required scopes. And I get required scopes in the context from the code generated by this package.
Can you please guide on this more .. I am trying to write my own middleware, but i cannot find the scopes that are set. please see #911
correct me if i am wrong, but wouldn't that require writing your own template for code generation? ..
right now, the code that is being generated is
func (w*ServerInterfaceWrapper) ValidateAPIKey(ctx echo.Context) error {
varerrerrorctx.Set(APIKeyAuthScopes, []string{""})
// Invoke the callback with all the unmarshalled argumentserr=w.Handler.ValidateAPIKey(ctx)
returnerr
}
func (w*ServerInterfaceWrapper) ValidateAPIKey(ctx echo.Context) error {
varerrerrorctx.Set(APIKeyAuthScopes, []string{""})
// Invoke the callback with all the unmarshalled argumentserr=w.Middleware(w.Handler.ValidateAPIKey(ctx))
returnerr
}
The README now has a dedicated "Implementing security" section with documentation on Security Schemes, Security Requirements, validation middleware, and the securityprovider package. There is also an authenticated API example at examples/authenticated-api/echo.
The original ask for guidance on bearer auth usage is well-addressed by the current documentation.
Activity
mitar commentedon May 22, 2021
Why is the code
[]string{""}and not[]string{}for empty list?mitar commentedon May 25, 2021
So I made this work by making a custom middleware which validates the access token and checks that it has all the required scopes. And I get required scopes in the context from the code generated by this package.
zeako commentedon Jul 5, 2021
@mitar that's a good workaround I found myself using, but.. if we want to use
OapiRequestValidatoras in the examples no matter what we do the call will always faildebuggerpk commentedon Dec 31, 2022
Can you please guide on this more .. I am trying to write my own middleware, but i cannot find the scopes that are set. please see #911
mitar commentedon Dec 31, 2022
My middleware is just a wrapper around a handler.
debuggerpk commentedon Dec 31, 2022
correct me if i am wrong, but wouldn't that require writing your own template for code generation? ..
right now, the code that is being generated is
we want the
ServerInterfaceWrapperto beand the actual function to generate
Thanks for taking a peak.
mitar commentedon Dec 31, 2022
I am using Chi. There, generated code looks like:
So middlewares are called after context is set.
mromaszewicz commentedon Feb 7, 2026
The README now has a dedicated "Implementing security" section with documentation on Security Schemes, Security Requirements, validation middleware, and the securityprovider package. There is also an authenticated API example at examples/authenticated-api/echo.
The original ask for guidance on bearer auth usage is well-addressed by the current documentation.
Closing as completed.