fix backend audit findings (auth, upload, sync, data, ocr, contract)
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s

This commit is contained in:
m
2026-09-21 19:59:52 +02:00
parent df7dc76405
commit a6b7f2002b
60 changed files with 3414 additions and 412 deletions
+18 -4
View File
@@ -3,6 +3,7 @@ package auth
import (
"crypto/sha256"
"errors"
"regexp"
"time"
"aidanwoods.dev/go-paseto"
@@ -14,6 +15,8 @@ const TokenTTL = 7 * 24 * time.Hour
var ErrInvalidToken = errors.New("invalid token")
var hex32Pattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant)
// et le device (claim secondaire, porté mais NON autorisant seul).
type Identity struct {
@@ -39,29 +42,40 @@ func NewManager(secret string) (*Manager, error) {
// Issue émet un token lié à un utilisateur ET à un device.
func (m *Manager) Issue(userID, deviceID string) (string, error) {
token, _, err := m.IssueWithExpiry(userID, deviceID)
return token, err
}
// IssueWithExpiry émet un token et retourne son expiration réelle (le `exp`
// embarqué). Les appelants doivent exposer expiresAt, jamais le recalculer
// via Now()+TTL (dérive d'horloge, cf. FEEDBACK A7).
func (m *Manager) IssueWithExpiry(userID, deviceID string) (string, time.Time, error) {
now := time.Now()
expiresAt := now.Add(TokenTTL)
token := paseto.NewToken()
token.SetIssuedAt(now)
token.SetNotBefore(now)
token.SetExpiration(now.Add(TokenTTL))
token.SetExpiration(expiresAt)
token.SetSubject(userID)
// v1.6.0 : SetString n'expose pas d'erreur (panic si non sérialisable).
token.SetString("device_id", deviceID)
return token.V4Encrypt(m.key, nil), nil
return token.V4Encrypt(m.key, nil), expiresAt, nil
}
// Verify décode et contrôle le token, retourne l'identité (user + device).
// Les deux identifiants doivent être 32-hex (cf. docs/api-v1.md) — rejet
// sinon pour préserver les invariants UNIQUE(device_id,operation_id)/CHECK.
func (m *Manager) Verify(signed string) (Identity, error) {
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
if err != nil {
return Identity{}, ErrInvalidToken
}
userID, err := parsed.GetSubject()
if err != nil || userID == "" {
if err != nil || !hex32Pattern.MatchString(userID) {
return Identity{}, ErrInvalidToken
}
deviceID, err := parsed.GetString("device_id")
if err != nil || deviceID == "" {
if err != nil || !hex32Pattern.MatchString(deviceID) {
return Identity{}, ErrInvalidToken
}
return Identity{UserID: userID, DeviceID: deviceID}, nil