@@ -244,6 +244,9 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo
244244 _ , span := h .tracer .Start (reqContext .Req .Context (), "initContextWithAPIKey" )
245245 defer span .End ()
246246
247+ ctx := WithAuthHTTPHeader (reqContext .Req .Context (), "Authorization" )
248+ * reqContext .Req = * reqContext .Req .WithContext (ctx )
249+
247250 var (
248251 apikey * models.ApiKey
249252 errKey error
@@ -326,7 +329,7 @@ func (h *ContextHandler) initContextWithBasicAuth(reqContext *models.ReqContext,
326329 return false
327330 }
328331
329- ctx , span := h .tracer .Start (reqContext .Req .Context (), "initContextWithBasicAuth" )
332+ _ , span := h .tracer .Start (reqContext .Req .Context (), "initContextWithBasicAuth" )
330333 defer span .End ()
331334
332335 username , password , err := util .DecodeBasicAuthHeader (header )
@@ -335,12 +338,15 @@ func (h *ContextHandler) initContextWithBasicAuth(reqContext *models.ReqContext,
335338 return true
336339 }
337340
341+ ctx := WithAuthHTTPHeader (reqContext .Req .Context (), "Authorization" )
342+ * reqContext .Req = * reqContext .Req .WithContext (ctx )
343+
338344 authQuery := models.LoginUserQuery {
339345 Username : username ,
340346 Password : password ,
341347 Cfg : h .Cfg ,
342348 }
343- if err := h .authenticator .AuthenticateUser (reqContext . Req . Context () , & authQuery ); err != nil {
349+ if err := h .authenticator .AuthenticateUser (ctx , & authQuery ); err != nil {
344350 reqContext .Logger .Debug (
345351 "Failed to authorize the user" ,
346352 "username" , username ,
@@ -571,6 +577,15 @@ func (h *ContextHandler) initContextWithAuthProxy(reqContext *models.ReqContext,
571577
572578 logger .Debug ("Successfully got user info" , "userID" , user .UserId , "username" , user .Login )
573579
580+ ctx := WithAuthHTTPHeader (reqContext .Req .Context (), h .Cfg .AuthProxyHeaderName )
581+ for _ , header := range h .Cfg .AuthProxyHeaders {
582+ if header != "" {
583+ ctx = WithAuthHTTPHeader (ctx , header )
584+ }
585+ }
586+
587+ * reqContext .Req = * reqContext .Req .WithContext (ctx )
588+
574589 // Add user info to context
575590 reqContext .SignedInUser = user
576591 reqContext .IsSignedIn = true
@@ -590,3 +605,38 @@ func (h *ContextHandler) initContextWithAuthProxy(reqContext *models.ReqContext,
590605
591606 return true
592607}
608+
609+ type authHTTPHeaderListContextKey struct {}
610+
611+ var authHTTPHeaderListKey = authHTTPHeaderListContextKey {}
612+
613+ // AuthHTTPHeaderList used to record HTTP headers that being when verifying authentication
614+ // of an incoming HTTP request.
615+ type AuthHTTPHeaderList struct {
616+ Items []string
617+ }
618+
619+ // WithAuthHTTPHeader returns a copy of parent in which the named HTTP header will be included
620+ // and later retrievable by AuthHTTPHeaderListFromContext.
621+ func WithAuthHTTPHeader (parent context.Context , name string ) context.Context {
622+ list := AuthHTTPHeaderListFromContext (parent )
623+
624+ if list == nil {
625+ list = & AuthHTTPHeaderList {
626+ Items : []string {},
627+ }
628+ }
629+
630+ list .Items = append (list .Items , name )
631+
632+ return context .WithValue (parent , authHTTPHeaderListKey , list )
633+ }
634+
635+ // AuthHTTPHeaderListFromContext returns the AuthHTTPHeaderList in a context.Context, if any,
636+ // and will include any HTTP headers used when verifying authentication of an incoming HTTP request.
637+ func AuthHTTPHeaderListFromContext (c context.Context ) * AuthHTTPHeaderList {
638+ if list , ok := c .Value (authHTTPHeaderListKey ).(* AuthHTTPHeaderList ); ok {
639+ return list
640+ }
641+ return nil
642+ }
0 commit comments