package handler import ( "crypto/subtle" "html/template" "net/http" "github.com/gin-gonic/gin" "github.com/warmbly/warmbly/internal/app/mailboxconnect" "github.com/warmbly/warmbly/internal/errx" "github.com/warmbly/warmbly/internal/models" "github.com/warmbly/warmbly/internal/pkg/crypt" "github.com/warmbly/warmbly/internal/pkg/displayname" ) // A sign-in link opens here. The page names the workspace asking, and the // cookie it sets is what the provider callback requires, so the mailbox // connects only in the browser where its owner read this and continued. const connectCookieName = "warmbly_connect" // connectFormTargets are where Continue may redirect: the Google and Microsoft authorize hosts. const connectFormTargets = "https://accounts.google.com https://login.microsoftonline.com" var connectLinkPage = template.Must(template.New("connect-link").Parse(` Connect your mailbox

Connect your {{.Provider}} mailbox

{{.Workspace}} asked you to connect {{if .Email}}{{.Email}}{{else}}a mailbox{{end}}{{if .App}} through {{.App}}{{end}}.

If you continue and sign in, that workspace can send and read mail as this mailbox, and warm it up.

Only continue if you expected this request. If you do not know who sent you this link, close this tab.

`)) func setConnectCookie(c *gin.Context, value string, maxAge int) { http.SetCookie(c.Writer, &http.Cookie{ Name: connectCookieName, Value: value, Path: brokerCookiePath, MaxAge: maxAge, HttpOnly: true, Secure: brokerCookieSecure(c.Request), SameSite: http.SameSiteLaxMode, }) } func renderConnectNotice(c *gin.Context, title string, xerr *errx.Error) { status, body := http.StatusBadRequest, mailboxconnect.ErrLinkGone.Message if xerr != nil && xerr.Code != errx.Internal { body = xerr.Message } else if xerr != nil { status, body = http.StatusInternalServerError, "Something went wrong. Open the link again in a moment." } renderNotice(c, status, title, body) } // MailboxConnectLinkPage is GET /addresses/link/:token. func (h *Handler) MailboxConnectLinkPage(c *gin.Context) { c.Header("Cache-Control", "no-store") c.Header("Referrer-Policy", "no-referrer") if h.MailboxConnectService == nil { renderConnectNotice(c, "Link unavailable", nil) return } token := c.Param("token") view, xerr := h.MailboxConnectService.Describe(c.Request.Context(), token) if xerr != nil { renderConnectNotice(c, "Link unavailable", xerr) return } binding, _ := c.Cookie(connectCookieName) if len(binding) < 16 { var err error if binding, err = crypt.Nonce(); err != nil { renderConnectNotice(c, "Link unavailable", errx.InternalError()) return } } setConnectCookie(c, binding, 20*60) data := struct{ Provider, Workspace, Email, App, ContinueURL, Token string }{ Provider: brokerProviderName(view.Provider), Workspace: displayname.DisplayableOr(view.Workspace, "A Warmbly workspace"), Email: view.Email, App: displayname.Displayable(view.App), ContinueURL: mailboxconnect.LinkPath + token + "/continue", Token: binding, } // Browsers hold the redirect a form submission ends in to form-action, so the providers' sign-in hosts are listed. c.Header("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'; form-action 'self' "+connectFormTargets) c.Header("Content-Type", "text/html; charset=utf-8") c.Status(http.StatusOK) _ = connectLinkPage.Execute(c.Writer, data) } // MailboxConnectLinkContinue is POST /addresses/link/:token/continue: it binds the sign-in to this browser and opens the provider. func (h *Handler) MailboxConnectLinkContinue(c *gin.Context) { c.Header("Cache-Control", "no-store") c.Header("Referrer-Policy", "no-referrer") if h.MailboxConnectService == nil { renderConnectNotice(c, "Link unavailable", nil) return } // The form posts the cookie's value, which only the page rendered in this browser knows. cookie, _ := c.Cookie(connectCookieName) if cookie == "" || subtle.ConstantTimeCompare([]byte(cookie), []byte(c.PostForm("t"))) != 1 { renderConnectNotice(c, "Sign-in not started", mailboxconnect.ErrLinkBrowser) return } to, xerr := h.MailboxConnectService.Continue(c.Request.Context(), c.Param("token"), cookie) if xerr != nil { renderConnectNotice(c, "Sign-in not started", xerr) return } c.Redirect(http.StatusSeeOther, to) } // finishConnectLink lands a provider callback a sign-in link started. func (h *Handler) finishConnectLink(c *gin.Context, code, state, providerErr string) { c.Header("Cache-Control", "no-store") c.Header("Referrer-Policy", "no-referrer") binding, _ := c.Cookie(connectCookieName) res, xerr := h.MailboxConnectService.Finish(c.Request.Context(), code, state, providerErr, binding) if res != nil && res.Link != nil { // No session here; the link's creator is the actor, as the mailbox's owner of record. if res.Account != nil && h.AuditService != nil { h.AuditService.LogAction(c.Request.Context(), res.Link.OrganizationID, res.Link.CreatedBy, models.AuditActionConnect, models.AuditEntityEmailAccount, &res.Account.ID, c.ClientIP(), c.Request.UserAgent(), nil, map[string]string{ "provider": res.Account.Provider, "email": res.Account.Email, "via": "connect_link", "link_id": res.Link.ID.String(), }) } // The cookie is left to expire: other sign-ins open in this browser still need it. if res.ReturnURL != "" { c.Redirect(http.StatusFound, res.ReturnURL) return } } if xerr != nil { renderConnectNotice(c, "Mailbox not connected", xerr) return } if res != nil && res.WarmupNote != "" { renderNotice(c, http.StatusOK, "Mailbox connected", "Your mailbox is connected, but not all of its warmup applied: "+res.WarmupNote+" The workspace that sent this link can finish it. You can close this tab.") return } renderNotice(c, http.StatusOK, "Mailbox connected", "Your mailbox is connected. You can close this tab.") }