grafana/pkg/services/publicdashboards/api/middleware.go
Jeff Levin 331110bde5
refactor public dashboards middleware testing (#55706)
This PR refactors how we add the orgId to the context on a public dashboard paths. We also split out accessToken handling into its own package and rework status code for "RequiresValidAccessToken". We will be modeling all endpoints to use these status codes going forward. Additionally, it includes a scaffold for better middleware testing and refactors existing tests to table drive tests.
2022-10-06 12:35:19 -08:00

69 lines
1.9 KiB
Go

package api
import (
"net/http"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/publicdashboards"
"github.com/grafana/grafana/pkg/services/publicdashboards/internal/tokens"
"github.com/grafana/grafana/pkg/web"
)
// Adds orgId to context based on org of public dashboard
func SetPublicDashboardOrgIdOnContext(publicDashboardService publicdashboards.Service) func(c *models.ReqContext) {
return func(c *models.ReqContext) {
accessToken, ok := web.Params(c.Req)[":accessToken"]
if !ok || !tokens.IsValidAccessToken(accessToken) {
return
}
// Get public dashboard
orgId, err := publicDashboardService.GetPublicDashboardOrgId(c.Req.Context(), accessToken)
if err != nil {
return
}
c.OrgID = orgId
}
}
// Adds public dashboard flag on context
func SetPublicDashboardFlag(c *models.ReqContext) {
c.IsPublicDashboardView = true
}
// Middleware to enforce that a public dashboards exists before continuing to
// handler
func RequiresValidAccessToken(publicDashboardService publicdashboards.Service) func(c *models.ReqContext) {
return func(c *models.ReqContext) {
accessToken, ok := web.Params(c.Req)[":accessToken"]
if !ok {
c.JsonApiErr(http.StatusBadRequest, "No access token provided", nil)
return
}
if !tokens.IsValidAccessToken(accessToken) {
c.JsonApiErr(http.StatusBadRequest, "Invalid access token", nil)
}
// Check that the access token references an enabled public dashboard
exists, err := publicDashboardService.AccessTokenExists(c.Req.Context(), accessToken)
if err != nil {
c.JsonApiErr(http.StatusInternalServerError, "Failed to query access token", nil)
return
}
if !exists {
c.JsonApiErr(http.StatusNotFound, "Public dashboard not found", nil)
return
}
}
}
func CountPublicDashboardRequest() func(c *models.ReqContext) {
return func(c *models.ReqContext) {
metrics.MPublicDashboardRequestCount.Inc()
}
}