API lint fix.

This commit is contained in:
Miroslav Šedivý 2020-10-31 21:58:57 +01:00
parent 5a25aefa34
commit b6393c4396
3 changed files with 17 additions and 11 deletions

View File

@ -36,7 +36,7 @@ func (h *RoomHandler) ClipboardRead(w http.ResponseWriter, r *http.Request) {
func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) {
data := &ClipboardData{} data := &ClipboardData{}
if err := render.Bind(r, data); err != nil { if err := render.Bind(r, data); err != nil {
render.Render(w, r, utils.ErrBadRequest(err)) _ = render.Render(w, r, utils.ErrBadRequest(err))
return return
} }

View File

@ -31,7 +31,7 @@ func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request
size := h.remote.GetScreenSize() size := h.remote.GetScreenSize()
if size == nil { 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 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) { func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.Request) {
data := &ScreenConfiguration{} data := &ScreenConfiguration{}
if err := render.Bind(r, data); err != nil { if err := render.Bind(r, data); err != nil {
render.Render(w, r, utils.ErrBadRequest(err)) _ = render.Render(w, r, utils.ErrBadRequest(err))
return return
} }
if err := h.remote.ChangeResolution(data.Width, data.Height, data.Rate); err != nil { 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 return
} }
if err := broadcast.ScreenConfiguration(h.sessions, "-todo-session-id-", data.Width, data.Height, data.Rate); err != nil { 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 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)
} }

View File

@ -10,8 +10,14 @@ import (
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
) )
type key int
const (
keyPrincipalID key = iota
)
func GetUserName(r *http.Request) interface{} { func GetUserName(r *http.Request) interface{} {
props, _ := r.Context().Value("props").(jwt.MapClaims) props, _ := r.Context().Value(keyPrincipalID).(jwt.MapClaims)
return props["user_name"] 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := strings.Split(r.Header.Get("Authorization"), "Bearer ") authHeader := strings.Split(r.Header.Get("Authorization"), "Bearer ")
if len(authHeader) != 2 { if len(authHeader) != 2 {
render.Render(w, r, ErrMessage(401, "Malformed JWT token.")) _ = render.Render(w, r, ErrMessage(401, "Malformed JWT token."))
return return
} }
@ -43,17 +49,17 @@ func AuthMiddleware(next http.Handler, jwtSecrets ...[]byte) http.Handler {
} }
if err != nil { if err != nil {
render.Render(w, r, ErrMessage(401, "Invalid JWT token.")) _ = render.Render(w, r, ErrMessage(401, "Invalid JWT token."))
return return
} }
if claims, ok := jwtVerified.Claims.(jwt.MapClaims); ok && jwtVerified.Valid { 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 // Access context values in handlers like this
// props, _ := r.Context().Value("props").(jwt.MapClaims) // props, _ := r.Context().Value("props").(jwt.MapClaims)
next.ServeHTTP(w, r.WithContext(ctx)) next.ServeHTTP(w, r.WithContext(ctx))
} else { } else {
render.Render(w, r, ErrMessage(401, "Unauthorized.")) _ = render.Render(w, r, ErrMessage(401, "Unauthorized."))
} }
}) })
} }