console: invites and notices reach plain email addresses
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7 files changed,
+222
−31
internal/console/authcli.go+22 −12internal/console/authcli_test.go+41 −0internal/console/invite_delivery_test.go+91 −6internal/console/server.go+11 −7internal/console/storenotify.go+9 −4internal/console/storenotify_test.go+46 −0internal/console/templates/team.html+2 −2
diff --git a/internal/console/authcli.go b/internal/console/authcli.go| index 931c1ff5a..ea21aab8f 100644 |
| --- a/internal/console/authcli.go |
| +++ b/internal/console/authcli.go |
| @@ -1317,7 +1317,7 @@ func (s *Server) notifyCLIIssued(address, rawToken string) { |
| } |
| }() |
| grant, err := s.store.GrantToken(address) |
| - if err != nil || grant == "" { |
| + if err != nil { |
| return |
| } |
| label, from := "", "" |
| @@ -1334,19 +1334,29 @@ func (s *Server) notifyCLIIssued(address, rawToken string) { |
| if from == "" { |
| from = "an address the console could not determine" |
| } |
| + subject := "A command line signed in as you on the CARLOS platform" |
| + body := "A terminal was just approved to act as " + address + " on the CARLOS platform.\n\n" + |
| + " terminal: " + label + "\n" + |
| + " from: " + from + "\n" + |
| + " when: " + time.Now().UTC().Format("2006-01-02 15:04 UTC") + "\n\n" + |
| + "It can do anything you can do from the command line, for the next " + |
| + fmt.Sprint(int(CLITokenTTL/(24*time.Hour))) + " days.\n\n" + |
| + "If that was you running `carlos auth login`, nothing to do.\n\n" + |
| + "If it was NOT you — if you clicked a link somebody sent you — revoke it now:\n\n" + |
| + s.cfg.Origin + "/settings\n" |
| + if grant == "" { |
| + // Signed in by email: no grant to send with. Keymail first if the |
| + // address turns out to have a mailbox, then the console's own email. |
| + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| + defer cancel() |
| + if _, err := s.deliver(ctx, address, subject, body, 12*time.Second); err != nil { |
| + log.Printf("console: could not notify %s of a CLI token issue: %v", address, err) |
| + } |
| + return |
| + } |
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| defer cancel() |
| - if _, err := s.km.SendApp(ctx, grant, address, |
| - "A command line signed in as you on the CARLOS platform", |
| - "A terminal was just approved to act as "+address+" on the CARLOS platform.\n\n"+ |
| - " terminal: "+label+"\n"+ |
| - " from: "+from+"\n"+ |
| - " when: "+time.Now().UTC().Format("2006-01-02 15:04 UTC")+"\n\n"+ |
| - "It can do anything you can do from the command line, for the next "+ |
| - fmt.Sprint(int(CLITokenTTL/(24*time.Hour)))+" days.\n\n"+ |
| - "If that was you running `carlos auth login`, nothing to do.\n\n"+ |
| - "If it was NOT you — if you clicked a link somebody sent you — revoke it now:\n\n"+ |
| - s.cfg.Origin+"/settings\n"); err != nil { |
| + if _, err := s.km.SendApp(ctx, grant, address, subject, body); err != nil { |
| log.Printf("console: could not notify %s of a CLI token issue: %v", address, err) |
| } |
| } |
diff --git a/internal/console/authcli_test.go b/internal/console/authcli_test.go| index 94ae6e206..8074b5acb 100644 |
| --- a/internal/console/authcli_test.go |
| +++ b/internal/console/authcli_test.go |
| @@ -278,6 +278,47 @@ func TestCLIIssuanceNotifiesTheUser(t *testing.T) { |
| } |
| } |
| +// Someone who signs in by email has no Keymail grant. The issuance notice |
| +// still reaches them, by the console mailer; someone with a grant still gets |
| +// the sealed Keymail send and no email. |
| +func TestCLIIssuedNoticeByEmail(t *testing.T) { |
| + fx, m := newEmailFixture(t, true) |
| + fx.srv.cliNotified = make(chan struct{}, 2) |
| + wait := func() { |
| + t.Helper() |
| + select { |
| + case <-fx.srv.cliNotified: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("notifyCLIIssued never finished") |
| + } |
| + } |
| + |
| + fx.srv.notifyCLIIssued("new@example.com", "raw-token") |
| + wait() |
| + sent := m.sentCopy() |
| + if len(sent) != 1 { |
| + t.Fatalf("console mailer got %d messages, want 1: %q", len(sent), sent) |
| + } |
| + to, body, _ := strings.Cut(sent[0], "|") |
| + if to != "new@example.com" { |
| + t.Fatalf("notice went to %q", to) |
| + } |
| + for _, want := range []string{"A terminal was just approved to act as new@example.com", "/settings", "revoke"} { |
| + if !strings.Contains(body, want) { |
| + t.Fatalf("notice missing %q:\n%s", want, body) |
| + } |
| + } |
| + |
| + if err := fx.st.SetGrantToken("granted@example.com", "grant-tok", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.notifyCLIIssued("granted@example.com", "raw-token-2") |
| + wait() |
| + if got := m.sentCopy(); len(got) != 1 { |
| + t.Fatalf("a grant holder was also emailed: %q", got) |
| + } |
| +} |
| + |
| // X-Forwarded-For is only believed from a peer we put there. Otherwise one |
| // caller presents a fresh "IP" per request and walks past every per-IP limit. |
| func TestClientIPIgnoresUntrustedForwardedFor(t *testing.T) { |
diff --git a/internal/console/invite_delivery_test.go b/internal/console/invite_delivery_test.go| index fbffe785d..3d631b97a 100644 |
| --- a/internal/console/invite_delivery_test.go |
| +++ b/internal/console/invite_delivery_test.go |
| @@ -202,17 +202,28 @@ func TestInviteCopyHelpers(t *testing.T) { |
| t.Errorf("inviteFlashTooManyPerHour = %q, want %q", got, want) |
| } |
| wantSubject := "You've been invited to an account on the CARLOS platform" |
| - wantBody := "paul@keymail.dev invited you to the account \"ops\" on the CARLOS platform.\n\n" + |
| - "Open this link while signed in as sam@keymail.dev to accept. It works once and expires in 7 days:\n\n" + |
| - "https://console.example/invite/tok\n\n" + |
| - "If you weren't expecting this, ignore it. Nothing happens without the link." |
| - gotSubject, gotBody := inviteMessage("paul@keymail.dev", "ops", "sam@keymail.dev", "https://console.example/invite/tok") |
| + wantBody := copyText("email.invite.body", "inviter", "paul@keymail.dev", "account", "ops", |
| + "console", "the CARLOS platform", "to", "sam@keymail.dev", "link", "https://console.example/invite/tok") |
| + gotSubject, gotBody := inviteMessage("paul@keymail.dev", "ops", "sam@keymail.dev", "https://console.example/invite/tok", "the CARLOS platform") |
| if gotSubject != wantSubject { |
| t.Errorf("inviteMessage subject = %q, want %q", gotSubject, wantSubject) |
| } |
| if gotBody != wantBody { |
| t.Errorf("inviteMessage body = %q, want %q", gotBody, wantBody) |
| } |
| + // The approved text itself, so a copy-file edit shows in this diff too. |
| + approved := "paul@keymail.dev invited you to the account \"ops\" on the CARLOS platform.\n\n" + |
| + "Open this link to accept. You'll sign in as sam@keymail.dev first if you aren't already:\n\n" + |
| + "https://console.example/invite/tok\n\n" + |
| + "It works once and expires in 7 days. If you weren't expecting this, ignore it. Nothing happens without the link." |
| + if gotBody != approved { |
| + t.Errorf("inviteMessage body = %q, want the approved text %q", gotBody, approved) |
| + } |
| + // A named console shows in both the subject and the body. |
| + subj, body := inviteMessage("paul@keymail.dev", "ops", "sam@keymail.dev", "https://console.example/invite/tok", "Acme Cloud") |
| + if subj != "You've been invited to an account on Acme Cloud" || !strings.Contains(body, "\" on Acme Cloud.\n") { |
| + t.Errorf("named console: subject %q body %q", subj, body) |
| + } |
| } |
| // inviteAttempts swaps both delivery legs for counters, so a refusal can |
| @@ -351,7 +362,7 @@ func TestInviteRefusesAnythingButOneBareAddress(t *testing.T) { |
| fed, mailer := inviteAttempts(fx) |
| csrf := sessionToken(t, fx, owner) |
| postForm(t, owner, fx.u("/invite"), url.Values{"csrf": {csrf}, "address": {to}}) |
| - if kind, msg := inviteFlashAfter(t, owner, fx.ts.URL); kind != "err" || msg != "That doesn't look like a keymail address." { |
| + if kind, msg := inviteFlashAfter(t, owner, fx.ts.URL); kind != "err" || msg != copyText("team.bad_address") { |
| t.Fatalf("flash = %q %q, want the address refusal", kind, msg) |
| } |
| if pending, _ := fx.st.PendingInvites(OpsAccountID); len(pending) != 0 { |
| @@ -385,3 +396,77 @@ func TestInviteLogsNoMailboxQuietly(t *testing.T) { |
| t.Fatalf("log = %q, still logs a no-mailbox lookup as a keymail failure", got) |
| } |
| } |
| + |
| +// The whole invite path for someone with only an email address: the owner |
| +// invites, the link arrives by email, the signed-out invite page asks them to |
| +// sign in, they sign in with a code, land back on the invite, and accept. |
| +func TestInviteEmailInviteeAcceptsEndToEnd(t *testing.T) { |
| + fx, m := newEmailFixture(t, true) |
| + owner := newBrowser(t) |
| + signUp(t, fx, owner, "code-owner") |
| + lookupAnswers(fx, "new@example.com", http.StatusNotFound) |
| + csrf := sessionToken(t, fx, owner) |
| + if st, _, _ := postForm(t, owner, fx.u("/invite"), url.Values{"csrf": {csrf}, "address": {"new@example.com"}}); st != http.StatusSeeOther { |
| + t.Fatalf("invite = %d", st) |
| + } |
| + if kind, msg := inviteFlashAfter(t, owner, fx.ts.URL); kind != "ok" || !strings.Contains(msg, "emailed") { |
| + t.Fatalf("flash = %q %q, want ok containing %q", kind, msg, "emailed") |
| + } |
| + pending, _ := fx.st.PendingInvites(OpsAccountID) |
| + if len(pending) != 1 { |
| + t.Fatalf("pending invites = %+v", pending) |
| + } |
| + next := "/invite/" + pending[0].Token |
| + sent := m.sentCopy() |
| + if len(sent) != 1 { |
| + t.Fatalf("console mailer got %d messages, want the invitation: %q", len(sent), sent) |
| + } |
| + to, body, _ := strings.Cut(sent[0], "|") |
| + if to != "new@example.com" || !strings.Contains(body, fx.ts.URL+next) || !strings.Contains(body, "You'll sign in as new@example.com first") { |
| + t.Fatalf("invitation email to %q:\n%s", to, body) |
| + } |
| + |
| + // The link, signed out: the home page with the banner and the prefill. |
| + guest := newBrowser(t) |
| + status, _, page := get(t, guest, fx.ts.URL+next) |
| + for _, want := range []string{esc(copyText("home.invite_prompt", "address", "new@example.com")), `value="new@example.com"`} { |
| + if status != http.StatusOK || !strings.Contains(page, want) { |
| + t.Fatalf("signed-out invite page = %d, missing %q:\n%s", status, want, page) |
| + } |
| + } |
| + // Sign in by code; the invite is where it lands. |
| + code, _ := askSignin(t, fx, m, guest, "new@example.com", next) |
| + status, loc, _ := postCode(t, fx, guest, code) |
| + if status != http.StatusSeeOther || loc != next { |
| + t.Fatalf("code sign-in = %d to %q, want 303 to %q", status, loc, next) |
| + } |
| + raw := emailSession(t, fx, guest, "new@example.com") |
| + status, _, page = get(t, guest, fx.ts.URL+next) |
| + if status != http.StatusOK || !strings.Contains(page, "Join ") { |
| + t.Fatalf("invite page after sign-in = %d:\n%s", status, page) |
| + } |
| + if status, _, _ := postForm(t, guest, fx.ts.URL+next, url.Values{"csrf": {raw}}); status != http.StatusSeeOther { |
| + t.Fatalf("accept = %d, want 303", status) |
| + } |
| + if role, err := fx.st.Role(OpsAccountID, "new@example.com"); err != nil || role != RoleMember { |
| + t.Fatalf("role = %q, %v, want member", role, err) |
| + } |
| +} |
| + |
| +func TestTeamPageInviteCopy(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + owner := newBrowser(t) |
| + signUp(t, fx, owner, "code-owner") |
| + status, _, body := get(t, owner, fx.u("/team")) |
| + if status != http.StatusOK { |
| + t.Fatalf("team = %d", status) |
| + } |
| + for _, want := range []string{`placeholder="` + esc(copyText("team.invite_placeholder")) + `"`, esc(copyText("team.invite_note"))} { |
| + if !strings.Contains(body, want) { |
| + t.Fatalf("team page missing %q:\n%s", want, body) |
| + } |
| + } |
| + if strings.Contains(body, "the platform never emails anyone") { |
| + t.Fatal("team page still says the platform never emails anyone") |
| + } |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index 562f6a594..a2dac6423 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1805,6 +1805,9 @@ type page struct { |
| // ConfirmNote is the team page's note on actions that ask you to |
| // confirm it's you (team.confirm_note). |
| ConfirmNote string |
| + // InvitePlaceholder and InviteNote are the team page's invite form |
| + // hints (team.invite_placeholder, team.invite_note). |
| + InvitePlaceholder, InviteNote string |
| Msg string // generic message page body |
| @@ -2506,7 +2509,7 @@ func (s *Server) handleInvite(w http.ResponseWriter, r *http.Request) { |
| // which an email To header reads as two recipients or a display name. |
| // One bare address only. |
| if parsed, err := netmail.ParseAddress(to); !ValidAddress(to) || err != nil || parsed.Address != to { |
| - back("err", "That doesn't look like a keymail address.") |
| + back("err", copyText("team.bad_address")) |
| return |
| } |
| if role, err := s.store.Role(acct.ID, to); err != nil { |
| @@ -2539,7 +2542,7 @@ func (s *Server) handleInvite(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| link := s.cfg.Origin + "/invite/" + url.PathEscape(tok) |
| - subject, body := inviteMessage(addr, acct.Name, to, link) |
| + subject, body := inviteMessage(addr, acct.Name, to, link, s.consoleName()) |
| ctx, cancel := context.WithTimeout(r.Context(), s.inviteTimeout) |
| defer cancel() |
| switch ch, err := s.deliver(ctx, to, subject, body, s.inviteKeymailShare); { |
| @@ -2555,11 +2558,10 @@ func (s *Server) handleInvite(w http.ResponseWriter, r *http.Request) { |
| // inviteMessage is the subject and body for an invitation, whichever channel |
| // carries it (Keymail federation or the console's own email). |
| -func inviteMessage(inviter, account, to, link string) (subject, body string) { |
| - return "You've been invited to an account on the CARLOS platform", |
| - inviter + " invited you to the account \"" + account + "\" on the CARLOS platform.\n\n" + |
| - "Open this link while signed in as " + to + " to accept. It works once and expires in 7 days:\n\n" + |
| - link + "\n\nIf you weren't expecting this, ignore it. Nothing happens without the link." |
| +func inviteMessage(inviter, account, to, link, console string) (subject, body string) { |
| + return "You've been invited to an account on " + console, |
| + copyText("email.invite.body", "inviter", inviter, "account", account, |
| + "console", console, "to", to, "link", link) |
| } |
| func inviteFlashKeymail(to string) string { |
| @@ -3176,6 +3178,8 @@ func (s *Server) handleTeam(w http.ResponseWriter, r *http.Request) { |
| p.Title = "Team" |
| p.ActiveNav = "team" |
| p.ConfirmNote = copyText("team.confirm_note") |
| + p.InvitePlaceholder = copyText("team.invite_placeholder") |
| + p.InviteNote = copyText("team.invite_note") |
| if members, err := s.store.Members(acct.ID); err != nil { |
| log.Printf("console: members: %v", err) |
| } else { |
diff --git a/internal/console/storenotify.go b/internal/console/storenotify.go| index 0cc09515c..0adde9024 100644 |
| --- a/internal/console/storenotify.go |
| +++ b/internal/console/storenotify.go |
| @@ -71,10 +71,6 @@ func (s *Server) notifyStoreDecided(account string, e *objstore.Entitlement) { |
| log.Printf("console: store decision %s: recipient: %v", account, err) |
| return |
| } |
| - if len(run.missing) != 0 { |
| - log.Printf("console: store decision %s: no keymail grant for %s — not notified (they must sign in to the console once)", account, address) |
| - return |
| - } |
| state := "declined" |
| if e.Decision.Approved { |
| state = "approved" |
| @@ -85,6 +81,15 @@ func (s *Server) notifyStoreDecided(account string, e *objstore.Entitlement) { |
| for _, recipient := range run.to { |
| s.sendStoreNotification(recipient, subject, body, "decision "+account) |
| } |
| + // Someone who signed in by email has no grant: the notice goes by |
| + // Keymail federation or the console's own email instead. |
| + for _, missing := range run.missing { |
| + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| + if _, err := s.deliver(ctx, missing, subject, body, 12*time.Second); err != nil { |
| + log.Printf("console: store decision %s: delivery to %s: %v", account, missing, err) |
| + } |
| + cancel() |
| + } |
| } |
| func (s *Server) sendStoreNotification(recipient operatorGrant, subject, body, event string) { |
diff --git a/internal/console/storenotify_test.go b/internal/console/storenotify_test.go| index 7f2bde1b4..39f1de46d 100644 |
| --- a/internal/console/storenotify_test.go |
| +++ b/internal/console/storenotify_test.go |
| @@ -85,6 +85,52 @@ func TestStoreDecisionTellsApplicantWithoutLeakingOperatorFields(t *testing.T) { |
| } |
| } |
| +// An applicant with no Keymail grant gets the decision by email; one with a |
| +// grant still gets it through Keymail and not by email. |
| +func TestStoreDecisionNoticeByEmail(t *testing.T) { |
| + fx := storeNotifyFixture(t) |
| + m := &fakeConsoleMail{ready: true} |
| + fx.srv.consoleMail = m |
| + e := &objstore.Entitlement{ |
| + Applied: &objstore.Applied{By: "Owner@Example.test", Note: "secret applicant note"}, |
| + Decision: &objstore.Decision{Approved: true, By: "decider@example.test", Reason: "secret reason"}, |
| + } |
| + |
| + fx.srv.notifyStoreEntitlement("bab", e) |
| + if len(fx.sent) != 0 { |
| + t.Fatalf("keymail sends without a grant: %+v", fx.sent) |
| + } |
| + sent := m.sentCopy() |
| + if len(sent) != 1 { |
| + t.Fatalf("console mailer got %d messages, want 1: %q", len(sent), sent) |
| + } |
| + to, body, _ := strings.Cut(sent[0], "|") |
| + if to != "owner@example.test" { |
| + t.Fatalf("notice went to %q", to) |
| + } |
| + for _, want := range []string{"approved", "bab", fx.srv.cfg.Origin + "/a/bab/settings"} { |
| + if !strings.Contains(body, want) { |
| + t.Fatalf("notice missing %q:\n%s", want, body) |
| + } |
| + } |
| + for _, secret := range []string{"secret applicant note", "secret reason", "decider@example.test"} { |
| + if strings.Contains(body, secret) { |
| + t.Fatalf("notice leaked %q:\n%s", secret, body) |
| + } |
| + } |
| + |
| + if err := fx.st.SetGrantToken("owner@example.test", "owner-token", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.notifyStoreEntitlement("bab", e) |
| + if len(fx.sent) != 1 || fx.sent[0].token != "owner-token" { |
| + t.Fatalf("grant holder not sent through keymail: %+v", fx.sent) |
| + } |
| + if got := m.sentCopy(); len(got) != 1 { |
| + t.Fatalf("grant holder was also emailed: %q", got) |
| + } |
| +} |
| + |
| func TestStoreNotificationIsBestEffortAndNilSafe(t *testing.T) { |
| fx := storeNotifyFixture(t) |
| fx.srv.cfg.StoreOperators = []string{"ops@keymail.dev"} |
diff --git a/internal/console/templates/team.html b/internal/console/templates/team.html| index c961bfc74..26b054525 100644 |
| --- a/internal/console/templates/team.html |
| +++ b/internal/console/templates/team.html |
| @@ -34,10 +34,10 @@ |
| <h2>Invite a teammate</h2> |
| <form class="signin" method="post" action="{{.Base}}/invite"> |
| <input type="hidden" name="csrf" value="{{.CSRF}}"> |
| - <input type="text" name="address" placeholder="them@keymail.dev"> |
| + <input type="text" name="address" placeholder="{{.InvitePlaceholder}}"> |
| <button type="submit">Send invitation</button> |
| </form> |
| -<p class="small">The invitation arrives in their Keymail Apps inbox — the platform never emails anyone. It works once and expires in 7 days.</p> |
| +<p class="small">{{.InviteNote}}</p> |
| {{if .Invites}} |
| <div class="tablewrap"> |
| <table> |