35 lines
755 B
Go
35 lines
755 B
Go
package middleware
|
|
|
|
import (
|
|
"brunel/auth"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func NewAuth() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
tokenPlusUsername := c.Cookies("pt")
|
|
fmt.Println("cookie", tokenPlusUsername)
|
|
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 {
|
|
fmt.Println("not ok")
|
|
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
|
}
|
|
if suser != username {
|
|
fmt.Println("not suser")
|
|
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
|
}
|
|
|
|
return c.Next()
|
|
}
|
|
}
|