2014-10-05 09:50:04 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2014-10-07 16:56:37 -05:00
|
|
|
"strconv"
|
2015-01-14 07:25:12 -06:00
|
|
|
"strings"
|
2014-10-05 09:50:04 -05:00
|
|
|
|
|
|
|
"github.com/Unknwon/macaron"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-02-26 10:23:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/apikeygen"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2015-03-22 14:14:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-10-05 09:50:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
*macaron.Context
|
2015-01-16 09:17:35 -06:00
|
|
|
*m.SignedInUser
|
2015-01-16 07:32:18 -06:00
|
|
|
|
2015-04-07 12:21:14 -05:00
|
|
|
Session SessionStore
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-03-11 11:34:11 -05:00
|
|
|
IsSignedIn bool
|
|
|
|
AllowAnonymous bool
|
2014-10-06 14:31:54 -05:00
|
|
|
}
|
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
func GetContextHandler() macaron.Handler {
|
2015-04-07 12:21:14 -05:00
|
|
|
return func(c *macaron.Context) {
|
2014-10-05 09:50:04 -05:00
|
|
|
ctx := &Context{
|
2015-03-11 11:34:11 -05:00
|
|
|
Context: c,
|
|
|
|
SignedInUser: &m.SignedInUser{},
|
2015-04-07 12:21:14 -05:00
|
|
|
Session: GetSession(),
|
2015-03-11 11:34:11 -05:00
|
|
|
IsSignedIn: false,
|
|
|
|
AllowAnonymous: false,
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 12:21:14 -05:00
|
|
|
if initContextWithApiKey(ctx) ||
|
|
|
|
initContextWithUserSessionCookie(ctx) ||
|
|
|
|
initContextWithAnonymousUser(ctx) {
|
2015-01-15 05:16:54 -06:00
|
|
|
}
|
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
c.Map(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 12:21:14 -05:00
|
|
|
func initContextWithAnonymousUser(ctx *Context) bool {
|
|
|
|
if !setting.AnonymousEnabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
|
|
|
|
if err := bus.Dispatch(&orgQuery); err != nil {
|
|
|
|
log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
ctx.IsSignedIn = false
|
|
|
|
ctx.AllowAnonymous = true
|
|
|
|
ctx.SignedInUser = &m.SignedInUser{}
|
|
|
|
ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
|
|
|
|
ctx.OrgId = orgQuery.Result.Id
|
|
|
|
ctx.OrgName = orgQuery.Result.Name
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContextWithUserSessionCookie(ctx *Context) bool {
|
|
|
|
// initialize session
|
|
|
|
if err := ctx.Session.Start(ctx); err != nil {
|
|
|
|
log.Error(3, "Failed to start session", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var userId int64
|
|
|
|
if userId = getRequestUserId(ctx); userId == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
query := m.GetSignedInUserQuery{UserId: userId}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
log.Error(3, "Failed to get user by id, %v, %v", userId, err)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
ctx.SignedInUser = query.Result
|
|
|
|
ctx.IsSignedIn = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContextWithApiKey(ctx *Context) bool {
|
|
|
|
var keyString string
|
|
|
|
if keyString = getApiKey(ctx); keyString == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// base64 decode key
|
|
|
|
decoded, err := apikeygen.Decode(keyString)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// fetch key
|
|
|
|
keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
|
|
|
|
if err := bus.Dispatch(&keyQuery); err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
apikey := keyQuery.Result
|
|
|
|
|
|
|
|
// validate api key
|
|
|
|
if !apikeygen.IsValid(decoded, apikey.Key) {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.IsSignedIn = true
|
|
|
|
ctx.SignedInUser = &m.SignedInUser{}
|
|
|
|
|
|
|
|
// TODO: fix this
|
|
|
|
ctx.OrgRole = apikey.Role
|
|
|
|
ctx.ApiKeyId = apikey.Id
|
|
|
|
ctx.OrgId = apikey.OrgId
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
// Handle handles and logs error by given status.
|
|
|
|
func (ctx *Context) Handle(status int, title string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "%s: %v", title, err)
|
2015-01-29 08:46:54 -06:00
|
|
|
if setting.Env != setting.PROD {
|
2014-10-05 09:50:04 -05:00
|
|
|
ctx.Data["ErrorMsg"] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:14:00 -05:00
|
|
|
switch status {
|
|
|
|
case 200:
|
|
|
|
metrics.M_Page_Status_200.Inc(1)
|
|
|
|
case 404:
|
|
|
|
metrics.M_Page_Status_404.Inc(1)
|
|
|
|
case 500:
|
|
|
|
metrics.M_Page_Status_500.Inc(1)
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:23:24 -06:00
|
|
|
ctx.Data["Title"] = title
|
2014-10-07 16:56:37 -05:00
|
|
|
ctx.HTML(status, strconv.Itoa(status))
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
2014-10-05 14:13:01 -05:00
|
|
|
|
2014-12-16 20:09:54 -06:00
|
|
|
func (ctx *Context) JsonOK(message string) {
|
|
|
|
resp := make(map[string]interface{})
|
|
|
|
|
|
|
|
resp["message"] = message
|
|
|
|
|
|
|
|
ctx.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
func (ctx *Context) IsApiRequest() bool {
|
|
|
|
return strings.HasPrefix(ctx.Req.URL.Path, "/api")
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:54:38 -05:00
|
|
|
func (ctx *Context) JsonApiErr(status int, message string, err error) {
|
2014-10-06 14:31:54 -05:00
|
|
|
resp := make(map[string]interface{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "%s: %v", message, err)
|
2014-12-16 05:04:08 -06:00
|
|
|
if setting.Env != setting.PROD {
|
|
|
|
resp["error"] = err.Error()
|
2014-10-06 14:31:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case 404:
|
|
|
|
resp["message"] = "Not Found"
|
2015-03-22 14:14:00 -05:00
|
|
|
metrics.M_Api_Status_500.Inc(1)
|
2014-10-06 14:31:54 -05:00
|
|
|
case 500:
|
2015-03-22 14:14:00 -05:00
|
|
|
metrics.M_Api_Status_404.Inc(1)
|
2014-10-06 14:31:54 -05:00
|
|
|
resp["message"] = "Internal Server Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
if message != "" {
|
|
|
|
resp["message"] = message
|
|
|
|
}
|
|
|
|
|
2014-11-24 03:17:13 -06:00
|
|
|
ctx.JSON(status, resp)
|
2014-10-06 14:31:54 -05:00
|
|
|
}
|