update auth middlewares.

This commit is contained in:
Miroslav Šedivý 2021-03-21 21:26:53 +01:00
parent 5a79212b32
commit 0eef9d4d98
3 changed files with 14 additions and 29 deletions

View File

@ -41,11 +41,6 @@ func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) {
} }
session := auth.GetSession(r) session := auth.GetSession(r)
if !session.Profile().CanHost {
utils.HttpBadRequest(w, "Session is not allowed to host.")
return
}
h.sessions.SetHost(session) h.sessions.SetHost(session)
utils.HttpSuccess(w) utils.HttpSuccess(w)
@ -58,11 +53,6 @@ func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
return return
} }
if !session.Profile().CanHost {
utils.HttpBadRequest(w, "Session is not allowed to host.")
return
}
h.desktop.ResetKeys() h.desktop.ResetKeys()
h.sessions.ClearHost() h.sessions.ClearHost()
@ -71,11 +61,6 @@ func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) {
session := auth.GetSession(r) session := auth.GetSession(r)
if !session.Profile().CanHost {
utils.HttpBadRequest(w, "Session is not allowed to host.")
return
}
h.sessions.SetHost(session) h.sessions.SetHost(session)
utils.HttpSuccess(w) utils.HttpSuccess(w)

View File

@ -37,7 +37,7 @@ func (h *RoomHandler) Route(r chi.Router) {
r.Post("/stop", h.boradcastStop) r.Post("/stop", h.boradcastStop)
}) })
r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) { r.With(auth.CanAccessClipboardOnly).With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) {
r.Get("/", h.clipboardGetText) r.Get("/", h.clipboardGetText)
r.Post("/", h.clipboardSetText) r.Post("/", h.clipboardSetText)
r.Get("/image.png", h.clipboardGetImage) r.Get("/image.png", h.clipboardGetImage)
@ -52,7 +52,7 @@ func (h *RoomHandler) Route(r chi.Router) {
//r.Get("/targets", h.clipboardGetTargets) //r.Get("/targets", h.clipboardGetTargets)
}) })
r.Route("/keyboard", func(r chi.Router) { r.With(auth.CanHostOnly).Route("/keyboard", func(r chi.Router) {
r.Get("/map", h.keyboardMapGet) r.Get("/map", h.keyboardMapGet)
r.With(auth.HostsOnly).Post("/map", h.keyboardMapSet) r.With(auth.HostsOnly).Post("/map", h.keyboardMapSet)
@ -60,7 +60,7 @@ func (h *RoomHandler) Route(r chi.Router) {
r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet) r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet)
}) })
r.Route("/control", func(r chi.Router) { r.With(auth.CanHostOnly).Route("/control", func(r chi.Router) {
r.Get("/", h.controlStatus) r.Get("/", h.controlStatus)
r.Post("/request", h.controlRequest) r.Post("/request", h.controlRequest)
r.Post("/release", h.controlRelease) r.Post("/release", h.controlRelease)
@ -70,13 +70,13 @@ func (h *RoomHandler) Route(r chi.Router) {
r.With(auth.AdminsOnly).Post("/reset", h.controlReset) r.With(auth.AdminsOnly).Post("/reset", h.controlReset)
}) })
r.Route("/screen", func(r chi.Router) { r.With(auth.CanWatchOnly).Route("/screen", func(r chi.Router) {
r.With(auth.CanWatchOnly).Get("/", h.screenConfiguration) r.Get("/", h.screenConfiguration)
r.With(auth.CanWatchOnly).Get("/shot.jpg", h.screenShotGet)
r.With(auth.CanWatchOnly).Get("/cast.jpg", h.screenCastGet)
r.With(auth.AdminsOnly).Post("/", h.screenConfigurationChange) r.With(auth.AdminsOnly).Post("/", h.screenConfigurationChange)
r.With(auth.AdminsOnly).Get("/configurations", h.screenConfigurationsList) r.With(auth.AdminsOnly).Get("/configurations", h.screenConfigurationsList)
r.Get("/cast.jpg", h.screenCastGet)
r.With(auth.AdminsOnly).Get("/shot.jpg", h.screenShotGet)
}) })
r.With(h.uploadMiddleware).Route("/upload", func(r chi.Router) { r.With(h.uploadMiddleware).Route("/upload", func(r chi.Router) {

View File

@ -45,11 +45,11 @@ func HostsOnly(next http.Handler) http.Handler {
}) })
} }
func HostsOrAdminsOnly(next http.Handler) http.Handler { func CanWatchOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := GetSession(r) session := GetSession(r)
if !session.IsHost() && !session.Profile().IsAdmin { if !session.Profile().CanWatch {
utils.HttpForbidden(w, "Only host can do this.") utils.HttpForbidden(w, "Only for sessions, that can watch.")
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
} }
@ -67,11 +67,11 @@ func CanHostOnly(next http.Handler) http.Handler {
}) })
} }
func CanWatchOnly(next http.Handler) http.Handler { func CanAccessClipboardOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := GetSession(r) session := GetSession(r)
if !session.Profile().CanWatch { if !session.Profile().CanAccessClipboard {
utils.HttpForbidden(w, "Only for sessions, that can watch.") utils.HttpForbidden(w, "Only for sessions, that can access clipboard.")
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
} }