middleware.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package middleware
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "github.com/Unknwon/macaron"
  7. "github.com/macaron-contrib/session"
  8. "github.com/grafana/grafana/pkg/bus"
  9. "github.com/grafana/grafana/pkg/components/apikeygen"
  10. "github.com/grafana/grafana/pkg/log"
  11. m "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/setting"
  13. )
  14. type Context struct {
  15. *macaron.Context
  16. *m.SignedInUser
  17. Session session.Store
  18. IsSignedIn bool
  19. HasAnonymousAccess bool
  20. }
  21. func GetContextHandler() macaron.Handler {
  22. return func(c *macaron.Context, sess session.Store) {
  23. ctx := &Context{
  24. Context: c,
  25. Session: sess,
  26. SignedInUser: &m.SignedInUser{},
  27. IsSignedIn: false,
  28. HasAnonymousAccess: false,
  29. }
  30. // try get account id from request
  31. if userId := getRequestUserId(ctx); userId != 0 {
  32. query := m.GetSignedInUserQuery{UserId: userId}
  33. if err := bus.Dispatch(&query); err != nil {
  34. log.Error(3, "Failed to get user by id, %v, %v", userId, err)
  35. } else {
  36. ctx.SignedInUser = query.Result
  37. ctx.IsSignedIn = true
  38. }
  39. } else if keyString := getApiKey(ctx); keyString != "" {
  40. // base64 decode key
  41. decoded, err := apikeygen.Decode(keyString)
  42. if err != nil {
  43. ctx.JsonApiErr(401, "Invalid API key", err)
  44. return
  45. }
  46. // fetch key
  47. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  48. if err := bus.Dispatch(&keyQuery); err != nil {
  49. ctx.JsonApiErr(401, "Invalid API key", err)
  50. return
  51. } else {
  52. apikey := keyQuery.Result
  53. // validate api key
  54. if !apikeygen.IsValid(decoded, apikey.Key) {
  55. ctx.JsonApiErr(401, "Invalid API key", err)
  56. return
  57. }
  58. ctx.IsSignedIn = true
  59. ctx.SignedInUser = &m.SignedInUser{}
  60. // TODO: fix this
  61. ctx.OrgRole = apikey.Role
  62. ctx.ApiKeyId = apikey.Id
  63. ctx.OrgId = apikey.OrgId
  64. }
  65. } else if setting.AnonymousEnabled {
  66. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  67. if err := bus.Dispatch(&orgQuery); err != nil {
  68. if err == m.ErrOrgNotFound {
  69. log.Error(3, "Anonymous access organization name does not exist", nil)
  70. }
  71. } else {
  72. ctx.IsSignedIn = false
  73. ctx.HasAnonymousAccess = true
  74. ctx.SignedInUser = &m.SignedInUser{}
  75. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  76. ctx.OrgId = orgQuery.Result.Id
  77. ctx.OrgName = orgQuery.Result.Name
  78. }
  79. }
  80. c.Map(ctx)
  81. }
  82. }
  83. // Handle handles and logs error by given status.
  84. func (ctx *Context) Handle(status int, title string, err error) {
  85. if err != nil {
  86. log.Error(4, "%s: %v", title, err)
  87. if setting.Env != setting.PROD {
  88. ctx.Data["ErrorMsg"] = err
  89. }
  90. }
  91. ctx.Data["Title"] = title
  92. ctx.HTML(status, strconv.Itoa(status))
  93. }
  94. func (ctx *Context) JsonOK(message string) {
  95. resp := make(map[string]interface{})
  96. resp["message"] = message
  97. ctx.JSON(200, resp)
  98. }
  99. func (ctx *Context) IsApiRequest() bool {
  100. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  101. }
  102. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  103. resp := make(map[string]interface{})
  104. if err != nil {
  105. log.Error(4, "%s: %v", message, err)
  106. if setting.Env != setting.PROD {
  107. resp["error"] = err.Error()
  108. }
  109. }
  110. switch status {
  111. case 404:
  112. resp["message"] = "Not Found"
  113. case 500:
  114. resp["message"] = "Internal Server Error"
  115. }
  116. if message != "" {
  117. resp["message"] = message
  118. }
  119. ctx.JSON(status, resp)
  120. }
  121. func (ctx *Context) JsonBody(model interface{}) bool {
  122. b, _ := ctx.Req.Body().Bytes()
  123. err := json.Unmarshal(b, &model)
  124. return err == nil
  125. }