API handler opts modifier (#26148)
* POC for API handler opts modifier * Made upload POSt api a file upload API * Specified file upload local API * Specified file upload local API * Specified file upload API * Simplified handler params * Added basic security checks * Fixed i18n * used type for API handler options * Removed limited reader from util deserializers (#26263)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ecb09de6c7
Коммит
521844fed5
@@ -18,9 +18,15 @@ type Context = web.Context
|
||||
|
||||
type handlerFunc func(*Context, http.ResponseWriter, *http.Request)
|
||||
|
||||
type APIHandlerOption string
|
||||
|
||||
const (
|
||||
handlerParamFileAPI = APIHandlerOption("fileAPI")
|
||||
)
|
||||
|
||||
// APIHandler provides a handler for API endpoints which do not require the user to be logged in order for access to be
|
||||
// granted.
|
||||
func (api *API) APIHandler(h handlerFunc) http.Handler {
|
||||
func (api *API) APIHandler(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -31,6 +37,8 @@ func (api *API) APIHandler(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -39,7 +47,7 @@ func (api *API) APIHandler(h handlerFunc) http.Handler {
|
||||
|
||||
// APISessionRequired provides a handler for API endpoints which require the user to be logged in in order for access to
|
||||
// be granted.
|
||||
func (api *API) APISessionRequired(h handlerFunc) http.Handler {
|
||||
func (api *API) APISessionRequired(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -50,6 +58,8 @@ func (api *API) APISessionRequired(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -57,7 +67,7 @@ func (api *API) APISessionRequired(h handlerFunc) http.Handler {
|
||||
}
|
||||
|
||||
// CloudAPIKeyRequired provides a handler for webhook endpoints to access Cloud installations from CWS
|
||||
func (api *API) CloudAPIKeyRequired(h handlerFunc) http.Handler {
|
||||
func (api *API) CloudAPIKeyRequired(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -69,6 +79,8 @@ func (api *API) CloudAPIKeyRequired(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -76,7 +88,7 @@ func (api *API) CloudAPIKeyRequired(h handlerFunc) http.Handler {
|
||||
}
|
||||
|
||||
// RemoteClusterTokenRequired provides a handler for remote cluster requests to /remotecluster endpoints.
|
||||
func (api *API) RemoteClusterTokenRequired(h handlerFunc) http.Handler {
|
||||
func (api *API) RemoteClusterTokenRequired(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -89,6 +101,8 @@ func (api *API) RemoteClusterTokenRequired(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -98,7 +112,7 @@ func (api *API) RemoteClusterTokenRequired(h handlerFunc) http.Handler {
|
||||
// APISessionRequiredMfa provides a handler for API endpoints which require a logged-in user session but when accessed,
|
||||
// if MFA is enabled, the MFA process is not yet complete, and therefore the requirement to have completed the MFA
|
||||
// authentication must be waived.
|
||||
func (api *API) APISessionRequiredMfa(h handlerFunc) http.Handler {
|
||||
func (api *API) APISessionRequiredMfa(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -109,6 +123,8 @@ func (api *API) APISessionRequiredMfa(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -118,7 +134,7 @@ func (api *API) APISessionRequiredMfa(h handlerFunc) http.Handler {
|
||||
// APIHandlerTrustRequester provides a handler for API endpoints which do not require the user to be logged in and are
|
||||
// allowed to be requested directly rather than via javascript/XMLHttpRequest, such as site branding images or the
|
||||
// websocket.
|
||||
func (api *API) APIHandlerTrustRequester(h handlerFunc) http.Handler {
|
||||
func (api *API) APIHandlerTrustRequester(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -129,6 +145,8 @@ func (api *API) APIHandlerTrustRequester(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -137,7 +155,7 @@ func (api *API) APIHandlerTrustRequester(h handlerFunc) http.Handler {
|
||||
|
||||
// APISessionRequiredTrustRequester provides a handler for API endpoints which do require the user to be logged in and
|
||||
// are allowed to be requested directly rather than via javascript/XMLHttpRequest, such as emoji or file uploads.
|
||||
func (api *API) APISessionRequiredTrustRequester(h handlerFunc) http.Handler {
|
||||
func (api *API) APISessionRequiredTrustRequester(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -148,6 +166,8 @@ func (api *API) APISessionRequiredTrustRequester(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -156,7 +176,7 @@ func (api *API) APISessionRequiredTrustRequester(h handlerFunc) http.Handler {
|
||||
|
||||
// DisableWhenBusy provides a handler for API endpoints which should be disabled when the server is under load,
|
||||
// responding with HTTP 503 (Service Unavailable).
|
||||
func (api *API) APISessionRequiredDisableWhenBusy(h handlerFunc) http.Handler {
|
||||
func (api *API) APISessionRequiredDisableWhenBusy(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -168,6 +188,8 @@ func (api *API) APISessionRequiredDisableWhenBusy(h handlerFunc) http.Handler {
|
||||
IsLocal: false,
|
||||
DisableWhenBusy: true,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
}
|
||||
@@ -178,7 +200,7 @@ func (api *API) APISessionRequiredDisableWhenBusy(h handlerFunc) http.Handler {
|
||||
// mode, this is, through a UNIX socket and without an authenticated
|
||||
// session, but with one that has no user set and no permission
|
||||
// restrictions
|
||||
func (api *API) APILocal(h handlerFunc) http.Handler {
|
||||
func (api *API) APILocal(h handlerFunc, opts ...APIHandlerOption) http.Handler {
|
||||
handler := &web.Handler{
|
||||
Srv: api.srv,
|
||||
HandleFunc: h,
|
||||
@@ -189,6 +211,7 @@ func (api *API) APILocal(h handlerFunc) http.Handler {
|
||||
IsStatic: false,
|
||||
IsLocal: true,
|
||||
}
|
||||
setHandlerOpts(handler, opts...)
|
||||
|
||||
if *api.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gzhttp.GzipHandler(handler)
|
||||
@@ -223,3 +246,16 @@ func minimumProfessionalLicense(c *Context) *model.AppError {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setHandlerOpts(handler *web.Handler, opts ...APIHandlerOption) {
|
||||
if len(opts) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, option := range opts {
|
||||
switch option {
|
||||
case handlerParamFileAPI:
|
||||
handler.FileAPI = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user