chore: build
ci-api / test (push) Failing after 8m7s
ci-web / test (push) Failing after 5m5s

This commit is contained in:
Xor290
2026-09-20 12:18:33 +02:00
parent 8f4c7fa47a
commit 919c807004
174 changed files with 9669 additions and 1308 deletions
@@ -1,6 +1,7 @@
package auth
import (
"context"
"errors"
"net/http"
@@ -18,28 +19,123 @@ const (
customerRefreshCookiePath = "/api/auth/customer"
)
// UserCreator is the minimal slice of users.Service that customer
// registration needs (create-only, consumer-defined per the project's Go
// interface convention).
type UserCreator interface {
Create(ctx context.Context, username, password, role string) (*users.User, error)
}
// AccountsGate reports whether the admin has turned on the customer login
// page and the customer registration page. CustomerRegistrationEnabled is a
// sub-toggle of CustomerLoginEnabled (site.Settings.CustomerLoginEnabled /
// CustomerRegistrationEnabled): an admin can allow login for already-existing
// accounts while keeping self-service registration closed, but turning login
// off always closes registration too, since a newly registered account is
// signed in immediately.
type AccountsGate interface {
CustomerLoginEnabled(ctx context.Context) (bool, error)
CustomerRegistrationEnabled(ctx context.Context) (bool, error)
}
// CustomerHandler mirrors AdminHandler but issues/accepts only
// customer-audience tokens. Not mounted by main.go in this phase (no
// customer-facing module needs it yet) — it exists so the future
// cart/orders module can activate customer auth without touching the admin
// code path at all.
// customer-audience tokens, and additionally gates on AccountsGate since
// this space, unlike the admin one, can be turned off entirely by the
// site's admin.
type CustomerHandler struct {
service *Service
users UserCreator
gate AccountsGate
cookieSecure bool
}
func NewCustomerHandler(service *Service, cookieSecure bool) *CustomerHandler {
return &CustomerHandler{service: service, cookieSecure: cookieSecure}
func NewCustomerHandler(service *Service, users UserCreator, gate AccountsGate, cookieSecure bool) *CustomerHandler {
return &CustomerHandler{service: service, users: users, gate: gate, cookieSecure: cookieSecure}
}
func (h *CustomerHandler) loginEnabledOrAbort(c *gin.Context) bool {
enabled, err := h.gate.CustomerLoginEnabled(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to check customer login setting"})
return false
}
if !enabled {
c.JSON(http.StatusForbidden, gin.H{"error": "customer login is currently disabled"})
return false
}
return true
}
func (h *CustomerHandler) registrationEnabledOrAbort(c *gin.Context) bool {
enabled, err := h.gate.CustomerRegistrationEnabled(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to check customer registration setting"})
return false
}
if !enabled {
c.JSON(http.StatusForbidden, gin.H{"error": "customer registration is currently disabled"})
return false
}
return true
}
type registerRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required,min=8"`
}
func (h *CustomerHandler) Register(c *gin.Context) {
// Registration is a sub-toggle of login: creating an account is pointless
// (and issuePair below would hand out a session) if the admin has turned
// customer login off, so both gates must pass.
if !h.loginEnabledOrAbort(c) {
return
}
if !h.registrationEnabledOrAbort(c) {
return
}
var req registerRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
return
}
user, err := h.users.Create(c.Request.Context(), req.Username, req.Password, users.RoleCustomer)
if err != nil {
if errors.Is(err, users.ErrUsernameTaken) {
c.JSON(http.StatusConflict, gin.H{"error": "username already taken"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create account"})
return
}
pair, err := h.service.issuePair(c.Request.Context(), security.AudienceCustomer, user)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "account created but failed to sign in"})
return
}
h.setRefreshCookie(c, pair.RefreshToken)
c.JSON(http.StatusCreated, accessTokenResponse{
AccessToken: pair.AccessToken,
User: &authUserResponse{ID: user.ID, Username: user.Username, Role: user.Role},
})
}
func (h *CustomerHandler) Login(c *gin.Context) {
if !h.loginEnabledOrAbort(c) {
return
}
var req loginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
return
}
pair, user, err := h.service.Login(c.Request.Context(), security.AudienceCustomer, users.RoleCustomer, req.Email, req.Password)
pair, user, err := h.service.Login(c.Request.Context(), security.AudienceCustomer, users.RoleCustomer, req.Username, req.Password)
if err != nil {
switch {
case errors.Is(err, ErrInvalidCredentials), errors.Is(err, ErrAccountDisabled):
@@ -53,7 +149,7 @@ func (h *CustomerHandler) Login(c *gin.Context) {
h.setRefreshCookie(c, pair.RefreshToken)
c.JSON(http.StatusOK, accessTokenResponse{
AccessToken: pair.AccessToken,
User: &authUserResponse{ID: user.ID, Email: user.Email, Role: user.Role},
User: &authUserResponse{ID: user.ID, Username: user.Username, Role: user.Role},
})
}
@@ -98,7 +194,7 @@ func (h *CustomerHandler) Me(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load user"})
return
}
c.JSON(http.StatusOK, authUserResponse{ID: user.ID, Email: user.Email, Role: user.Role})
c.JSON(http.StatusOK, authUserResponse{ID: user.ID, Username: user.Username, Role: user.Role})
}
func (h *CustomerHandler) setRefreshCookie(c *gin.Context, token string) {