middleware.go 5.6 KB

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