| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package middleware
- import (
- "strconv"
- "strings"
- "github.com/Unknwon/macaron"
- "github.com/grafana/grafana/pkg/bus"
- "github.com/grafana/grafana/pkg/components/apikeygen"
- "github.com/grafana/grafana/pkg/log"
- "github.com/grafana/grafana/pkg/metrics"
- m "github.com/grafana/grafana/pkg/models"
- "github.com/grafana/grafana/pkg/setting"
- )
- type Context struct {
- *macaron.Context
- *m.SignedInUser
- Session SessionStore
- IsSignedIn bool
- AllowAnonymous bool
- }
- func GetContextHandler() macaron.Handler {
- return func(c *macaron.Context) {
- ctx := &Context{
- Context: c,
- SignedInUser: &m.SignedInUser{},
- Session: GetSession(),
- IsSignedIn: false,
- AllowAnonymous: false,
- }
- // the order in which these are tested are important
- // look for api key in Authorization header first
- // then init session and look for userId in session
- // then look for api key in session (special case for render calls via api)
- // then test if anonymous access is enabled
- if initContextWithApiKey(ctx) ||
- initContextWithUserSessionCookie(ctx) ||
- initContextWithApiKeyFromSession(ctx) ||
- initContextWithAnonymousUser(ctx) {
- }
- c.Map(ctx)
- }
- }
- 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 {
- 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{}
- ctx.OrgRole = apikey.Role
- ctx.ApiKeyId = apikey.Id
- ctx.OrgId = apikey.OrgId
- return true
- }
- }
- // special case for panel render calls with api key
- func initContextWithApiKeyFromSession(ctx *Context) bool {
- keyId := ctx.Session.Get(SESS_KEY_APIKEY)
- if keyId == nil {
- return false
- }
- keyQuery := m.GetApiKeyByIdQuery{ApiKeyId: keyId.(int64)}
- if err := bus.Dispatch(&keyQuery); err != nil {
- log.Error(3, "Failed to get api key by id", err)
- return false
- } else {
- apikey := keyQuery.Result
- ctx.IsSignedIn = true
- ctx.SignedInUser = &m.SignedInUser{}
- ctx.OrgRole = apikey.Role
- ctx.ApiKeyId = apikey.Id
- ctx.OrgId = apikey.OrgId
- return true
- }
- }
- // 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)
- if setting.Env != setting.PROD {
- ctx.Data["ErrorMsg"] = err
- }
- }
- 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)
- }
- ctx.Data["Title"] = title
- ctx.HTML(status, strconv.Itoa(status))
- }
- func (ctx *Context) JsonOK(message string) {
- resp := make(map[string]interface{})
- resp["message"] = message
- ctx.JSON(200, resp)
- }
- func (ctx *Context) IsApiRequest() bool {
- return strings.HasPrefix(ctx.Req.URL.Path, "/api")
- }
- func (ctx *Context) JsonApiErr(status int, message string, err error) {
- resp := make(map[string]interface{})
- if err != nil {
- log.Error(4, "%s: %v", message, err)
- if setting.Env != setting.PROD {
- resp["error"] = err.Error()
- }
- }
- switch status {
- case 404:
- resp["message"] = "Not Found"
- metrics.M_Api_Status_500.Inc(1)
- case 500:
- metrics.M_Api_Status_404.Inc(1)
- resp["message"] = "Internal Server Error"
- }
- if message != "" {
- resp["message"] = message
- }
- ctx.JSON(status, resp)
- }
|