kin-openapi
kin-openapi copied to clipboard
How AuthenticationFunc pass identifier to handler
Hi.
I'm using thi great library with https://github.com/deepmap/oapi-codegen.
I want to authenticate with AuthenticationFunc, and pass authenticated identifier to relayed handlers, but, AuthenticationFunc doesn't has output interface.
Is there a way to pass the id to the handler?
Hi @blame2020, I just found a solution:
...
import oapimw "github.com/deepmap/oapi-codegen/pkg/middleware"
...
oapimw.OapiRequestValidatorWithOptions(spec, &oapimw.Options{
Options: openapi3filter.Options{
AuthenticationFunc: func(ctx context.Context, input *openapi3filter.AuthenticationInput) error {
c := oapimw.GetEchoContext(ctx)
c.Set("user id", "id")
return nil
},
},
})
func(c echo.Context) error {
xxx, ok := c.Get("user id").(string)
return error
}