github.com/infraboard/keyauth@v0.8.1/client/interceptor/http_restful.go (about) 1 package interceptor 2 3 import ( 4 "github.com/emicklei/go-restful/v3" 5 "github.com/infraboard/keyauth/apps/token" 6 "github.com/infraboard/mcube/http/label" 7 "github.com/infraboard/mcube/http/response" 8 "github.com/infraboard/mcube/pb/http" 9 ) 10 11 func GetEntryFromRouteReader(r restful.RouteReader) http.Entry { 12 m := label.Meta(r.Metadata()) 13 return http.Entry{ 14 FunctionName: r.Operation(), 15 Path: r.Path(), 16 Resource: m.Resource(), 17 AuthEnable: m.AuthEnable(), 18 PermissionEnable: m.PermissionEnable(), 19 Allow: m.Allow(), 20 AuditLog: m.AuditEnable(), 21 Labels: map[string]string{ 22 label.Action: m.Action(), 23 }, 24 } 25 } 26 27 func (a *HTTPAuther) RestfulAuthHandlerFunc(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) { 28 authInfo, err := a.Auth(req.Request, GetEntryFromRouteReader(req.SelectedRoute())) 29 if err != nil { 30 response.Failed(resp.ResponseWriter, err) 31 return 32 } 33 34 if tk, ok := authInfo.(*token.Token); ok { 35 if tk != nil { 36 req.SetAttribute("token", tk) 37 } 38 } 39 40 chain.ProcessFilter(req, resp) 41 }