brunel/middleware/auth.go

31 lines
652 B
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package middleware
import (
"brunel/auth"
"strings"
"github.com/gofiber/fiber/v2"
)
func NewAuth() fiber.Handler {
return func(c *fiber.Ctx) error {
tokenPlusUsername := c.Cookies("pt")
if tokenPlusUsername == "" {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
split := strings.Split(tokenPlusUsername, ":")
token := split[0]
username := split[1]
ok, suser := auth.CheckSessionToken(token)
if !ok {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
if suser != username {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
return c.Next()
}
}