forked from mtusi2026/mtusi2026lab3
feat(app) lab 3 implemented
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package auth implements password hashing, Basic login, Bearer session
|
||||
// middleware and the authentication routes.
|
||||
package auth
|
||||
|
||||
import (
|
||||
@@ -5,16 +7,18 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/model"
|
||||
"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 basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
const bearerChallenge = `Bearer realm="messenger"`
|
||||
const (
|
||||
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
bearerChallenge = `Bearer realm="messenger"`
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store *store.Store
|
||||
@@ -25,9 +29,6 @@ func NewHandler(st *store.Store, sessions *session.Store) *Handler {
|
||||
return &Handler{store: st, sessions: sessions}
|
||||
}
|
||||
|
||||
// AGENT: CreateSession is the only endpoint that accepts Basic auth. It reads
|
||||
// no JSON body and returns the same 401 for an unknown user and a wrong
|
||||
// password, so the response does not reveal which usernames exist.
|
||||
func (h *Handler) CreateSession(c fiber.Ctx) error {
|
||||
username, password, ok := parseBasic(c.Get(fiber.HeaderAuthorization))
|
||||
if !ok {
|
||||
@@ -37,6 +38,11 @@ func (h *Handler) CreateSession(c fiber.Ctx) error {
|
||||
user, err := h.store.GetUserByUsername(c.Context(), username)
|
||||
if err != nil {
|
||||
if common.IsCode(err, common.ErrorCodeNotFound) {
|
||||
// An unknown username returns before Argon2 runs, while a known
|
||||
// username with a wrong password runs VerifyPassword. The branches
|
||||
// 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.SendAppError(c, err)
|
||||
@@ -62,8 +68,6 @@ func (h *Handler) CreateSession(c fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// RevokeSession deletes the current token. The middleware has already proven
|
||||
// that the token exists, so a repeated DELETE is a 401, not a 204.
|
||||
func (h *Handler) RevokeSession(c fiber.Ctx) error {
|
||||
token, ok := common.SessionToken(c)
|
||||
if !ok {
|
||||
@@ -79,8 +83,6 @@ func (h *Handler) RevokeSession(c fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
|
||||
// BearerAuth accepts only an opaque Bearer token. X-User-Id is deliberately
|
||||
// never read: with lab 2 it can neither create nor override an identity.
|
||||
func BearerAuth(sessions *session.Store) fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
header := strings.TrimSpace(c.Get(fiber.HeaderAuthorization))
|
||||
@@ -95,8 +97,6 @@ func BearerAuth(sessions *session.Store) fiber.Handler {
|
||||
if errors.Is(err, session.ErrNotFound) {
|
||||
return common.SendChallenge(c, bearerChallenge, "session expired or revoked")
|
||||
}
|
||||
// AGENT: A dead Redis must not degrade to "anonymous allowed".
|
||||
// Protected calls get a bounded 503 instead of a 401 or a bypass.
|
||||
log.Errorf("session lookup: %v", err)
|
||||
return common.SendError(c, fiber.StatusServiceUnavailable,
|
||||
common.ErrorCodeUnavailable, "session store unavailable")
|
||||
|
||||
Reference in New Issue
Block a user