Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add Route metadata to request attributes or allow adding attributes to routes #459

Closed
Fapiko opened this issue Dec 7, 2020 · 4 comments

Comments

@Fapiko
Copy link

Fapiko commented Dec 7, 2020

It would be nice to see metadata or some way to inject attributes of routes into the request object so they are accessible in handlers and filters. It seems like it would be fairly easy to do it when the request is wrapped:

go-restful/route.go

Lines 69 to 77 in 80bbcf8

func (r *Route) wrapRequestResponse(httpWriter http.ResponseWriter, httpRequest *http.Request, pathParams map[string]string) (*Request, *Response) {
wrappedRequest := NewRequest(httpRequest)
wrappedRequest.pathParameters = pathParams
wrappedRequest.selectedRoutePath = r.Path
wrappedResponse := NewResponse(httpWriter)
wrappedResponse.requestAccept = httpRequest.Header.Get(HEADER_Accept)
wrappedResponse.routeProduces = r.Produces
return wrappedRequest, wrappedResponse
}
It already has an Attributes field but those seem to be scoped to an inbound request so I'm not sure how that field usage is intended to differ from Context

This would be incredibly handy for handling things like authorization. In my case I'd like to tag each of the routes with an object and permission required to access that resource, then add a filter to verify the authenticated client has authorization to that object and permission.

It would allow something like this when building the routes:

ws.Route(ws.GET("/users").To(u.getUsers).
	Doc("Gets a list of all users in the platform").
	Metadata(restfulspec.KeyOpenAPITags, tags).
	Attribute("auth_object", "users").
	Attribute("auth_permission", "view").
	Writes([]UserResponse{}))

I was attempting to use the metadata field but as it stands right now I cannot figure out a way to link a request from the handler or a filter back to a route to look that info up.

@emicklei
Copy link
Owner

emicklei commented Dec 8, 2020

interesting.

Yes, the attributes field was introduced at a time that Context was not available or not part of the standard pkgs.
I will have a look if and how attributes can be made available to the request.

@emicklei
Copy link
Owner

emicklei commented Dec 13, 2020

have a look at #462 . I will create a test before merging it

@emicklei
Copy link
Owner

@Fapiko does this help your usecase?

emicklei added a commit that referenced this issue Apr 10, 2021
* add access to Route from Request

* add RouteReader
emicklei added a commit that referenced this issue Apr 10, 2021
* add access to Route from Request

* add RouteReader
@emicklei
Copy link
Owner

this feature is include in v.3.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants