kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

How AuthenticationFunc pass identifier to handler

Open blame2020 opened this issue 2 years ago • 1 comments

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?

blame2020 avatar Oct 20 '22 12:10 blame2020

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
}

ddzero2c avatar Nov 15 '22 03:11 ddzero2c