middleware.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package middleware
  2. import (
  3. "strconv"
  4. "strings"
  5. "gopkg.in/macaron.v1"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/apikeygen"
  8. "github.com/grafana/grafana/pkg/log"
  9. l "github.com/grafana/grafana/pkg/login"
  10. "github.com/grafana/grafana/pkg/metrics"
  11. m "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/setting"
  13. "github.com/grafana/grafana/pkg/util"
  14. )
  15. type Context struct {
  16. *macaron.Context
  17. *m.SignedInUser
  18. Session SessionStore
  19. IsSignedIn bool
  20. IsRenderCall bool
  21. AllowAnonymous bool
  22. Logger log.Logger
  23. }
  24. func GetContextHandler() macaron.Handler {
  25. return func(c *macaron.Context) {
  26. ctx := &Context{
  27. Context: c,
  28. SignedInUser: &m.SignedInUser{},
  29. Session: GetSession(),
  30. IsSignedIn: false,
  31. AllowAnonymous: false,
  32. Logger: log.New("context"),
  33. }
  34. // the order in which these are tested are important
  35. // look for api key in Authorization header first
  36. // then init session and look for userId in session
  37. // then look for api key in session (special case for render calls via api)
  38. // then test if anonymous access is enabled
  39. if initContextWithRenderAuth(ctx) ||
  40. initContextWithApiKey(ctx) ||
  41. initContextWithBasicAuth(ctx) ||
  42. initContextWithAuthProxy(ctx) ||
  43. initContextWithUserSessionCookie(ctx) ||
  44. initContextWithAnonymousUser(ctx) {
  45. }
  46. ctx.Logger = log.New("context", "userId", ctx.UserId, "orgId", ctx.OrgId, "uname", ctx.Login)
  47. ctx.Data["ctx"] = ctx
  48. c.Map(ctx)
  49. }
  50. }
  51. func initContextWithAnonymousUser(ctx *Context) bool {
  52. if !setting.AnonymousEnabled {
  53. return false
  54. }
  55. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  56. if err := bus.Dispatch(&orgQuery); err != nil {
  57. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  58. return false
  59. } else {
  60. ctx.IsSignedIn = false
  61. ctx.AllowAnonymous = true
  62. ctx.SignedInUser = &m.SignedInUser{}
  63. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  64. ctx.OrgId = orgQuery.Result.Id
  65. ctx.OrgName = orgQuery.Result.Name
  66. return true
  67. }
  68. }
  69. func initContextWithUserSessionCookie(ctx *Context) bool {
  70. // initialize session
  71. if err := ctx.Session.Start(ctx); err != nil {
  72. ctx.Logger.Error("Failed to start session", "error", err)
  73. return false
  74. }
  75. var userId int64
  76. if userId = getRequestUserId(ctx); userId == 0 {
  77. return false
  78. }
  79. query := m.GetSignedInUserQuery{UserId: userId}
  80. if err := bus.Dispatch(&query); err != nil {
  81. ctx.Logger.Error("Failed to get user with id", "userId", userId)
  82. return false
  83. } else {
  84. ctx.SignedInUser = query.Result
  85. ctx.IsSignedIn = true
  86. return true
  87. }
  88. }
  89. func initContextWithApiKey(ctx *Context) 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. } else {
  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. }
  120. func initContextWithBasicAuth(ctx *Context) bool {
  121. if !setting.BasicAuthEnabled {
  122. return false
  123. }
  124. header := ctx.Req.Header.Get("Authorization")
  125. if header == "" {
  126. return false
  127. }
  128. username, password, err := util.DecodeBasicAuthHeader(header)
  129. if err != nil {
  130. ctx.JsonApiErr(401, "Invalid Basic Auth Header", err)
  131. return true
  132. }
  133. loginQuery := m.GetUserByLoginQuery{LoginOrEmail: username}
  134. if err := bus.Dispatch(&loginQuery); err != nil {
  135. ctx.JsonApiErr(401, "Basic auth failed", err)
  136. return true
  137. }
  138. user := loginQuery.Result
  139. loginUserQuery := l.LoginUserQuery{Username: username, Password: password, User: user}
  140. if err := bus.Dispatch(&loginUserQuery); err != nil {
  141. ctx.JsonApiErr(401, "Invalid username or password", err)
  142. return true
  143. }
  144. query := m.GetSignedInUserQuery{UserId: user.Id}
  145. if err := bus.Dispatch(&query); err != nil {
  146. ctx.JsonApiErr(401, "Authentication error", err)
  147. return true
  148. } else {
  149. ctx.SignedInUser = query.Result
  150. ctx.IsSignedIn = true
  151. return true
  152. }
  153. }
  154. // Handle handles and logs error by given status.
  155. func (ctx *Context) Handle(status int, title string, err error) {
  156. if err != nil {
  157. ctx.Logger.Error(title, "error", err)
  158. if setting.Env != setting.PROD {
  159. ctx.Data["ErrorMsg"] = err
  160. }
  161. }
  162. ctx.Data["Title"] = title
  163. ctx.Data["AppSubUrl"] = setting.AppSubUrl
  164. ctx.HTML(status, strconv.Itoa(status))
  165. }
  166. func (ctx *Context) JsonOK(message string) {
  167. resp := make(map[string]interface{})
  168. resp["message"] = message
  169. ctx.JSON(200, resp)
  170. }
  171. func (ctx *Context) IsApiRequest() bool {
  172. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  173. }
  174. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  175. resp := make(map[string]interface{})
  176. if err != nil {
  177. ctx.Logger.Error(message, "error", err)
  178. if setting.Env != setting.PROD {
  179. resp["error"] = err.Error()
  180. }
  181. }
  182. switch status {
  183. case 404:
  184. resp["message"] = "Not Found"
  185. case 500:
  186. resp["message"] = "Internal Server Error"
  187. }
  188. if message != "" {
  189. resp["message"] = message
  190. }
  191. ctx.JSON(status, resp)
  192. }
  193. func (ctx *Context) HasUserRole(role m.RoleType) bool {
  194. return ctx.OrgRole.Includes(role)
  195. }
  196. func (ctx *Context) HasHelpFlag(flag m.HelpFlags1) bool {
  197. return ctx.HelpFlags1.HasFlag(flag)
  198. }
  199. func (ctx *Context) TimeRequest(timer metrics.Timer) {
  200. ctx.Data["perfmon.timer"] = timer
  201. }