refactor(app) agent refactoring + a little review

This commit is contained in:
imadev
2026-10-31 20:22:16 +03:00
parent 2ef20e7040
commit 362ba16cc4
25 changed files with 240 additions and 375 deletions
+14 -17
View File
@@ -5,6 +5,7 @@ package auth
import (
"encoding/base64"
"errors"
"log/slog"
"strings"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
@@ -12,7 +13,6 @@ import (
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/session"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
)
const (
@@ -32,7 +32,7 @@ func NewHandler(st *store.Store, sessions *session.Store) *Handler {
func (h *Handler) CreateSession(c fiber.Ctx) error {
username, password, ok := parseBasic(c.Get(fiber.HeaderAuthorization))
if !ok {
return common.SendChallenge(c, basicChallenge, "invalid credentials")
return common.NewChallenge(basicChallenge, "invalid credentials")
}
user, err := h.store.GetUserByUsername(c.Context(), username)
@@ -43,21 +43,20 @@ func (h *Handler) CreateSession(c fiber.Ctx) error {
// therefore take different time, so username enumeration through
// response timing is theoretically possible. The contract only
// requires the same 401 body and challenge.
return common.SendChallenge(c, basicChallenge, "invalid credentials")
return common.NewChallenge(basicChallenge, "invalid credentials")
}
return common.SendAppError(c, err)
return err
}
valid, err := VerifyPassword(user.PasswordHash, password)
if err != nil || !valid {
return common.SendChallenge(c, basicChallenge, "invalid credentials")
return common.NewChallenge(basicChallenge, "invalid credentials")
}
token, expiresAt, err := h.sessions.Create(c.Context(), user.ID)
if err != nil {
log.Errorf("create session: %v", err)
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "session store unavailable")
slog.Error("create session", "err", err)
return common.Unavailable("session store unavailable")
}
return c.Status(fiber.StatusCreated).JSON(model.Session{
@@ -71,13 +70,12 @@ func (h *Handler) CreateSession(c fiber.Ctx) error {
func (h *Handler) RevokeSession(c fiber.Ctx) error {
token, ok := common.SessionToken(c)
if !ok {
return common.SendChallenge(c, bearerChallenge, "authentication required")
return common.NewChallenge(bearerChallenge, "authentication required")
}
if err := h.sessions.Revoke(c.Context(), token); err != nil {
log.Errorf("revoke session: %v", err)
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "session store unavailable")
slog.Error("revoke session", "err", err)
return common.Unavailable("session store unavailable")
}
return c.SendStatus(fiber.StatusNoContent)
@@ -89,17 +87,16 @@ func BearerAuth(sessions *session.Store) fiber.Handler {
scheme, token, found := strings.Cut(header, " ")
token = strings.TrimSpace(token)
if !found || !strings.EqualFold(scheme, "Bearer") || token == "" || strings.ContainsAny(token, " \t") {
return common.SendChallenge(c, bearerChallenge, "authentication required")
return common.NewChallenge(bearerChallenge, "authentication required")
}
userID, err := sessions.Lookup(c.Context(), token)
if err != nil {
if errors.Is(err, session.ErrNotFound) {
return common.SendChallenge(c, bearerChallenge, "session expired or revoked")
return common.NewChallenge(bearerChallenge, "session expired or revoked")
}
log.Errorf("session lookup: %v", err)
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "session store unavailable")
slog.Error("session lookup", "err", err)
return common.Unavailable("session store unavailable")
}
c.Locals(common.KeyUserID, userID)
-17
View File
@@ -1,17 +0,0 @@
package auth
import (
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/session"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
"github.com/gofiber/fiber/v3"
)
func RegisterPublicRoutes(router fiber.Router, st *store.Store, sessions *session.Store) {
handler := NewHandler(st, sessions)
router.Post("/auth/sessions", handler.CreateSession)
}
func RegisterProtectedRoutes(router fiber.Router, st *store.Store, sessions *session.Store) {
handler := NewHandler(st, sessions)
router.Delete("/auth/sessions/current", handler.RevokeSession)
}