middleware.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package middleware
  2. import (
  3. "strconv"
  4. "strings"
  5. "github.com/Unknwon/macaron"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/apikeygen"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/metrics"
  10. m "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/setting"
  12. )
  13. type Context struct {
  14. *macaron.Context
  15. *m.SignedInUser
  16. Session SessionStore
  17. IsSignedIn bool
  18. AllowAnonymous bool
  19. }
  20. func GetContextHandler() macaron.Handler {
  21. return func(c *macaron.Context) {
  22. ctx := &Context{
  23. Context: c,
  24. SignedInUser: &m.SignedInUser{},
  25. Session: GetSession(),
  26. IsSignedIn: false,
  27. AllowAnonymous: false,
  28. }
  29. if initContextWithApiKey(ctx) ||
  30. initContextWithUserSessionCookie(ctx) ||
  31. initContextWithAnonymousUser(ctx) {
  32. }
  33. c.Map(ctx)
  34. }
  35. }
  36. func initContextWithAnonymousUser(ctx *Context) bool {
  37. if !setting.AnonymousEnabled {
  38. return false
  39. }
  40. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  41. if err := bus.Dispatch(&orgQuery); err != nil {
  42. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  43. return false
  44. } else {
  45. ctx.IsSignedIn = false
  46. ctx.AllowAnonymous = true
  47. ctx.SignedInUser = &m.SignedInUser{}
  48. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  49. ctx.OrgId = orgQuery.Result.Id
  50. ctx.OrgName = orgQuery.Result.Name
  51. return true
  52. }
  53. }
  54. func initContextWithUserSessionCookie(ctx *Context) bool {
  55. // initialize session
  56. if err := ctx.Session.Start(ctx); err != nil {
  57. log.Error(3, "Failed to start session", err)
  58. return false
  59. }
  60. var userId int64
  61. if userId = getRequestUserId(ctx); userId == 0 {
  62. return false
  63. }
  64. query := m.GetSignedInUserQuery{UserId: userId}
  65. if err := bus.Dispatch(&query); err != nil {
  66. log.Error(3, "Failed to get user by id, %v, %v", userId, err)
  67. return false
  68. } else {
  69. ctx.SignedInUser = query.Result
  70. ctx.IsSignedIn = true
  71. return true
  72. }
  73. }
  74. func initContextWithApiKey(ctx *Context) bool {
  75. var keyString string
  76. if keyString = getApiKey(ctx); keyString == "" {
  77. return false
  78. }
  79. // base64 decode key
  80. decoded, err := apikeygen.Decode(keyString)
  81. if err != nil {
  82. ctx.JsonApiErr(401, "Invalid API key", err)
  83. return true
  84. }
  85. // fetch key
  86. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  87. if err := bus.Dispatch(&keyQuery); err != nil {
  88. ctx.JsonApiErr(401, "Invalid API key", err)
  89. return true
  90. } else {
  91. apikey := keyQuery.Result
  92. // validate api key
  93. if !apikeygen.IsValid(decoded, apikey.Key) {
  94. ctx.JsonApiErr(401, "Invalid API key", err)
  95. return true
  96. }
  97. ctx.IsSignedIn = true
  98. ctx.SignedInUser = &m.SignedInUser{}
  99. // TODO: fix this
  100. ctx.OrgRole = apikey.Role
  101. ctx.ApiKeyId = apikey.Id
  102. ctx.OrgId = apikey.OrgId
  103. return true
  104. }
  105. }
  106. // Handle handles and logs error by given status.
  107. func (ctx *Context) Handle(status int, title string, err error) {
  108. if err != nil {
  109. log.Error(4, "%s: %v", title, err)
  110. if setting.Env != setting.PROD {
  111. ctx.Data["ErrorMsg"] = err
  112. }
  113. }
  114. switch status {
  115. case 200:
  116. metrics.M_Page_Status_200.Inc(1)
  117. case 404:
  118. metrics.M_Page_Status_404.Inc(1)
  119. case 500:
  120. metrics.M_Page_Status_500.Inc(1)
  121. }
  122. ctx.Data["Title"] = title
  123. ctx.HTML(status, strconv.Itoa(status))
  124. }
  125. func (ctx *Context) JsonOK(message string) {
  126. resp := make(map[string]interface{})
  127. resp["message"] = message
  128. ctx.JSON(200, resp)
  129. }
  130. func (ctx *Context) IsApiRequest() bool {
  131. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  132. }
  133. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  134. resp := make(map[string]interface{})
  135. if err != nil {
  136. log.Error(4, "%s: %v", message, err)
  137. if setting.Env != setting.PROD {
  138. resp["error"] = err.Error()
  139. }
  140. }
  141. switch status {
  142. case 404:
  143. resp["message"] = "Not Found"
  144. metrics.M_Api_Status_500.Inc(1)
  145. case 500:
  146. metrics.M_Api_Status_404.Inc(1)
  147. resp["message"] = "Internal Server Error"
  148. }
  149. if message != "" {
  150. resp["message"] = message
  151. }
  152. ctx.JSON(status, resp)
  153. }