package handler import ( "context" "crypto/rand" "encoding/base64" "encoding/json" "net/http" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/rs/zerolog/log" "github.com/warmbly/warmbly/internal/api/middleware" "github.com/warmbly/warmbly/internal/app/brandkit" "github.com/warmbly/warmbly/internal/app/customhost" "github.com/warmbly/warmbly/internal/app/portalhandoff" "github.com/warmbly/warmbly/internal/config" "github.com/warmbly/warmbly/internal/errx" ) // The branded dashboard on an agency's verified portal domain: what it shows, and the challenge bridge its sign-in embeds. // captchaFrameAvailable reports whether the API can render a challenge for a portal's sign-in. func captchaFrameAvailable() bool { return config.CaptchaProvider() != "none" && config.TurnstileSiteKey() != "" } // PortalInfo serves GET /v1/auth/portal?host=: the agency brand a dashboard on a verified portal domain shows. func (h *Handler) PortalInfo(c *gin.Context) { host := customhost.Normalize(c.Query("host")) view, err := h.BrandService.PortalView(c.Request.Context(), host, captchaFrameAvailable()) if err != nil { // A failed lookup is not an answer: nothing may cache it as "not a portal". log.Error().Err(err).Msg("portal lookup failed") errx.JSON(c, errx.InternalError()) return } if view == nil { view = &brandkit.PortalView{} } c.Header("Cache-Control", "no-store") c.JSON(http.StatusOK, view) } // InternalGetPortal answers the portal service: 200 when host is a verified agency portal domain, else 404. func (h *Handler) InternalGetPortal(c *gin.Context) { portals := h.BrandService.Portals() if portals == nil { c.Status(http.StatusNotFound) return } p, err := portals.Lookup(c.Request.Context(), c.Param("host")) if err != nil { c.Status(http.StatusServiceUnavailable) return } if p == nil { c.Status(http.StatusNotFound) return } c.JSON(http.StatusOK, gin.H{"host": p.Host, "agency_id": p.AgencyID}) } // CaptchaFrame serves GET /v1/auth/captcha-frame?origin=: the challenge, rendered on the API's own host, framed only by a verified portal origin. func (h *Handler) CaptchaFrame(c *gin.Context) { origin := config.PortalOrigin(c.Query("origin")) if origin == "" || !captchaFrameAvailable() { c.Data(http.StatusBadRequest, "text/plain; charset=utf-8", []byte("This page can only be opened from a verified portal domain.")) return } nonceRaw := make([]byte, 18) if _, err := rand.Read(nonceRaw); err != nil { c.Status(http.StatusInternalServerError) return } nonce := base64.StdEncoding.EncodeToString(nonceRaw) h2 := c.Writer.Header() // The one framable page on the API: framed by exactly the portal it was asked for. h2.Del("X-Frame-Options") h2.Set("Content-Security-Policy", "default-src 'none'; script-src 'nonce-"+nonce+"' https://challenges.cloudflare.com; "+ "style-src 'nonce-"+nonce+"'; frame-src https://challenges.cloudflare.com; connect-src https://challenges.cloudflare.com; "+ "frame-ancestors "+origin+"; base-uri 'none'; form-action 'none'") h2.Set("Cache-Control", "no-store") originJS, _ := json.Marshal(origin) keyJS, _ := json.Marshal(config.TurnstileSiteKey()) page := `