middleware.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package middleware
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. macaron "gopkg.in/macaron.v1"
  10. "github.com/grafana/grafana/pkg/bus"
  11. "github.com/grafana/grafana/pkg/components/apikeygen"
  12. "github.com/grafana/grafana/pkg/infra/log"
  13. "github.com/grafana/grafana/pkg/infra/remotecache"
  14. m "github.com/grafana/grafana/pkg/models"
  15. "github.com/grafana/grafana/pkg/setting"
  16. "github.com/grafana/grafana/pkg/util"
  17. )
  18. var (
  19. ReqGrafanaAdmin = Auth(&AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
  20. ReqSignedIn = Auth(&AuthOptions{ReqSignedIn: true})
  21. ReqEditorRole = RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
  22. ReqOrgAdmin = RoleAuth(m.ROLE_ADMIN)
  23. )
  24. func GetContextHandler(
  25. ats m.UserTokenService,
  26. remoteCache *remotecache.RemoteCache,
  27. ) macaron.Handler {
  28. return func(c *macaron.Context) {
  29. ctx := &m.ReqContext{
  30. Context: c,
  31. SignedInUser: &m.SignedInUser{},
  32. IsSignedIn: false,
  33. AllowAnonymous: false,
  34. SkipCache: false,
  35. Logger: log.New("context"),
  36. }
  37. orgId := int64(0)
  38. orgIdHeader := ctx.Req.Header.Get("X-Grafana-Org-Id")
  39. if orgIdHeader != "" {
  40. orgId, _ = strconv.ParseInt(orgIdHeader, 10, 64)
  41. }
  42. // the order in which these are tested are important
  43. // look for api key in Authorization header first
  44. // then init session and look for userId in session
  45. // then look for api key in session (special case for render calls via api)
  46. // then test if anonymous access is enabled
  47. switch {
  48. case initContextWithRenderAuth(ctx):
  49. case initContextWithApiKey(ctx):
  50. case initContextWithBasicAuth(ctx, orgId):
  51. case initContextWithAuthProxy(remoteCache, ctx, orgId):
  52. case initContextWithToken(ats, ctx, orgId):
  53. case initContextWithAnonymousUser(ctx):
  54. }
  55. ctx.Logger = log.New("context", "userId", ctx.UserId, "orgId", ctx.OrgId, "uname", ctx.Login)
  56. ctx.Data["ctx"] = ctx
  57. c.Map(ctx)
  58. // update last seen every 5min
  59. if ctx.ShouldUpdateLastSeenAt() {
  60. ctx.Logger.Debug("Updating last user_seen_at", "user_id", ctx.UserId)
  61. if err := bus.Dispatch(&m.UpdateUserLastSeenAtCommand{UserId: ctx.UserId}); err != nil {
  62. ctx.Logger.Error("Failed to update last_seen_at", "error", err)
  63. }
  64. }
  65. }
  66. }
  67. func initContextWithAnonymousUser(ctx *m.ReqContext) bool {
  68. if !setting.AnonymousEnabled {
  69. return false
  70. }
  71. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  72. if err := bus.Dispatch(&orgQuery); err != nil {
  73. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  74. return false
  75. }
  76. ctx.IsSignedIn = false
  77. ctx.AllowAnonymous = true
  78. ctx.SignedInUser = &m.SignedInUser{IsAnonymous: true}
  79. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  80. ctx.OrgId = orgQuery.Result.Id
  81. ctx.OrgName = orgQuery.Result.Name
  82. return true
  83. }
  84. func initContextWithApiKey(ctx *m.ReqContext) bool {
  85. var keyString string
  86. if keyString = getApiKey(ctx); keyString == "" {
  87. return false
  88. }
  89. // base64 decode key
  90. decoded, err := apikeygen.Decode(keyString)
  91. if err != nil {
  92. ctx.JsonApiErr(401, "Invalid API key", err)
  93. return true
  94. }
  95. // fetch key
  96. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  97. if err := bus.Dispatch(&keyQuery); err != nil {
  98. ctx.JsonApiErr(401, "Invalid API key", err)
  99. return true
  100. }
  101. apikey := keyQuery.Result
  102. // validate api key
  103. if !apikeygen.IsValid(decoded, apikey.Key) {
  104. ctx.JsonApiErr(401, "Invalid API key", err)
  105. return true
  106. }
  107. ctx.IsSignedIn = true
  108. ctx.SignedInUser = &m.SignedInUser{}
  109. ctx.OrgRole = apikey.Role
  110. ctx.ApiKeyId = apikey.Id
  111. ctx.OrgId = apikey.OrgId
  112. return true
  113. }
  114. func initContextWithBasicAuth(ctx *m.ReqContext, orgId int64) bool {
  115. if !setting.BasicAuthEnabled {
  116. return false
  117. }
  118. header := ctx.Req.Header.Get("Authorization")
  119. if header == "" {
  120. return false
  121. }
  122. username, password, err := util.DecodeBasicAuthHeader(header)
  123. if err != nil {
  124. ctx.JsonApiErr(401, "Invalid Basic Auth Header", err)
  125. return true
  126. }
  127. loginQuery := m.GetUserByLoginQuery{LoginOrEmail: username}
  128. if err := bus.Dispatch(&loginQuery); err != nil {
  129. ctx.JsonApiErr(401, "Basic auth failed", err)
  130. return true
  131. }
  132. user := loginQuery.Result
  133. loginUserQuery := m.LoginUserQuery{Username: username, Password: password, User: user}
  134. if err := bus.Dispatch(&loginUserQuery); err != nil {
  135. ctx.JsonApiErr(401, "Invalid username or password", err)
  136. return true
  137. }
  138. query := m.GetSignedInUserQuery{UserId: user.Id, OrgId: orgId}
  139. if err := bus.Dispatch(&query); err != nil {
  140. ctx.JsonApiErr(401, "Authentication error", err)
  141. return true
  142. }
  143. ctx.SignedInUser = query.Result
  144. ctx.IsSignedIn = true
  145. return true
  146. }
  147. func initContextWithToken(authTokenService m.UserTokenService, ctx *m.ReqContext, orgID int64) bool {
  148. rawToken := ctx.GetCookie(setting.LoginCookieName)
  149. if rawToken == "" {
  150. return false
  151. }
  152. token, err := authTokenService.LookupToken(ctx.Req.Context(), rawToken)
  153. if err != nil {
  154. ctx.Logger.Error("failed to look up user based on cookie", "error", err)
  155. WriteSessionCookie(ctx, "", -1)
  156. return false
  157. }
  158. query := m.GetSignedInUserQuery{UserId: token.UserId, OrgId: orgID}
  159. if err := bus.Dispatch(&query); err != nil {
  160. ctx.Logger.Error("failed to get user with id", "userId", token.UserId, "error", err)
  161. return false
  162. }
  163. ctx.SignedInUser = query.Result
  164. ctx.IsSignedIn = true
  165. ctx.UserToken = token
  166. rotated, err := authTokenService.TryRotateToken(ctx.Req.Context(), token, ctx.RemoteAddr(), ctx.Req.UserAgent())
  167. if err != nil {
  168. ctx.Logger.Error("failed to rotate token", "error", err)
  169. return true
  170. }
  171. if rotated {
  172. WriteSessionCookie(ctx, token.UnhashedToken, setting.LoginMaxLifetimeDays)
  173. }
  174. return true
  175. }
  176. func WriteSessionCookie(ctx *m.ReqContext, value string, maxLifetimeDays int) {
  177. if setting.Env == setting.DEV {
  178. ctx.Logger.Info("new token", "unhashed token", value)
  179. }
  180. var maxAge int
  181. if maxLifetimeDays <= 0 {
  182. maxAge = -1
  183. } else {
  184. maxAgeHours := (time.Duration(setting.LoginMaxLifetimeDays) * 24 * time.Hour) + time.Hour
  185. maxAge = int(maxAgeHours.Seconds())
  186. }
  187. ctx.Resp.Header().Del("Set-Cookie")
  188. cookie := http.Cookie{
  189. Name: setting.LoginCookieName,
  190. Value: url.QueryEscape(value),
  191. HttpOnly: true,
  192. Path: setting.AppSubUrl + "/",
  193. Secure: setting.CookieSecure,
  194. MaxAge: maxAge,
  195. SameSite: setting.CookieSameSite,
  196. }
  197. http.SetCookie(ctx.Resp, &cookie)
  198. }
  199. func AddDefaultResponseHeaders() macaron.Handler {
  200. return func(ctx *macaron.Context) {
  201. ctx.Resp.Before(func(w macaron.ResponseWriter) {
  202. if !strings.HasPrefix(ctx.Req.URL.Path, "/api/datasources/proxy/") {
  203. AddNoCacheHeaders(ctx.Resp)
  204. }
  205. if !setting.AllowEmbedding {
  206. AddXFrameOptionsDenyHeader(w)
  207. }
  208. AddSecurityHeaders(w)
  209. })
  210. }
  211. }
  212. // AddSecurityHeaders adds various HTTP(S) response headers that enable various security protections behaviors in the client's browser.
  213. func AddSecurityHeaders(w macaron.ResponseWriter) {
  214. if setting.Protocol == setting.HTTPS && setting.StrictTransportSecurity {
  215. strictHeader := "Strict-Transport-Security"
  216. w.Header().Add(strictHeader, fmt.Sprintf("max-age=%v", setting.StrictTransportSecurityMaxAge))
  217. if setting.StrictTransportSecurityPreload {
  218. w.Header().Add(strictHeader, "preload")
  219. }
  220. if setting.StrictTransportSecuritySubDomains {
  221. w.Header().Add(strictHeader, "includeSubDomains")
  222. }
  223. }
  224. if setting.ContentTypeProtectionHeader {
  225. w.Header().Add("X-Content-Type-Options", "nosniff")
  226. }
  227. if setting.XSSProtectionHeader {
  228. w.Header().Add("X-XSS-Protection", "1")
  229. w.Header().Add("X-XSS-Protection", "mode=block")
  230. }
  231. }
  232. func AddNoCacheHeaders(w macaron.ResponseWriter) {
  233. w.Header().Add("Cache-Control", "no-cache")
  234. w.Header().Add("Pragma", "no-cache")
  235. w.Header().Add("Expires", "-1")
  236. }
  237. func AddXFrameOptionsDenyHeader(w macaron.ResponseWriter) {
  238. w.Header().Add("X-Frame-Options", "deny")
  239. }