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")
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
// AGENT: Argon2id is used instead of bcrypt because the contract allows 128
|
||||
// Unicode code points and bcrypt silently truncates at 72 bytes. Salt and
|
||||
// parameters are stored inside the encoded hash string.
|
||||
package auth
|
||||
|
||||
import (
|
||||
@@ -38,8 +35,6 @@ func HashPassword(password string) (string, error) {
|
||||
base64.RawStdEncoding.EncodeToString(key)), nil
|
||||
}
|
||||
|
||||
// VerifyPassword recomputes the digest with the stored parameters and compares
|
||||
// in constant time.
|
||||
func VerifyPassword(encoded, password string) (bool, error) {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[1] != "argon2id" {
|
||||
@@ -57,13 +52,19 @@ func VerifyPassword(encoded, password string) (bool, error) {
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil {
|
||||
return false, ErrInvalidHash
|
||||
}
|
||||
// argon2.IDKey panics when the cost parameters are outside its supported
|
||||
// range. A corrupted row or an attacker-supplied encoding must be rejected
|
||||
// with ErrInvalidHash instead of taking the process down.
|
||||
if iterations < 1 || parallelism < 1 || memory < 8*uint32(parallelism) {
|
||||
return false, ErrInvalidHash
|
||||
}
|
||||
|
||||
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
if err != nil {
|
||||
if err != nil || len(salt) == 0 {
|
||||
return false, ErrInvalidHash
|
||||
}
|
||||
expected, err := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if err != nil {
|
||||
if err != nil || len(expected) == 0 {
|
||||
return false, ErrInvalidHash
|
||||
}
|
||||
|
||||
|
||||
@@ -44,3 +44,21 @@ func TestPasswordHashIsSalted(t *testing.T) {
|
||||
t.Fatal("invalid encoded hash was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPasswordRejectsMalformedCostAndEmptyDigest(t *testing.T) {
|
||||
malformed := []string{
|
||||
// Empty digest would make argon2 derive a zero-length key.
|
||||
"$argon2id$v=19$m=65536,t=2,p=4$c2FsdA$",
|
||||
// Zero cost parameters are outside argon2's supported range and would
|
||||
// otherwise panic inside the KDF.
|
||||
"$argon2id$v=19$m=65536,t=0,p=4$c2FsdA$YWJjZA",
|
||||
"$argon2id$v=19$m=65536,t=2,p=0$c2FsdA$YWJjZA",
|
||||
"$argon2id$v=19$m=1,t=2,p=4$c2FsdA$YWJjZA",
|
||||
}
|
||||
for _, encoded := range malformed {
|
||||
ok, err := VerifyPassword(encoded, "same-password-123")
|
||||
if err == nil || ok {
|
||||
t.Fatalf("VerifyPassword(%q) = (%v, %v), want ErrInvalidHash", encoded, ok, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/session"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user