middleware.go 6.0 KB

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