go-openapi / runtime

openapi runtime interfaces
Apache License 2.0
244 stars 146 forks source link

Get Principal and other Request information from OperationHandler #203

Open gcastillo-bakkt opened 3 years ago

gcastillo-bakkt commented 3 years ago

Hi, i'm trying to figure out a way to get the Principal or request context from within the OperationHandler. I need to be able to take the caller's credentials to make a call to a downstream system. Right now, I am able to work around this by defining the api_key as a header parameter but that doesn't play well with swagger-ui and other tools.

youyuanwu commented 3 years ago

If the info is in the http context object, you can access it. https://github.com/go-swagger/go-swagger/blob/b49f50b58d021e303ce819ab8310add9bad3a99d/examples/auto-configure/restapi/operations/todos/destroy_one_parameters.go#L29

HTTPRequest.Context()

Can give you the context, and extract the values in side.

gcastillo-bakkt commented 3 years ago

Thank you for this, I'm using an untyped api though. With this workflow I don't the RequestBinder interface comes into play.

fredbi commented 9 months ago
principal := middleware.SecurityPrincipalFrom(req)

should do the job.

Other information is stored in the request context can be extracted with similar helpers: