handlers.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package api4
  4. import (
  5. "net/http"
  6. "github.com/NYTimes/gziphandler"
  7. "github.com/mattermost/mattermost-server/v5/web"
  8. )
  9. type Context = web.Context
  10. // ApiHandler provides a handler for API endpoints which do not require the user to be logged in order for access to be
  11. // granted.
  12. func (api *API) ApiHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  13. handler := &web.Handler{
  14. GetGlobalAppOptions: api.GetGlobalAppOptions,
  15. HandleFunc: h,
  16. HandlerName: web.GetHandlerName(h),
  17. RequireSession: false,
  18. TrustRequester: false,
  19. RequireMfa: false,
  20. IsStatic: false,
  21. IsLocal: false,
  22. }
  23. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  24. return gziphandler.GzipHandler(handler)
  25. }
  26. return handler
  27. }
  28. // ApiSessionRequired provides a handler for API endpoints which require the user to be logged in in order for access to
  29. // be granted.
  30. func (api *API) ApiSessionRequired(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  31. handler := &web.Handler{
  32. GetGlobalAppOptions: api.GetGlobalAppOptions,
  33. HandleFunc: h,
  34. HandlerName: web.GetHandlerName(h),
  35. RequireSession: true,
  36. TrustRequester: false,
  37. RequireMfa: true,
  38. IsStatic: false,
  39. IsLocal: false,
  40. }
  41. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  42. return gziphandler.GzipHandler(handler)
  43. }
  44. return handler
  45. }
  46. // ApiSessionRequiredMfa provides a handler for API endpoints which require a logged-in user session but when accessed,
  47. // if MFA is enabled, the MFA process is not yet complete, and therefore the requirement to have completed the MFA
  48. // authentication must be waived.
  49. func (api *API) ApiSessionRequiredMfa(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  50. handler := &web.Handler{
  51. GetGlobalAppOptions: api.GetGlobalAppOptions,
  52. HandleFunc: h,
  53. HandlerName: web.GetHandlerName(h),
  54. RequireSession: true,
  55. TrustRequester: false,
  56. RequireMfa: false,
  57. IsStatic: false,
  58. IsLocal: false,
  59. }
  60. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  61. return gziphandler.GzipHandler(handler)
  62. }
  63. return handler
  64. }
  65. // ApiHandlerTrustRequester provides a handler for API endpoints which do not require the user to be logged in and are
  66. // allowed to be requested directly rather than via javascript/XMLHttpRequest, such as site branding images or the
  67. // websocket.
  68. func (api *API) ApiHandlerTrustRequester(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  69. handler := &web.Handler{
  70. GetGlobalAppOptions: api.GetGlobalAppOptions,
  71. HandleFunc: h,
  72. HandlerName: web.GetHandlerName(h),
  73. RequireSession: false,
  74. TrustRequester: true,
  75. RequireMfa: false,
  76. IsStatic: false,
  77. IsLocal: false,
  78. }
  79. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  80. return gziphandler.GzipHandler(handler)
  81. }
  82. return handler
  83. }
  84. // ApiSessionRequiredTrustRequester provides a handler for API endpoints which do require the user to be logged in and
  85. // are allowed to be requested directly rather than via javascript/XMLHttpRequest, such as emoji or file uploads.
  86. func (api *API) ApiSessionRequiredTrustRequester(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  87. handler := &web.Handler{
  88. GetGlobalAppOptions: api.GetGlobalAppOptions,
  89. HandleFunc: h,
  90. HandlerName: web.GetHandlerName(h),
  91. RequireSession: true,
  92. TrustRequester: true,
  93. RequireMfa: true,
  94. IsStatic: false,
  95. IsLocal: false,
  96. }
  97. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  98. return gziphandler.GzipHandler(handler)
  99. }
  100. return handler
  101. }
  102. // DisableWhenBusy provides a handler for API endpoints which should be disabled when the server is under load,
  103. // responding with HTTP 503 (Service Unavailable).
  104. func (api *API) ApiSessionRequiredDisableWhenBusy(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  105. handler := &web.Handler{
  106. GetGlobalAppOptions: api.GetGlobalAppOptions,
  107. HandleFunc: h,
  108. HandlerName: web.GetHandlerName(h),
  109. RequireSession: true,
  110. TrustRequester: false,
  111. RequireMfa: false,
  112. IsStatic: false,
  113. IsLocal: false,
  114. DisableWhenBusy: true,
  115. }
  116. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  117. return gziphandler.GzipHandler(handler)
  118. }
  119. return handler
  120. }
  121. // ApiLocal provides a handler for API endpoints to be used in local
  122. // mode, this is, through a UNIX socket and without an authenticated
  123. // session, but with one that has no user set and no permission
  124. // restrictions
  125. func (api *API) ApiLocal(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
  126. handler := &web.Handler{
  127. GetGlobalAppOptions: api.GetGlobalAppOptions,
  128. HandleFunc: h,
  129. HandlerName: web.GetHandlerName(h),
  130. RequireSession: false,
  131. TrustRequester: false,
  132. RequireMfa: false,
  133. IsStatic: false,
  134. IsLocal: true,
  135. }
  136. if *api.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
  137. return gziphandler.GzipHandler(handler)
  138. }
  139. return handler
  140. }