Files
warmbly/internal/api/handler/auth.go
T
Matthew Meszaros 1e37fb6aa3 feat: capture and score a signup's origin instead of discarding it (#234)
* feat: give an organization one fused abuse posture, because every existing control watches a single subject and an actor slightly wrong on several axes sits under all of them: organizations gains risk_state, risk_score, risk_reason and an append-only risk_signals evidence blob, modelled on the warmup participant health machine that already works rather than a second vocabulary for the same idea; restricted cuts per-mailbox cold volume to a quarter and forces the free warmup pool so a risky tenant cannot spend the paid pool's shared reputation, suspended stops sending at the send gate, and watch deliberately changes nothing a customer can feel so evidence accumulates before anything is taken away; an operator's suspension outranks the derived band so a detector clearing cannot release a workspace a human suspended, transitions ride the audit spine to every teammate's dashboard, a banner says which limit is active and why rather than letting volume drop silently, and the posture never travels in a workspace archive since it is one platform's verdict reached from evidence the destination never saw

* feat: make the suspension actually stop sending, and emit the audit transitions the PR claimed: emailsend.SendEmail is only the manual and API path, so campaign and warmup sends went nowhere near the gate and a suspended workspace kept sending on its schedule, while the restricted multiplier floored every mailbox at one a day which quietly turned suspension into a trickle rather than a stop; the campaign scheduler now defers the whole campaign with a reason and the warmup task skips as org-suspended, since warmup is outbound mail from the same domains; separately the band change emitted no audit entry at all despite the entity type and the frontend spine entry both existing, so no banner moved for a teammate and there was no trail of who was restricted when, and only a real transition is logged so a detector re-recording the same finding cannot fill the feed; one of my own live tests also asserted how far out a slot lands, which depends on the hour the suite runs, and now asserts the property it was about

* feat: capture and score a signup's origin instead of discarding it

* feat: document what a signup records, and prove the origin write is on the account path with a seam test rather than only testing the scorer

* chore: drop the em dashes and trim the comments the review flagged
2026-08-28 11:26:44 -07:00

234 lines
5.4 KiB
Go

package handler
import (
"context"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/api/middleware"
"github.com/warmbly/warmbly/internal/app/auth"
"github.com/warmbly/warmbly/internal/errx"
)
const authRequestTimeout = 15 * time.Second
func (h *Handler) LoginStart(c *gin.Context) {
var data auth.AuthData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
resp, err := h.AuthService.LoginStart(ctx, &data, c.ClientIP(), c.Request.UserAgent())
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, resp)
}
func (h *Handler) LoginConfirm(c *gin.Context) {
var data auth.ConfirmData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, err)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
resp, err := h.AuthService.LoginConfirm(ctx, &data, data.Session, c.ClientIP(), c.Request.UserAgent())
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, resp)
}
func (h *Handler) RegistrationStart(c *gin.Context) {
var data auth.AuthData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
resp, err := h.AuthService.RegistrationStart(ctx, &data, auth.SignupOrigin{IP: c.ClientIP(), UserAgent: c.Request.UserAgent()})
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, resp)
}
func (h *Handler) RegistrationConfirm(c *gin.Context) {
var data auth.ConfirmData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, err)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
if err := h.AuthService.RegistrationConfirm(ctx, &data, data.Session, auth.SignupOrigin{IP: c.ClientIP(), UserAgent: c.Request.UserAgent()}); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (h *Handler) RefreshToken(c *gin.Context) {
var data struct {
RefreshToken string `json:"refresh_token"`
}
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
token, err := h.TokenService.RefreshToken(c.Request.Context(), data.RefreshToken)
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, token)
}
func (h *Handler) Logout(c *gin.Context) {
accessToken := middleware.GetAccessToken(c)
if err := h.TokenService.RevokeSession(c.Request.Context(), accessToken); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (h *Handler) LogoutAll(c *gin.Context) {
accessToken := middleware.GetAccessToken(c)
if err := h.TokenService.RevokeAllSession(c.Request.Context(), accessToken); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (h *Handler) GetUser(c *gin.Context) {
userID := middleware.GetUserID(c)
uid, err := uuid.Parse(userID)
if err != nil {
errx.Handle(c, errx.ErrUser)
return
}
ctx := c.Request.Context()
u, xerr := h.UserService.GetUser(ctx, uid)
if xerr != nil {
errx.Handle(c, xerr)
return
}
// Populate the per-user label groups so the frontend can render
// folder/tag pickers on initial page load without three extra
// round-trips. Without this, anything the user created in a
// previous session would disappear after a refresh: the cache
// would optimistic-update from a Create response, but on reload
// the /auth/me payload had empty folders/tags/categories.
if folders, ferr := h.FolderService.List(ctx, uid); ferr == nil {
u.Folders = folders
}
if tags, terr := h.TagService.List(ctx, uid); terr == nil {
u.Tags = tags
}
if cats, cerr := h.CategoryService.List(ctx, uid); cerr == nil {
u.Categories = cats
}
c.JSON(http.StatusOK, u)
}
func (h *Handler) ResetPasswordStart(c *gin.Context) {
var data auth.ResetPasswordStart
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
if err := h.AuthService.ResetPasswordStart(ctx, &data, c.ClientIP()); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusOK)
}
func (h *Handler) ResetPasswordConfirm(c *gin.Context) {
var data auth.ResetPasswordConfirm
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
if err := h.AuthService.ResetPasswordConfirm(ctx, &data, data.Session, c.ClientIP()); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusOK)
}
// ChangePassword updates the signed-in user's password (current + new).
func (h *Handler) ChangePassword(c *gin.Context) {
uid, err := uuid.Parse(middleware.GetUserID(c))
if err != nil {
errx.Handle(c, errx.ErrUnauthorized)
return
}
var data auth.ChangePassword
if berr := c.ShouldBindJSON(&data); berr != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), authRequestTimeout)
defer cancel()
if xerr := h.AuthService.ChangePassword(ctx, uid, currentSessionID(c), &data); xerr != nil {
errx.Handle(c, xerr)
return
}
c.Status(http.StatusOK)
}