code refactor, implementing sockets
This commit is contained in:
65
utils/errorHandler.go
Normal file
65
utils/errorHandler.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
ErrInternal ErrorCode = "INTERNAL_ERROR"
|
||||
ErrNotFound ErrorCode = "NOT_FOUND"
|
||||
ErrInvalidInput ErrorCode = "INVALID_INPUT"
|
||||
ErrForbidden ErrorCode = "FORBIDDEN"
|
||||
ErrUnauthorized ErrorCode = "UNAUTHORIZED"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Code ErrorCode
|
||||
UserMessage string
|
||||
InternalError error
|
||||
StatusCode int
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
if e.InternalError != nil {
|
||||
return e.InternalError.Error()
|
||||
}
|
||||
return e.UserMessage
|
||||
}
|
||||
|
||||
func (e *Error) Unwrap() error {
|
||||
return e.InternalError
|
||||
}
|
||||
|
||||
func NewError(code ErrorCode, userMsg string, internalErr error) *Error {
|
||||
statusCode := fiber.StatusInternalServerError
|
||||
switch code {
|
||||
case ErrNotFound:
|
||||
statusCode = fiber.StatusNotFound
|
||||
case ErrInvalidInput:
|
||||
statusCode = fiber.StatusBadRequest
|
||||
case ErrInternal:
|
||||
statusCode = fiber.StatusInternalServerError
|
||||
case ErrForbidden:
|
||||
statusCode = fiber.StatusForbidden
|
||||
case ErrUnauthorized:
|
||||
statusCode = fiber.StatusUnauthorized
|
||||
}
|
||||
|
||||
return &Error{
|
||||
Code: code,
|
||||
UserMessage: userMsg,
|
||||
InternalError: internalErr,
|
||||
StatusCode: statusCode,
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorHandler(c *fiber.Ctx, err error) error {
|
||||
var e *Error
|
||||
if errors.As(err, &e) {
|
||||
return c.Status(e.StatusCode).JSON(fiber.Map{"message": e.UserMessage})
|
||||
}
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "An unexpected error occurred"})
|
||||
}
|
||||
50
utils/filter.go
Normal file
50
utils/filter.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"relay-server/config"
|
||||
)
|
||||
|
||||
func IsValidUsername(username interface{}) bool {
|
||||
// Checks if username is type of string
|
||||
strUsername, ok := username.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
match, _ := regexp.MatchString(config.USERNAME_REGEX, strUsername)
|
||||
if !match {
|
||||
return false
|
||||
}
|
||||
// Checks if username length is valid
|
||||
if len(strUsername) < config.MIN_USERNAME_LENGTH {
|
||||
return false
|
||||
}
|
||||
if len(strUsername) > config.MAX_USERNAME_LENGTH {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func IsValidPassword(password interface{}) bool {
|
||||
strPassword, ok := password.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(strPassword) < config.MIN_PASSWORD_LENGTH {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(strPassword) > config.MAX_PASSWORD_LENGTH {
|
||||
return false
|
||||
}
|
||||
|
||||
match, _ := regexp.MatchString(config.PASSWORD_REGEX, strPassword)
|
||||
if !match {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
54
utils/token.go
Normal file
54
utils/token.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
"os"
|
||||
"relay-server/model"
|
||||
)
|
||||
|
||||
func GenerateToken(userID uuid.UUID, username string) (string, error) {
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"username": username,
|
||||
})
|
||||
signedToken, err := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
|
||||
if err != nil {
|
||||
return "", NewError(ErrInternal, "Failed to generate token", err)
|
||||
}
|
||||
return signedToken, nil
|
||||
}
|
||||
|
||||
func ValidateToken(tokenString string) (*model.UserClaims, error) {
|
||||
secretKey := os.Getenv("JWT_SECRET")
|
||||
|
||||
// Parse the token
|
||||
token, err := jwt.ParseWithClaims(tokenString, &model.UserClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
// Validate the signing method
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
|
||||
return []byte(secretKey), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse token: %v", err)
|
||||
}
|
||||
|
||||
// Check if the token is valid
|
||||
if !token.Valid {
|
||||
return nil, errors.New("invalid token")
|
||||
}
|
||||
|
||||
// Type assert the claims
|
||||
claims, ok := token.Claims.(*model.UserClaims)
|
||||
if !ok {
|
||||
return nil, errors.New("failed to parse claims")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
Reference in New Issue
Block a user