middleware.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package middleware
  2. import (
  3. "strconv"
  4. "gopkg.in/macaron.v1"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/apikeygen"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/session"
  10. "github.com/grafana/grafana/pkg/setting"
  11. "github.com/grafana/grafana/pkg/util"
  12. )
  13. func GetContextHandler() macaron.Handler {
  14. return func(c *macaron.Context) {
  15. ctx := &m.ReqContext{
  16. Context: c,
  17. SignedInUser: &m.SignedInUser{},
  18. Session: session.GetSession(),
  19. IsSignedIn: false,
  20. AllowAnonymous: false,
  21. Logger: log.New("context"),
  22. }
  23. orgId := int64(0)
  24. orgIdHeader := ctx.Req.Header.Get("X-Grafana-Org-Id")
  25. if orgIdHeader != "" {
  26. orgId, _ = strconv.ParseInt(orgIdHeader, 10, 64)
  27. }
  28. // the order in which these are tested are important
  29. // look for api key in Authorization header first
  30. // then init session and look for userId in session
  31. // then look for api key in session (special case for render calls via api)
  32. // then test if anonymous access is enabled
  33. if initContextWithRenderAuth(ctx) ||
  34. initContextWithApiKey(ctx) ||
  35. initContextWithBasicAuth(ctx, orgId) ||
  36. initContextWithAuthProxy(ctx, orgId) ||
  37. initContextWithUserSessionCookie(ctx, orgId) ||
  38. initContextWithAnonymousUser(ctx) {
  39. }
  40. ctx.Logger = log.New("context", "userId", ctx.UserId, "orgId", ctx.OrgId, "uname", ctx.Login)
  41. ctx.Data["ctx"] = ctx
  42. c.Map(ctx)
  43. // update last seen at
  44. // update last seen every 5min
  45. if ctx.ShouldUpdateLastSeenAt() {
  46. ctx.Logger.Debug("Updating last user_seen_at", "user_id", ctx.UserId)
  47. if err := bus.Dispatch(&m.UpdateUserLastSeenAtCommand{UserId: ctx.UserId}); err != nil {
  48. ctx.Logger.Error("Failed to update last_seen_at", "error", err)
  49. }
  50. }
  51. }
  52. }
  53. func initContextWithAnonymousUser(ctx *m.ReqContext) bool {
  54. if !setting.AnonymousEnabled {
  55. return false
  56. }
  57. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  58. if err := bus.Dispatch(&orgQuery); err != nil {
  59. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  60. return false
  61. }
  62. ctx.IsSignedIn = false
  63. ctx.AllowAnonymous = true
  64. ctx.SignedInUser = &m.SignedInUser{IsAnonymous: true}
  65. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  66. ctx.OrgId = orgQuery.Result.Id
  67. ctx.OrgName = orgQuery.Result.Name
  68. return true
  69. }
  70. func initContextWithUserSessionCookie(ctx *m.ReqContext, orgId int64) bool {
  71. // initialize session
  72. if err := ctx.Session.Start(ctx.Context); err != nil {
  73. ctx.Logger.Error("Failed to start session", "error", err)
  74. return false
  75. }
  76. var userId int64
  77. if userId = getRequestUserId(ctx); userId == 0 {
  78. return false
  79. }
  80. query := m.GetSignedInUserQuery{UserId: userId, OrgId: orgId}
  81. if err := bus.Dispatch(&query); err != nil {
  82. ctx.Logger.Error("Failed to get user with id", "userId", userId, "error", err)
  83. return false
  84. }
  85. ctx.SignedInUser = query.Result
  86. ctx.IsSignedIn = true
  87. return true
  88. }
  89. func initContextWithApiKey(ctx *m.ReqContext) bool {
  90. var keyString string
  91. if keyString = getApiKey(ctx); keyString == "" {
  92. return false
  93. }
  94. // base64 decode key
  95. decoded, err := apikeygen.Decode(keyString)
  96. if err != nil {
  97. ctx.JsonApiErr(401, "Invalid API key", err)
  98. return true
  99. }
  100. // fetch key
  101. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  102. if err := bus.Dispatch(&keyQuery); err != nil {
  103. ctx.JsonApiErr(401, "Invalid API key", err)
  104. return true
  105. }
  106. apikey := keyQuery.Result
  107. // validate api key
  108. if !apikeygen.IsValid(decoded, apikey.Key) {
  109. ctx.JsonApiErr(401, "Invalid API key", err)
  110. return true
  111. }
  112. ctx.IsSignedIn = true
  113. ctx.SignedInUser = &m.SignedInUser{}
  114. ctx.OrgRole = apikey.Role
  115. ctx.ApiKeyId = apikey.Id
  116. ctx.OrgId = apikey.OrgId
  117. return true
  118. }
  119. func initContextWithBasicAuth(ctx *m.ReqContext, orgId int64) 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. loginUserQuery := m.LoginUserQuery{Username: username, Password: password, User: user}
  139. if err := bus.Dispatch(&loginUserQuery); err != nil {
  140. ctx.JsonApiErr(401, "Invalid username or password", err)
  141. return true
  142. }
  143. query := m.GetSignedInUserQuery{UserId: user.Id, OrgId: orgId}
  144. if err := bus.Dispatch(&query); err != nil {
  145. ctx.JsonApiErr(401, "Authentication error", err)
  146. return true
  147. }
  148. ctx.SignedInUser = query.Result
  149. ctx.IsSignedIn = true
  150. return true
  151. }
  152. func AddDefaultResponseHeaders() macaron.Handler {
  153. return func(ctx *m.ReqContext) {
  154. if ctx.IsApiRequest() && ctx.Req.Method == "GET" {
  155. ctx.Resp.Header().Add("Cache-Control", "no-cache")
  156. ctx.Resp.Header().Add("Pragma", "no-cache")
  157. ctx.Resp.Header().Add("Expires", "-1")
  158. }
  159. }
  160. }