middleware.go 8.5 KB

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