|
|
|
@ -4,7 +4,9 @@ package contexthandler |
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"net/http" |
|
|
|
|
"strconv" |
|
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute" |
|
|
|
|
"go.opentelemetry.io/otel/trace" |
|
|
|
@ -135,10 +137,51 @@ func (h *ContextHandler) Middleware(next http.Handler) http.Handler { |
|
|
|
|
attribute.Int64("userId", reqContext.UserID), |
|
|
|
|
)) |
|
|
|
|
|
|
|
|
|
if h.Cfg.IDResponseHeaderEnabled && reqContext.SignedInUser != nil { |
|
|
|
|
namespace, id := getNamespaceAndID(reqContext.SignedInUser) |
|
|
|
|
reqContext.Resp.Before(h.addIDHeaderEndOfRequestFunc(namespace, id)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO(kalleep): Refactor to user identity.Requester interface and methods after we have backported this
|
|
|
|
|
func getNamespaceAndID(user *user.SignedInUser) (string, string) { |
|
|
|
|
var namespace, id string |
|
|
|
|
if user.UserID > 0 && user.IsServiceAccount { |
|
|
|
|
id = strconv.Itoa(int(user.UserID)) |
|
|
|
|
namespace = "service-account" |
|
|
|
|
} else if user.UserID > 0 { |
|
|
|
|
id = strconv.Itoa(int(user.UserID)) |
|
|
|
|
namespace = "user" |
|
|
|
|
} else if user.ApiKeyID > 0 { |
|
|
|
|
id = strconv.Itoa(int(user.ApiKeyID)) |
|
|
|
|
namespace = "api-key" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return namespace, id |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *ContextHandler) addIDHeaderEndOfRequestFunc(namespace, id string) web.BeforeFunc { |
|
|
|
|
return func(w web.ResponseWriter) { |
|
|
|
|
if w.Written() { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if namespace == "" || id == "" { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, ok := h.Cfg.IDResponseHeaderNamespaces[namespace]; !ok { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
headerName := fmt.Sprintf("%s-Identity-Id", h.Cfg.IDResponseHeaderPrefix) |
|
|
|
|
w.Header().Add(headerName, fmt.Sprintf("%s:%s", namespace, id)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *ContextHandler) deleteInvalidCookieEndOfRequestFunc(reqContext *contextmodel.ReqContext) web.BeforeFunc { |
|
|
|
|
return func(w web.ResponseWriter) { |
|
|
|
|
if h.features.IsEnabled(reqContext.Req.Context(), featuremgmt.FlagClientTokenRotation) { |
|
|
|
|