middleware.go 5.4 KB

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