2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-08-28 03:59:17 +00:00
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-08-28 03:59:17 +00:00
|
|
|
"net/http"
|
2016-09-08 01:24:11 +00:00
|
|
|
"strings"
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/token"
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-06-06 22:07:29 +00:00
|
|
|
|
2021-05-17 17:29:50 +00:00
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
2016-08-28 03:59:17 +00:00
|
|
|
)
|
|
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
// setRequestsContexts updates the request with necessary context values for a request
|
2021-06-07 01:10:58 +00:00
|
|
|
func setRequestsContexts(svc fleet.Service) kithttp.RequestFunc {
|
2016-09-08 01:24:11 +00:00
|
|
|
return func(ctx context.Context, r *http.Request) context.Context {
|
2016-09-26 17:14:39 +00:00
|
|
|
bearer := token.FromHTTPRequest(r)
|
|
|
|
|
ctx = token.NewContext(ctx, bearer)
|
2021-09-23 15:44:04 +00:00
|
|
|
if bearer != "" {
|
|
|
|
|
v, err := authViewer(ctx, string(bearer), svc)
|
|
|
|
|
if err == nil {
|
|
|
|
|
ctx = viewer.NewContext(ctx, *v)
|
|
|
|
|
}
|
2016-09-08 01:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the user-id for request
|
|
|
|
|
if strings.Contains(r.URL.Path, "users/") {
|
|
|
|
|
ctx = withUserIDFromRequest(r, ctx)
|
|
|
|
|
}
|
2021-08-02 22:06:27 +00:00
|
|
|
|
|
|
|
|
ctx = logging.NewContext(ctx, &logging.LoggingContext{})
|
|
|
|
|
ctx = logging.WithStartTime(ctx)
|
2016-09-08 01:24:11 +00:00
|
|
|
return ctx
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
2016-09-08 01:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func withUserIDFromRequest(r *http.Request, ctx context.Context) context.Context {
|
|
|
|
|
id, _ := idFromRequest(r, "id")
|
|
|
|
|
return context.WithValue(ctx, "request-id", id)
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|