2014-10-06 03:13:01 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2015-01-27 19:05:23 +08:00
|
|
|
"net/url"
|
2015-01-14 16:33:34 +08:00
|
|
|
"strings"
|
2014-11-20 22:19:44 +08:00
|
|
|
|
2016-01-13 22:11:23 +08:00
|
|
|
"gopkg.in/macaron.v1"
|
2015-01-14 21:25:12 +08:00
|
|
|
|
2015-02-05 17:37:13 +08:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-10-06 03:13:01 +08:00
|
|
|
)
|
|
|
|
|
2015-01-15 19:16:54 +08:00
|
|
|
type AuthOptions struct {
|
2015-01-16 21:32:18 +08:00
|
|
|
ReqGrafanaAdmin bool
|
|
|
|
ReqSignedIn bool
|
2015-01-15 19:16:54 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 01:01:04 +08:00
|
|
|
func getRequestUserId(c *Context) int64 {
|
2015-01-29 19:10:34 +08:00
|
|
|
userId := c.Session.Get(SESS_KEY_USERID)
|
2014-10-06 03:13:01 +08:00
|
|
|
|
2015-01-20 01:01:04 +08:00
|
|
|
if userId != nil {
|
|
|
|
return userId.(int64)
|
2015-01-15 19:16:54 +08:00
|
|
|
}
|
2015-01-07 23:37:24 +08:00
|
|
|
|
2015-01-16 23:15:35 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-01-27 15:26:11 +08:00
|
|
|
func getApiKey(c *Context) string {
|
2015-01-15 19:16:54 +08:00
|
|
|
header := c.Req.Header.Get("Authorization")
|
|
|
|
parts := strings.SplitN(header, " ", 2)
|
2015-04-01 21:23:26 +08:00
|
|
|
if len(parts) == 2 && parts[0] == "Bearer" {
|
2015-01-27 15:26:11 +08:00
|
|
|
key := parts[1]
|
|
|
|
return key
|
2014-10-06 03:13:01 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 23:15:35 +08:00
|
|
|
return ""
|
2014-10-06 03:13:01 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:46:31 +08:00
|
|
|
func accessForbidden(c *Context) {
|
2015-01-14 21:25:12 +08:00
|
|
|
if c.IsApiRequest() {
|
2015-09-08 16:46:31 +08:00
|
|
|
c.JsonApiErr(403, "Permission denied", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:17:47 +08:00
|
|
|
c.Redirect(setting.AppSubUrl + "/")
|
2015-09-08 16:46:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func notAuthorized(c *Context) {
|
|
|
|
if c.IsApiRequest() {
|
|
|
|
c.JsonApiErr(401, "Unauthorized", nil)
|
2015-01-27 19:05:23 +08:00
|
|
|
return
|
2015-01-14 21:25:12 +08:00
|
|
|
}
|
|
|
|
|
2015-03-31 20:03:01 +08:00
|
|
|
c.SetCookie("redirect_to", url.QueryEscape(setting.AppSubUrl+c.Req.RequestURI), 0, setting.AppSubUrl+"/")
|
2015-01-05 04:03:40 +08:00
|
|
|
c.Redirect(setting.AppSubUrl + "/login")
|
2014-10-06 03:13:01 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 22:28:44 +08:00
|
|
|
func RoleAuth(roles ...m.RoleType) macaron.Handler {
|
|
|
|
return func(c *Context) {
|
|
|
|
ok := false
|
|
|
|
for _, role := range roles {
|
2015-02-24 03:07:49 +08:00
|
|
|
if role == c.OrgRole {
|
2015-01-16 22:28:44 +08:00
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
2015-09-08 16:46:31 +08:00
|
|
|
accessForbidden(c)
|
2015-01-16 22:28:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:16:54 +08:00
|
|
|
func Auth(options *AuthOptions) macaron.Handler {
|
|
|
|
return func(c *Context) {
|
2015-09-08 16:46:31 +08:00
|
|
|
if !c.IsSignedIn && options.ReqSignedIn && !c.AllowAnonymous {
|
|
|
|
notAuthorized(c)
|
2015-01-15 19:16:54 +08:00
|
|
|
return
|
|
|
|
}
|
2015-01-14 16:33:34 +08:00
|
|
|
|
2015-09-08 16:46:31 +08:00
|
|
|
if !c.IsGrafanaAdmin && options.ReqGrafanaAdmin {
|
|
|
|
accessForbidden(c)
|
2015-01-15 19:16:54 +08:00
|
|
|
return
|
2015-01-14 16:33:34 +08:00
|
|
|
}
|
2014-10-06 03:13:01 +08:00
|
|
|
}
|
|
|
|
}
|