influxdb/http/mock/middleware.go

28 lines
647 B
Go
Raw Normal View History

2019-01-18 20:46:37 +00:00
package mock
import (
"net/http"
"github.com/influxdata/influxdb/v2"
platcontext "github.com/influxdata/influxdb/v2/context"
2019-01-18 20:46:37 +00:00
)
2019-01-23 17:11:06 +00:00
// NewAuthMiddlewareHandler create a mocked middleware handler.
func NewAuthMiddlewareHandler(handler http.Handler, auth influxdb.Authorizer) http.Handler {
return &authMiddlewareHandler{
2019-01-18 20:46:37 +00:00
handler: handler,
auth: auth,
}
}
2019-01-23 17:11:06 +00:00
type authMiddlewareHandler struct {
2019-01-18 20:46:37 +00:00
handler http.Handler
auth influxdb.Authorizer
}
2019-01-23 17:11:06 +00:00
func (m *authMiddlewareHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2019-01-18 20:46:37 +00:00
ctx := r.Context()
r = r.WithContext(platcontext.SetAuthorizer(ctx, m.auth))
m.handler.ServeHTTP(w, r)
}