From b6393c4396ef08c0a9848716aa67631cbc79980c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 31 Oct 2020 21:58:57 +0100 Subject: [PATCH] API lint fix. --- internal/api/room/clipboard.go | 2 +- internal/api/room/screen.go | 10 +++++----- internal/api/utils/auth.go | 16 +++++++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/internal/api/room/clipboard.go b/internal/api/room/clipboard.go index aaf7d2b2..0cc4beb6 100644 --- a/internal/api/room/clipboard.go +++ b/internal/api/room/clipboard.go @@ -36,7 +36,7 @@ func (h *RoomHandler) ClipboardRead(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) { data := &ClipboardData{} if err := render.Bind(r, data); err != nil { - render.Render(w, r, utils.ErrBadRequest(err)) + _ = render.Render(w, r, utils.ErrBadRequest(err)) return } diff --git a/internal/api/room/screen.go b/internal/api/room/screen.go index 30932939..aa29be0c 100644 --- a/internal/api/room/screen.go +++ b/internal/api/room/screen.go @@ -31,7 +31,7 @@ func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request size := h.remote.GetScreenSize() if size == nil { - render.Render(w, r, utils.ErrMessage(500, "Unable to get screen configuration.")) + _ = render.Render(w, r, utils.ErrMessage(500, "Unable to get screen configuration.")) return } @@ -45,17 +45,17 @@ func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.Request) { data := &ScreenConfiguration{} if err := render.Bind(r, data); err != nil { - render.Render(w, r, utils.ErrBadRequest(err)) + _ = render.Render(w, r, utils.ErrBadRequest(err)) return } if err := h.remote.ChangeResolution(data.Width, data.Height, data.Rate); err != nil { - render.Render(w, r, utils.ErrUnprocessableEntity(err)) + _ = render.Render(w, r, utils.ErrUnprocessableEntity(err)) return } if err := broadcast.ScreenConfiguration(h.sessions, "-todo-session-id-", data.Width, data.Height, data.Rate); err != nil { - render.Render(w, r, utils.ErrInternalServer(err)) + _ = render.Render(w, r, utils.ErrInternalServer(err)) return } @@ -76,5 +76,5 @@ func (h *RoomHandler) ScreenConfigurationsList(w http.ResponseWriter, r *http.Re } } - render.RenderList(w, r, list) + _ = render.RenderList(w, r, list) } diff --git a/internal/api/utils/auth.go b/internal/api/utils/auth.go index 03f80dbb..8d25bbfe 100644 --- a/internal/api/utils/auth.go +++ b/internal/api/utils/auth.go @@ -10,8 +10,14 @@ import ( "github.com/dgrijalva/jwt-go" ) +type key int + +const ( + keyPrincipalID key = iota +) + func GetUserName(r *http.Request) interface{} { - props, _ := r.Context().Value("props").(jwt.MapClaims) + props, _ := r.Context().Value(keyPrincipalID).(jwt.MapClaims) return props["user_name"] } @@ -21,7 +27,7 @@ func AuthMiddleware(next http.Handler, jwtSecrets ...[]byte) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { authHeader := strings.Split(r.Header.Get("Authorization"), "Bearer ") if len(authHeader) != 2 { - render.Render(w, r, ErrMessage(401, "Malformed JWT token.")) + _ = render.Render(w, r, ErrMessage(401, "Malformed JWT token.")) return } @@ -43,17 +49,17 @@ func AuthMiddleware(next http.Handler, jwtSecrets ...[]byte) http.Handler { } if err != nil { - render.Render(w, r, ErrMessage(401, "Invalid JWT token.")) + _ = render.Render(w, r, ErrMessage(401, "Invalid JWT token.")) return } if claims, ok := jwtVerified.Claims.(jwt.MapClaims); ok && jwtVerified.Valid { - ctx := context.WithValue(r.Context(), "props", claims) + ctx := context.WithValue(r.Context(), keyPrincipalID, claims) // Access context values in handlers like this // props, _ := r.Context().Value("props").(jwt.MapClaims) next.ServeHTTP(w, r.WithContext(ctx)) } else { - render.Render(w, r, ErrMessage(401, "Unauthorized.")) + _ = render.Render(w, r, ErrMessage(401, "Unauthorized.")) } }) }