mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Clean up the context handler (#75864)
* Chore: Clean up the context handler Co-authored-by: Kalle Persson <kalle.persson@grafana.com> * Better comment Co-authored-by: Kalle Persson <kalle.persson@grafana.com> --------- Co-authored-by: Kalle Persson <kalle.persson@grafana.com>
This commit is contained in:
parent
e1aad9c9bf
commit
13a10b9bf0
@ -83,13 +83,11 @@ func CopyWithReqContext(ctx context.Context) context.Context {
|
|||||||
// Middleware provides a middleware to initialize the request context.
|
// Middleware provides a middleware to initialize the request context.
|
||||||
func (h *ContextHandler) Middleware(next http.Handler) http.Handler {
|
func (h *ContextHandler) Middleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx, span := h.tracer.Start(r.Context(), "Auth - Middleware")
|
||||||
mContext := web.FromContext(ctx)
|
defer span.End() // this will span to next handlers as well
|
||||||
_, span := h.tracer.Start(ctx, "Auth - Middleware")
|
|
||||||
defer span.End()
|
|
||||||
|
|
||||||
reqContext := &contextmodel.ReqContext{
|
reqContext := &contextmodel.ReqContext{
|
||||||
Context: mContext,
|
Context: web.FromContext(ctx), // Extract web context from context (no knowledge of the trace)
|
||||||
SignedInUser: &user.SignedInUser{
|
SignedInUser: &user.SignedInUser{
|
||||||
Permissions: map[int64]map[string][]string{},
|
Permissions: map[int64]map[string][]string{},
|
||||||
},
|
},
|
||||||
@ -99,17 +97,20 @@ func (h *ContextHandler) Middleware(next http.Handler) http.Handler {
|
|||||||
Logger: log.New("context"),
|
Logger: log.New("context"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inject ReqContext into http.Request.Context
|
// inject ReqContext in the context
|
||||||
*r = *r.WithContext(context.WithValue(ctx, reqContextKey{}, reqContext))
|
ctx = context.WithValue(ctx, reqContextKey{}, reqContext)
|
||||||
// store list of possible auth header in context
|
// store list of possible auth header in context
|
||||||
*reqContext.Req = *reqContext.Req.WithContext(WithAuthHTTPHeaders(reqContext.Req.Context(), h.Cfg))
|
ctx = WithAuthHTTPHeaders(ctx, h.Cfg)
|
||||||
|
// Set the context for the http.Request.Context
|
||||||
|
// This modifies both r and reqContext.Req since they point to the same value
|
||||||
|
*reqContext.Req = *reqContext.Req.WithContext(ctx)
|
||||||
|
|
||||||
traceID := tracing.TraceIDFromContext(mContext.Req.Context(), false)
|
traceID := tracing.TraceIDFromContext(reqContext.Req.Context(), false)
|
||||||
if traceID != "" {
|
if traceID != "" {
|
||||||
reqContext.Logger = reqContext.Logger.New("traceID", traceID)
|
reqContext.Logger = reqContext.Logger.New("traceID", traceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
identity, err := h.authnService.Authenticate(ctx, &authn.Request{HTTPRequest: reqContext.Req, Resp: reqContext.Resp})
|
identity, err := h.authnService.Authenticate(reqContext.Req.Context(), &authn.Request{HTTPRequest: reqContext.Req, Resp: reqContext.Resp})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, auth.ErrInvalidSessionToken) || errors.Is(err, authn.ErrExpiredAccessToken) {
|
if errors.Is(err, auth.ErrInvalidSessionToken) || errors.Is(err, authn.ErrExpiredAccessToken) {
|
||||||
// Burn the cookie in case of invalid, expired or missing token
|
// Burn the cookie in case of invalid, expired or missing token
|
||||||
|
Loading…
Reference in New Issue
Block a user