Skip to content

Commit

Permalink
Fix common errors
Browse files Browse the repository at this point in the history
  • Loading branch information
icerzack committed Mar 2, 2025
1 parent 4f63fbf commit c574eb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 18 additions & 15 deletions internal/rest/ws/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/gorilla/websocket"
"go.uber.org/zap"
Expand All @@ -21,6 +20,7 @@ import (
var (
ErrInvalidMessage = errors.New("invalid message")
ErrValidatingJWT = errors.New("failed to validate jwt")
ErrInvalidJWT = errors.New("invalid jwt")
)

const (
Expand Down Expand Up @@ -390,12 +390,14 @@ func (ws *WebSocketHandler) sendUserDisconnected(request MessageUserDisconnected
}

func (ws *WebSocketHandler) validateJWT(jwt string) (string, error) {
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, ws.jwtValidationURL, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, ws.jwtValidationURL, nil)
if err != nil {
return "", fmt.Errorf("failed to create validation request: %w", err)
}
req.Header.Set(ws.jwtHeaderName, jwt)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
ws.logger.Error("Failed to send jwt validation request", zap.Error(err))
return "", fmt.Errorf("failed to send validation request: %w", err)
}
defer resp.Body.Close()
Expand All @@ -409,29 +411,30 @@ func (ws *WebSocketHandler) validateJWT(jwt string) (string, error) {
return "", fmt.Errorf("internal server error: %w", ErrValidatingJWT)
}

// Decode the JWTValidationResponse
var jwtResponse JWTValidationResponse
err = json.NewDecoder(resp.Body).Decode(&jwtResponse)
if err != nil {
ws.logger.Error("Failed to decode JWT response", zap.Error(err))
return "", fmt.Errorf("failed to decode JWT response: %w", err)
}
if jwtResponse.ID == "0" {
return "", ErrInvalidJWT
}

return strconv.Itoa(jwtResponse.ID), nil
return jwtResponse.ID, nil
}

func (ws *WebSocketHandler) validateBoardAccess(boardID, jwt string) bool {
fullURL, err := url.JoinPath(ws.boardValidationURL, boardID)
if err != nil {
ws.logger.Error("Failed to join URL", zap.Error(err))
ws.logger.Error("failed to join URL", zap.Error(err))
return false
}
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, fullURL, nil)
req.Header.Set(ws.jwtHeaderName, jwt)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
ws.logger.Error("Failed to send board validation request", zap.Error(err))
ws.logger.Error("failed to send board validation request", zap.Error(err))
return false
}
defer resp.Body.Close()
Expand Down Expand Up @@ -482,34 +485,34 @@ func (ws *WebSocketHandler) cacheOrValidate(jwt, boardID string) (string, error)
// Check if the user is in cache
if v, err := ws.cache.Get(jwt); v == nil {
if err != nil {
ws.logger.Error("Failed to get from cache", zap.Error(err))
return "", fmt.Errorf("failed to get from cache: %w", err)
}
// Get the UserID from the JWT token
userID, err = ws.validateJWT(jwt)
if err != nil {
ws.logger.Error("Failed to validate JWT", zap.Error(err))
return "", fmt.Errorf("failed to validate JWT: %w", err)
}

// Check if the user has access to the board
if !ws.validateBoardAccess(boardID, jwt) {
ws.logger.Debug("User doesn't have access to the board",
zap.String("userID", userID), zap.String("boardID", boardID))
return "", fmt.Errorf("user doesn't have access to the board: %w", err)
return "", fmt.Errorf(
"user '%s' doesn't have access to the board '%s': %w",
userID,
boardID,
err,
)
}

// Store the validation result
_ = ws.cache.SetWithTTL(jwt, userID, ws.cacheTTLInSeconds)
} else {
if err != nil {
ws.logger.Error("Failed to get from cache", zap.Error(err))
return "", fmt.Errorf("failed to get from cache: %w", err)
}
var ok bool
userID, ok = v.(string)
if !ok {
return "", fmt.Errorf("failed to get from cache: %w", err)
return "", fmt.Errorf("failed to parse userID to string: %w", err)
}
}
return userID, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/rest/ws/messages.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ws

type JWTValidationResponse struct {
ID int `json:"id"`
ID string `json:"id"`
}

type Message struct {
Expand Down

0 comments on commit c574eb7

Please sign in to comment.