2022-07-06 18:51:44 -05:00
|
|
|
package api
|
2022-06-02 15:57:55 -05:00
|
|
|
|
2022-07-19 09:50:37 -05:00
|
|
|
import (
|
2022-08-10 12:14:48 -05:00
|
|
|
"net/http"
|
|
|
|
|
2022-07-19 09:50:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-08-10 12:14:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/publicdashboards"
|
2023-01-27 08:20:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/publicdashboards/validation"
|
2022-08-10 12:14:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2022-07-19 09:50:37 -05:00
|
|
|
)
|
2022-06-02 15:57:55 -05:00
|
|
|
|
2022-10-20 14:43:33 -05:00
|
|
|
// SetPublicDashboardOrgIdOnContext Adds orgId to context based on org of public dashboard
|
2023-01-27 01:50:36 -06:00
|
|
|
func SetPublicDashboardOrgIdOnContext(publicDashboardService publicdashboards.Service) func(c *contextmodel.ReqContext) {
|
|
|
|
return func(c *contextmodel.ReqContext) {
|
2022-10-06 15:35:19 -05:00
|
|
|
accessToken, ok := web.Params(c.Req)[":accessToken"]
|
2023-01-27 08:20:22 -06:00
|
|
|
if !ok || !validation.IsValidAccessToken(accessToken) {
|
2022-10-06 15:35:19 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get public dashboard
|
2022-10-25 19:40:42 -05:00
|
|
|
orgId, err := publicDashboardService.GetOrgIdByAccessToken(c.Req.Context(), accessToken)
|
2022-10-06 15:35:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
2022-09-21 11:29:27 -05:00
|
|
|
}
|
2022-10-06 15:35:19 -05:00
|
|
|
|
|
|
|
c.OrgID = orgId
|
2022-06-02 15:57:55 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-19 09:50:37 -05:00
|
|
|
|
2022-10-20 14:43:33 -05:00
|
|
|
// SetPublicDashboardFlag Adds public dashboard flag on context
|
2023-01-27 01:50:36 -06:00
|
|
|
func SetPublicDashboardFlag(c *contextmodel.ReqContext) {
|
2022-10-06 15:35:19 -05:00
|
|
|
c.IsPublicDashboardView = true
|
|
|
|
}
|
|
|
|
|
2022-10-20 14:43:33 -05:00
|
|
|
// RequiresExistingAccessToken Middleware to enforce that a public dashboards exists before continuing to handler. This
|
|
|
|
// method will query the database to ensure that it exists.
|
|
|
|
// Use when we want to enforce a public dashboard is valid on an endpoint we do not maintain
|
2023-01-27 01:50:36 -06:00
|
|
|
func RequiresExistingAccessToken(publicDashboardService publicdashboards.Service) func(c *contextmodel.ReqContext) {
|
|
|
|
return func(c *contextmodel.ReqContext) {
|
2022-08-10 12:14:48 -05:00
|
|
|
accessToken, ok := web.Params(c.Req)[":accessToken"]
|
|
|
|
|
2022-10-06 15:35:19 -05:00
|
|
|
if !ok {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "No access token provided", nil)
|
2022-08-10 12:14:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-27 08:20:22 -06:00
|
|
|
if !validation.IsValidAccessToken(accessToken) {
|
2022-10-06 15:35:19 -05:00
|
|
|
c.JsonApiErr(http.StatusBadRequest, "Invalid access token", nil)
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:14:48 -05:00
|
|
|
// Check that the access token references an enabled public dashboard
|
2022-10-25 19:40:42 -05:00
|
|
|
exists, err := publicDashboardService.ExistsEnabledByAccessToken(c.Req.Context(), accessToken)
|
2022-08-10 12:14:48 -05:00
|
|
|
if err != nil {
|
2022-10-06 15:35:19 -05:00
|
|
|
c.JsonApiErr(http.StatusInternalServerError, "Failed to query access token", nil)
|
2022-08-10 12:14:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !exists {
|
2022-10-06 15:35:19 -05:00
|
|
|
c.JsonApiErr(http.StatusNotFound, "Public dashboard not found", nil)
|
2022-08-10 12:14:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func CountPublicDashboardRequest() func(c *contextmodel.ReqContext) {
|
|
|
|
return func(c *contextmodel.ReqContext) {
|
2022-07-19 09:50:37 -05:00
|
|
|
metrics.MPublicDashboardRequestCount.Inc()
|
|
|
|
}
|
|
|
|
}
|