middleware.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "github.com/grafana/grafana/pkg/util"
  13. )
  14. type Context struct {
  15. *macaron.Context
  16. *m.SignedInUser
  17. Session SessionStore
  18. IsSignedIn bool
  19. AllowAnonymous bool
  20. }
  21. func GetContextHandler() macaron.Handler {
  22. return func(c *macaron.Context) {
  23. ctx := &Context{
  24. Context: c,
  25. SignedInUser: &m.SignedInUser{},
  26. Session: GetSession(),
  27. IsSignedIn: false,
  28. AllowAnonymous: false,
  29. }
  30. // the order in which these are tested are important
  31. // look for api key in Authorization header first
  32. // then init session and look for userId in session
  33. // then look for api key in session (special case for render calls via api)
  34. // then test if anonymous access is enabled
  35. if initContextWithApiKey(ctx) ||
  36. initContextWithBasicAuth(ctx) ||
  37. initContextWithAuthProxy(ctx) ||
  38. initContextWithUserSessionCookie(ctx) ||
  39. initContextWithApiKeyFromSession(ctx) ||
  40. initContextWithAnonymousUser(ctx) {
  41. }
  42. c.Map(ctx)
  43. }
  44. }
  45. func initContextWithAnonymousUser(ctx *Context) bool {
  46. if !setting.AnonymousEnabled {
  47. return false
  48. }
  49. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  50. if err := bus.Dispatch(&orgQuery); err != nil {
  51. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  52. return false
  53. } else {
  54. ctx.IsSignedIn = false
  55. ctx.AllowAnonymous = true
  56. ctx.SignedInUser = &m.SignedInUser{}
  57. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  58. ctx.OrgId = orgQuery.Result.Id
  59. ctx.OrgName = orgQuery.Result.Name
  60. return true
  61. }
  62. }
  63. func initContextWithUserSessionCookie(ctx *Context) bool {
  64. // initialize session
  65. if err := ctx.Session.Start(ctx); err != nil {
  66. log.Error(3, "Failed to start session", err)
  67. return false
  68. }
  69. var userId int64
  70. if userId = getRequestUserId(ctx); userId == 0 {
  71. return false
  72. }
  73. query := m.GetSignedInUserQuery{UserId: userId}
  74. if err := bus.Dispatch(&query); err != nil {
  75. log.Error(3, "Failed to get user with id %v", userId)
  76. return false
  77. } else {
  78. ctx.SignedInUser = query.Result
  79. ctx.IsSignedIn = true
  80. return true
  81. }
  82. }
  83. func initContextWithApiKey(ctx *Context) bool {
  84. var keyString string
  85. if keyString = getApiKey(ctx); keyString == "" {
  86. return false
  87. }
  88. // base64 decode key
  89. decoded, err := apikeygen.Decode(keyString)
  90. if err != nil {
  91. ctx.JsonApiErr(401, "Invalid API key", err)
  92. return true
  93. }
  94. // fetch key
  95. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  96. if err := bus.Dispatch(&keyQuery); err != nil {
  97. ctx.JsonApiErr(401, "Invalid API key", err)
  98. return true
  99. } else {
  100. apikey := keyQuery.Result
  101. // validate api key
  102. if !apikeygen.IsValid(decoded, apikey.Key) {
  103. ctx.JsonApiErr(401, "Invalid API key", err)
  104. return true
  105. }
  106. ctx.IsSignedIn = true
  107. ctx.SignedInUser = &m.SignedInUser{}
  108. ctx.OrgRole = apikey.Role
  109. ctx.ApiKeyId = apikey.Id
  110. ctx.OrgId = apikey.OrgId
  111. return true
  112. }
  113. }
  114. func initContextWithBasicAuth(ctx *Context) bool {
  115. if !setting.BasicAuthEnabled {
  116. return false
  117. }
  118. header := ctx.Req.Header.Get("Authorization")
  119. if header == "" {
  120. return false
  121. }
  122. username, password, err := util.DecodeBasicAuthHeader(header)
  123. if err != nil {
  124. ctx.JsonApiErr(401, "Invalid Basic Auth Header", err)
  125. return true
  126. }
  127. loginQuery := m.GetUserByLoginQuery{LoginOrEmail: username}
  128. if err := bus.Dispatch(&loginQuery); err != nil {
  129. ctx.JsonApiErr(401, "Basic auth failed", err)
  130. return true
  131. }
  132. user := loginQuery.Result
  133. // validate password
  134. if util.EncodePassword(password, user.Salt) != user.Password {
  135. ctx.JsonApiErr(401, "Invalid username or password", nil)
  136. return true
  137. }
  138. query := m.GetSignedInUserQuery{UserId: user.Id}
  139. if err := bus.Dispatch(&query); err != nil {
  140. ctx.JsonApiErr(401, "Authentication error", err)
  141. return true
  142. } else {
  143. ctx.SignedInUser = query.Result
  144. ctx.IsSignedIn = true
  145. return true
  146. }
  147. }
  148. // special case for panel render calls with api key
  149. func initContextWithApiKeyFromSession(ctx *Context) bool {
  150. keyId := ctx.Session.Get(SESS_KEY_APIKEY)
  151. if keyId == nil {
  152. return false
  153. }
  154. keyQuery := m.GetApiKeyByIdQuery{ApiKeyId: keyId.(int64)}
  155. if err := bus.Dispatch(&keyQuery); err != nil {
  156. log.Error(3, "Failed to get api key by id", err)
  157. return false
  158. } else {
  159. apikey := keyQuery.Result
  160. ctx.IsSignedIn = true
  161. ctx.SignedInUser = &m.SignedInUser{}
  162. ctx.OrgRole = apikey.Role
  163. ctx.ApiKeyId = apikey.Id
  164. ctx.OrgId = apikey.OrgId
  165. return true
  166. }
  167. }
  168. // Handle handles and logs error by given status.
  169. func (ctx *Context) Handle(status int, title string, err error) {
  170. if err != nil {
  171. log.Error(4, "%s: %v", title, err)
  172. if setting.Env != setting.PROD {
  173. ctx.Data["ErrorMsg"] = err
  174. }
  175. }
  176. switch status {
  177. case 200:
  178. metrics.M_Page_Status_200.Inc(1)
  179. case 404:
  180. metrics.M_Page_Status_404.Inc(1)
  181. case 500:
  182. metrics.M_Page_Status_500.Inc(1)
  183. }
  184. ctx.Data["Title"] = title
  185. ctx.HTML(status, strconv.Itoa(status))
  186. }
  187. func (ctx *Context) JsonOK(message string) {
  188. resp := make(map[string]interface{})
  189. resp["message"] = message
  190. ctx.JSON(200, resp)
  191. }
  192. func (ctx *Context) IsApiRequest() bool {
  193. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  194. }
  195. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  196. resp := make(map[string]interface{})
  197. if err != nil {
  198. log.Error(4, "%s: %v", message, err)
  199. if setting.Env != setting.PROD {
  200. resp["error"] = err.Error()
  201. }
  202. }
  203. switch status {
  204. case 404:
  205. metrics.M_Api_Status_404.Inc(1)
  206. resp["message"] = "Not Found"
  207. case 500:
  208. metrics.M_Api_Status_500.Inc(1)
  209. resp["message"] = "Internal Server Error"
  210. }
  211. if message != "" {
  212. resp["message"] = message
  213. }
  214. ctx.JSON(status, resp)
  215. }
  216. func (ctx *Context) HasUserRole(role m.RoleType) bool {
  217. return ctx.OrgRole.Includes(role)
  218. }