grafana/pkg/middleware/middleware.go

150 lines
3.5 KiB
Go
Raw Normal View History

2014-10-05 09:50:04 -05:00
package middleware
import (
2014-10-05 14:13:01 -05:00
"encoding/json"
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"
"github.com/macaron-contrib/session"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/apikeygen"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/log"
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
*m.SignedInUser
2014-10-05 09:50:04 -05:00
Session session.Store
2015-01-27 08:45:27 -06:00
IsSignedIn bool
HasAnonymousAccess bool
2014-10-06 14:31:54 -05:00
}
2014-10-05 09:50:04 -05:00
func GetContextHandler() macaron.Handler {
2014-10-05 14:13:01 -05:00
return func(c *macaron.Context, sess session.Store) {
2014-10-05 09:50:04 -05:00
ctx := &Context{
2015-01-27 08:45:27 -06:00
Context: c,
Session: sess,
SignedInUser: &m.SignedInUser{},
IsSignedIn: false,
HasAnonymousAccess: false,
2014-10-05 09:50:04 -05:00
}
// try get account id from request
if userId := getRequestUserId(ctx); userId != 0 {
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)
} else {
ctx.SignedInUser = query.Result
2015-01-27 08:45:27 -06:00
ctx.IsSignedIn = true
}
} else if keyString := getApiKey(ctx); keyString != "" {
// base64 decode key
decoded, err := apikeygen.Decode(keyString)
if err != nil {
ctx.JsonApiErr(401, "Invalid API key", err)
return
}
// fetch key
keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
2015-01-27 01:26:11 -06:00
if err := bus.Dispatch(&keyQuery); err != nil {
ctx.JsonApiErr(401, "Invalid API key", err)
return
} else {
apikey := keyQuery.Result
// validate api key
if !apikeygen.IsValid(decoded, apikey.Key) {
ctx.JsonApiErr(401, "Invalid API key", err)
return
}
ctx.IsSignedIn = true
ctx.SignedInUser = &m.SignedInUser{}
// TODO: fix this
ctx.OrgRole = apikey.Role
ctx.ApiKeyId = apikey.Id
ctx.OrgId = apikey.OrgId
}
2015-01-27 08:45:27 -06:00
} else if setting.AnonymousEnabled {
orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
if err := bus.Dispatch(&orgQuery); err != nil {
if err == m.ErrOrgNotFound {
log.Error(3, "Anonymous access organization name does not exist", nil)
2015-01-27 08:45:27 -06:00
}
} else {
ctx.IsSignedIn = false
ctx.HasAnonymousAccess = true
ctx.SignedInUser = &m.SignedInUser{}
ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
ctx.OrgId = orgQuery.Result.Id
ctx.OrgName = orgQuery.Result.Name
2015-01-27 08:45:27 -06:00
}
}
2014-10-05 09:50:04 -05:00
c.Map(ctx)
}
}
// 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
}
}
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
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"
case 500:
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
}
2014-10-05 14:13:01 -05:00
func (ctx *Context) JsonBody(model interface{}) bool {
2014-11-20 02:16:28 -06:00
b, _ := ctx.Req.Body().Bytes()
2014-10-05 14:13:01 -05:00
err := json.Unmarshal(b, &model)
return err == nil
}