2014-10-05 14:13:01 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2015-01-27 05:05:23 -06:00
|
|
|
"net/url"
|
2015-01-14 02:33:34 -06:00
|
|
|
"strings"
|
2014-11-20 08:19:44 -06:00
|
|
|
|
2019-03-05 05:41:01 -06:00
|
|
|
macaron "gopkg.in/macaron.v1"
|
2015-01-14 07:25:12 -06:00
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-06-28 05:08:32 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-10-05 14:13:01 -05:00
|
|
|
)
|
|
|
|
|
2015-01-15 05:16:54 -06:00
|
|
|
type AuthOptions struct {
|
2015-01-16 07:32:18 -06:00
|
|
|
ReqGrafanaAdmin bool
|
|
|
|
ReqSignedIn bool
|
2015-01-15 05:16:54 -06:00
|
|
|
}
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func getApiKey(c *models.ReqContext) string {
|
2015-01-15 05:16:54 -06:00
|
|
|
header := c.Req.Header.Get("Authorization")
|
|
|
|
parts := strings.SplitN(header, " ", 2)
|
2015-04-01 08:23:26 -05:00
|
|
|
if len(parts) == 2 && parts[0] == "Bearer" {
|
2015-01-27 01:26:11 -06:00
|
|
|
key := parts[1]
|
|
|
|
return key
|
2014-10-05 14:13:01 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 05:08:32 -05:00
|
|
|
username, password, err := util.DecodeBasicAuthHeader(header)
|
|
|
|
if err == nil && username == "api_key" {
|
|
|
|
return password
|
|
|
|
}
|
|
|
|
|
2015-01-16 09:15:35 -06:00
|
|
|
return ""
|
2014-10-05 14:13:01 -05:00
|
|
|
}
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func accessForbidden(c *models.ReqContext) {
|
2015-01-14 07:25:12 -06:00
|
|
|
if c.IsApiRequest() {
|
2015-09-08 03:46:31 -05:00
|
|
|
c.JsonApiErr(403, "Permission denied", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-05 11:17:47 -06:00
|
|
|
c.Redirect(setting.AppSubUrl + "/")
|
2015-09-08 03:46:31 -05:00
|
|
|
}
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func notAuthorized(c *models.ReqContext) {
|
2015-09-08 03:46:31 -05:00
|
|
|
if c.IsApiRequest() {
|
|
|
|
c.JsonApiErr(401, "Unauthorized", nil)
|
2015-01-27 05:05:23 -06:00
|
|
|
return
|
2015-01-14 07:25:12 -06:00
|
|
|
}
|
|
|
|
|
2020-03-11 04:04:48 -05:00
|
|
|
redirectTo := c.Req.RequestURI
|
|
|
|
if setting.AppSubUrl != "" && !strings.HasPrefix(redirectTo, setting.AppSubUrl) {
|
|
|
|
redirectTo = setting.AppSubUrl + c.Req.RequestURI
|
|
|
|
}
|
|
|
|
WriteCookie(c.Resp, "redirect_to", url.QueryEscape(redirectTo), 0, newCookieOptions)
|
2018-02-15 03:56:29 -06:00
|
|
|
|
2015-01-04 14:03:40 -06:00
|
|
|
c.Redirect(setting.AppSubUrl + "/login")
|
2014-10-05 14:13:01 -05:00
|
|
|
}
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func EnsureEditorOrViewerCanEdit(c *models.ReqContext) {
|
|
|
|
if !c.SignedInUser.HasRole(models.ROLE_EDITOR) && !setting.ViewersCanEdit {
|
2019-03-05 05:41:01 -06:00
|
|
|
accessForbidden(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func RoleAuth(roles ...models.RoleType) macaron.Handler {
|
|
|
|
return func(c *models.ReqContext) {
|
2015-01-16 08:28:44 -06:00
|
|
|
ok := false
|
|
|
|
for _, role := range roles {
|
2015-02-23 13:07:49 -06:00
|
|
|
if role == c.OrgRole {
|
2015-01-16 08:28:44 -06:00
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
2015-09-08 03:46:31 -05:00
|
|
|
accessForbidden(c)
|
2015-01-16 08:28:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:16:54 -06:00
|
|
|
func Auth(options *AuthOptions) macaron.Handler {
|
2020-02-28 05:50:58 -06:00
|
|
|
return func(c *models.ReqContext) {
|
2015-09-08 03:46:31 -05:00
|
|
|
if !c.IsSignedIn && options.ReqSignedIn && !c.AllowAnonymous {
|
|
|
|
notAuthorized(c)
|
2015-01-15 05:16:54 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-14 02:33:34 -06:00
|
|
|
|
2015-09-08 03:46:31 -05:00
|
|
|
if !c.IsGrafanaAdmin && options.ReqGrafanaAdmin {
|
|
|
|
accessForbidden(c)
|
2015-01-15 05:16:54 -06:00
|
|
|
return
|
2015-01-14 02:33:34 -06:00
|
|
|
}
|
2014-10-05 14:13:01 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 03:27:38 -06:00
|
|
|
|
2019-03-14 04:02:46 -05:00
|
|
|
// AdminOrFeatureEnabled creates a middleware that allows access
|
|
|
|
// if the signed in user is either an Org Admin or if the
|
|
|
|
// feature flag is enabled.
|
|
|
|
// Intended for when feature flags open up access to APIs that
|
|
|
|
// are otherwise only available to admins.
|
|
|
|
func AdminOrFeatureEnabled(enabled bool) macaron.Handler {
|
2020-02-28 05:50:58 -06:00
|
|
|
return func(c *models.ReqContext) {
|
|
|
|
if c.OrgRole == models.ROLE_ADMIN {
|
2019-03-13 04:38:09 -05:00
|
|
|
return
|
2019-03-06 03:27:38 -06:00
|
|
|
}
|
|
|
|
|
2019-03-13 04:38:09 -05:00
|
|
|
if !enabled {
|
2019-03-06 03:27:38 -06:00
|
|
|
accessForbidden(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 08:15:46 -05:00
|
|
|
|
|
|
|
func SnapshotPublicModeOrSignedIn() macaron.Handler {
|
2020-02-28 05:50:58 -06:00
|
|
|
return func(c *models.ReqContext) {
|
2019-09-02 08:15:46 -05:00
|
|
|
if setting.SnapshotPublicMode {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.Invoke(ReqSignedIn)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to invoke required signed in middleware", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|