middleware.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package middleware
  2. import (
  3. "net/http"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/components/apikeygen"
  9. "github.com/grafana/grafana/pkg/log"
  10. m "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/services/auth"
  12. "github.com/grafana/grafana/pkg/services/session"
  13. "github.com/grafana/grafana/pkg/setting"
  14. "github.com/grafana/grafana/pkg/util"
  15. macaron "gopkg.in/macaron.v1"
  16. )
  17. var (
  18. ReqGrafanaAdmin = Auth(&AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
  19. ReqSignedIn = Auth(&AuthOptions{ReqSignedIn: true})
  20. ReqEditorRole = RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
  21. ReqOrgAdmin = RoleAuth(m.ROLE_ADMIN)
  22. )
  23. func GetContextHandler(ats *auth.UserAuthTokenService) macaron.Handler {
  24. return func(c *macaron.Context) {
  25. ctx := &m.ReqContext{
  26. Context: c,
  27. SignedInUser: &m.SignedInUser{},
  28. Session: session.GetSession(),
  29. IsSignedIn: false,
  30. AllowAnonymous: false,
  31. SkipCache: 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. switch {
  45. case initContextWithRenderAuth(ctx):
  46. case initContextWithApiKey(ctx):
  47. case initContextWithBasicAuth(ctx, orgId):
  48. case initContextWithAuthProxy(ctx, orgId):
  49. //case initContextWithUserSessionCookie(ctx, orgId):
  50. case initContextWithToken(ctx, orgId, ats):
  51. case initContextWithAnonymousUser(ctx):
  52. }
  53. ctx.Logger = log.New("context", "userId", ctx.UserId, "orgId", ctx.OrgId, "uname", ctx.Login)
  54. ctx.Data["ctx"] = ctx
  55. c.Map(ctx)
  56. c.Next()
  57. //if signed in with token
  58. rotated, err := ats.RefreshToken(ctx.UserToken, ctx.RemoteAddr(), ctx.Req.UserAgent())
  59. if err != nil {
  60. ctx.Logger.Error("failed to rotate token", "error", err)
  61. return
  62. }
  63. if rotated {
  64. ctx.Logger.Info("new token", "unhashed token", ctx.UserToken.UnhashedToken)
  65. //c.SetCookie("grafana_session", url.QueryEscape(ctx.UserToken.UnhashedToken), nil, setting.AppSubUrl+"/", setting.Domain, false, true)
  66. // ctx.Resp.Header().Del("Set-Cookie")
  67. cookie := http.Cookie{
  68. Name: "grafana_session",
  69. Value: url.QueryEscape(ctx.UserToken.UnhashedToken),
  70. HttpOnly: true,
  71. MaxAge: int(time.Minute * 10),
  72. Domain: setting.Domain,
  73. Path: setting.AppSubUrl + "/",
  74. }
  75. ctx.Resp.Header().Add("Set-Cookie", cookie.String())
  76. }
  77. // update last seen every 5min
  78. if ctx.ShouldUpdateLastSeenAt() {
  79. ctx.Logger.Debug("Updating last user_seen_at", "user_id", ctx.UserId)
  80. if err := bus.Dispatch(&m.UpdateUserLastSeenAtCommand{UserId: ctx.UserId}); err != nil {
  81. ctx.Logger.Error("Failed to update last_seen_at", "error", err)
  82. }
  83. }
  84. }
  85. }
  86. func initContextWithAnonymousUser(ctx *m.ReqContext) bool {
  87. if !setting.AnonymousEnabled {
  88. return false
  89. }
  90. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  91. if err := bus.Dispatch(&orgQuery); err != nil {
  92. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  93. return false
  94. }
  95. ctx.IsSignedIn = false
  96. ctx.AllowAnonymous = true
  97. ctx.SignedInUser = &m.SignedInUser{IsAnonymous: true}
  98. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  99. ctx.OrgId = orgQuery.Result.Id
  100. ctx.OrgName = orgQuery.Result.Name
  101. return true
  102. }
  103. func initContextWithToken(ctx *m.ReqContext, orgID int64, ts *auth.UserAuthTokenService) bool {
  104. unhashedToken := ctx.GetCookie("grafana_session")
  105. if unhashedToken == "" {
  106. return false
  107. }
  108. user, err := ts.LookupToken(unhashedToken)
  109. if err != nil {
  110. ctx.Logger.Info("failed to look up user based on cookie")
  111. return false
  112. }
  113. query := m.GetSignedInUserQuery{UserId: user.UserId, OrgId: orgID}
  114. if err := bus.Dispatch(&query); err != nil {
  115. ctx.Logger.Error("Failed to get user with id", "userId", user.UserId, "error", err)
  116. return false
  117. }
  118. ctx.SignedInUser = query.Result
  119. ctx.IsSignedIn = true
  120. ctx.UserToken = user
  121. return true
  122. }
  123. func initContextWithUserSessionCookie(ctx *m.ReqContext, orgId int64) bool {
  124. // initialize session
  125. if err := ctx.Session.Start(ctx.Context); err != nil {
  126. ctx.Logger.Error("Failed to start session", "error", err)
  127. return false
  128. }
  129. var userId int64
  130. if userId = getRequestUserId(ctx); userId == 0 {
  131. return false
  132. }
  133. query := m.GetSignedInUserQuery{UserId: userId, OrgId: orgId}
  134. if err := bus.Dispatch(&query); err != nil {
  135. ctx.Logger.Error("Failed to get user with id", "userId", userId, "error", err)
  136. return false
  137. }
  138. ctx.SignedInUser = query.Result
  139. ctx.IsSignedIn = true
  140. return true
  141. }
  142. func initContextWithApiKey(ctx *m.ReqContext) bool {
  143. var keyString string
  144. if keyString = getApiKey(ctx); keyString == "" {
  145. return false
  146. }
  147. // base64 decode key
  148. decoded, err := apikeygen.Decode(keyString)
  149. if err != nil {
  150. ctx.JsonApiErr(401, "Invalid API key", err)
  151. return true
  152. }
  153. // fetch key
  154. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  155. if err := bus.Dispatch(&keyQuery); err != nil {
  156. ctx.JsonApiErr(401, "Invalid API key", err)
  157. return true
  158. }
  159. apikey := keyQuery.Result
  160. // validate api key
  161. if !apikeygen.IsValid(decoded, apikey.Key) {
  162. ctx.JsonApiErr(401, "Invalid API key", err)
  163. return true
  164. }
  165. ctx.IsSignedIn = true
  166. ctx.SignedInUser = &m.SignedInUser{}
  167. ctx.OrgRole = apikey.Role
  168. ctx.ApiKeyId = apikey.Id
  169. ctx.OrgId = apikey.OrgId
  170. return true
  171. }
  172. func initContextWithBasicAuth(ctx *m.ReqContext, orgId int64) bool {
  173. if !setting.BasicAuthEnabled {
  174. return false
  175. }
  176. header := ctx.Req.Header.Get("Authorization")
  177. if header == "" {
  178. return false
  179. }
  180. username, password, err := util.DecodeBasicAuthHeader(header)
  181. if err != nil {
  182. ctx.JsonApiErr(401, "Invalid Basic Auth Header", err)
  183. return true
  184. }
  185. loginQuery := m.GetUserByLoginQuery{LoginOrEmail: username}
  186. if err := bus.Dispatch(&loginQuery); err != nil {
  187. ctx.JsonApiErr(401, "Basic auth failed", err)
  188. return true
  189. }
  190. user := loginQuery.Result
  191. loginUserQuery := m.LoginUserQuery{Username: username, Password: password, User: user}
  192. if err := bus.Dispatch(&loginUserQuery); err != nil {
  193. ctx.JsonApiErr(401, "Invalid username or password", err)
  194. return true
  195. }
  196. query := m.GetSignedInUserQuery{UserId: user.Id, OrgId: orgId}
  197. if err := bus.Dispatch(&query); err != nil {
  198. ctx.JsonApiErr(401, "Authentication error", err)
  199. return true
  200. }
  201. ctx.SignedInUser = query.Result
  202. ctx.IsSignedIn = true
  203. return true
  204. }
  205. func AddDefaultResponseHeaders() macaron.Handler {
  206. return func(ctx *m.ReqContext) {
  207. if ctx.IsApiRequest() && ctx.Req.Method == "GET" {
  208. ctx.Resp.Header().Add("Cache-Control", "no-cache")
  209. ctx.Resp.Header().Add("Pragma", "no-cache")
  210. ctx.Resp.Header().Add("Expires", "-1")
  211. }
  212. }
  213. }