Files
Laterna/cmd/utils/utils.go
T
Siesta 57592b4987 feat: a lot
i did a lot of work, and i also forgot to add commits regularly. I added the controller routes to create and delete controllers, improved error handling und improved the authentification process. I'm gonna add an websocket next and then I should be hopefully done with the foundations of this project. I may also add anviewControllers route which respons with every available controller
2025-07-20 18:31:38 +02:00

65 lines
1.5 KiB
Go

package utils
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"regexp"
"strconv"
"time"
"github.com/siestaw/laterna/server/cmd/internal/models"
"golang.org/x/crypto/bcrypt"
)
func IDtoInt(id string) (int, error) {
idInt, err := strconv.Atoi(id)
if err != nil {
return 0, errors.New("invalid ID format")
}
if idInt <= 0 {
return 0, errors.New("invalid ID")
}
return idInt, nil
}
func IsValidHexColor(color string) bool {
pattern := `^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$`
match, _ := regexp.MatchString(pattern, color)
return match
}
func GenerateToken() (string, error) {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func HashToken(token string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(token), bcrypt.DefaultCost)
return string(hash), err
}
func ValidateToken(providedToken string, storedHash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(storedHash), []byte(providedToken))
return err == nil
}
func HTTPResponseHandler(w http.ResponseWriter, r *http.Request, status int, message string) {
timestamp := time.Now().Format("2006-01-02_15-04-05")
errResp := models.HTTPResponse{
Timestamp: timestamp,
Status: status,
Text: http.StatusText(status),
Message: message,
Path: r.URL.Path,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(errResp)
}