middleware.go 6.5 KB

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