console: keymail discovery, and federation falls through to SES only on a definite no
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7 files changed,
+395
−34
internal/console/delivery.go+78 −0internal/console/delivery_test.go+199 −0internal/console/invite_delivery_test.go+12 −3internal/console/keymail.go+11 −0internal/console/keymail_test.go+42 −5internal/console/keymailfed.go+47 −1internal/console/server.go+6 −25
diff --git a/internal/console/delivery.go b/internal/console/delivery.go| new file mode 100644 |
| index 000000000..2debd9e2b |
| --- /dev/null |
| +++ b/internal/console/delivery.go |
| @@ -0,0 +1,78 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "log" |
| + "strings" |
| + "time" |
| +) |
| + |
| +// deliveryChannel names how deliver got a message out. |
| +type deliveryChannel string |
| + |
| +const ( |
| + deliveredKeymail deliveryChannel = "keymail" |
| + deliveredEmail deliveryChannel = "email" |
| +) |
| + |
| +// domainOf is the lowercased part of addr after its last "@", "" if none. |
| +func domainOf(addr string) string { |
| + i := strings.LastIndex(addr, "@") |
| + if i < 0 { |
| + return "" |
| + } |
| + return strings.ToLower(addr[i+1:]) |
| +} |
| + |
| +// deliver sends one message to an address the person did not just type (an |
| +// invite, a notice): Keymail federation first, then the console mailer (SES). |
| +// The fall-through rule (spec 4.3): |
| +// |
| +// - a definite "no mailbox" (ErrNoKeymailMailbox) goes to the mailer; |
| +// - an unreachable lookup stops when the address is at the Keymail host |
| +// (it IS a Keymail mailbox, so SES must not be used), and otherwise goes |
| +// to the mailer; |
| +// - a block, a price above the cap, or any failure after a successful |
| +// lookup stops with that error: the address is a Keymail mailbox. |
| +// |
| +// keymailShare bounds Keymail's part of ctx so the mailer keeps the rest. |
| +// On error the channel is "". |
| +func (s *Server) deliver(ctx context.Context, to, subject, body string, keymailShare time.Duration) (deliveryChannel, error) { |
| + if s.sendFederated != nil { |
| + kctx, cancel := context.WithTimeout(ctx, keymailShare) |
| + _, err := s.sendFederated(kctx, to, subject, body) |
| + cancel() |
| + if err == nil { |
| + return deliveredKeymail, nil |
| + } |
| + switch { |
| + case IsKeymailBlocked(err): |
| + // The recipient blocked the console. That is their answer; |
| + // email to the same address would go round it. |
| + log.Printf("console: delivery to %s: the recipient blocked the console on keymail; not emailing", to) |
| + return "", err |
| + case errors.Is(err, ErrNoKeymailMailbox): |
| + log.Printf("console: delivery to %s: no keymail mailbox, trying email", to) |
| + case errors.Is(err, ErrKeymailUnreachable): |
| + if s.km != nil && domainOf(to) == s.km.Host() { |
| + log.Printf("console: delivery to %s: keymail lookup unreachable for a keymail address; not emailing: %v", to, err) |
| + return "", err |
| + } |
| + log.Printf("console: delivery to %s: keymail lookup unreachable, trying email: %v", to, err) |
| + default: |
| + // ErrFederationPriceTooHigh, or a failure after the lookup |
| + // said the mailbox exists. |
| + log.Printf("console: delivery to %s by keymail: %v", to, err) |
| + return "", err |
| + } |
| + } |
| + if s.consoleMail == nil { |
| + return "", errors.New("console: no email sender is configured") |
| + } |
| + if err := s.consoleMail.Send(ctx, to, subject, body); err != nil { |
| + log.Printf("console: delivery to %s by email: %v", to, err) |
| + return "", err |
| + } |
| + return deliveredEmail, nil |
| +} |
diff --git a/internal/console/delivery_test.go b/internal/console/delivery_test.go| new file mode 100644 |
| index 000000000..a55e3db60 |
| --- /dev/null |
| +++ b/internal/console/delivery_test.go |
| @@ -0,0 +1,199 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net/url" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestHasMailboxClassification(t *testing.T) { |
| + fk := newFakeKeymail(t, "paul@keymail.dev") |
| + k := fedClient(t, fk) |
| + fk.lookupStatus["a@example.com"] = 200 |
| + fk.lookupStatus["b@example.com"] = 400 |
| + fk.lookupStatus["c@example.com"] = 404 |
| + fk.lookupStatus["d@example.com"] = 409 |
| + fk.lookupStatus["e@example.com"] = 500 |
| + fk.lookupStatus["f@example.com"] = 503 |
| + cases := []struct { |
| + addr string |
| + want bool |
| + unreachable bool |
| + }{ |
| + {"a@example.com", true, false}, |
| + {"b@example.com", false, false}, |
| + {"c@example.com", false, false}, |
| + {"d@example.com", false, false}, |
| + {"e@example.com", false, true}, |
| + {"f@example.com", false, true}, |
| + {"sam@keymail.dev", true, false}, // the fake's ordinary answer |
| + } |
| + for _, c := range cases { |
| + got, err := k.HasMailbox(context.Background(), c.addr) |
| + if got != c.want || errors.Is(err, ErrKeymailUnreachable) != c.unreachable || (err != nil) != c.unreachable { |
| + t.Errorf("HasMailbox(%s) = %v, %v; want %v, unreachable=%v", c.addr, got, err, c.want, c.unreachable) |
| + } |
| + } |
| + // A lookup that stalls past the caller's deadline is unreachable, not a no. |
| + fk.lookupStall["slow@example.com"] = 3 * time.Second |
| + fk.lookupStatus["slow@example.com"] = 200 |
| + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| + defer cancel() |
| + start := time.Now() |
| + got, err := k.HasMailbox(ctx, "slow@example.com") |
| + if got || !errors.Is(err, ErrKeymailUnreachable) { |
| + t.Errorf("stalled HasMailbox = %v, %v; want false, ErrKeymailUnreachable", got, err) |
| + } |
| + if d := time.Since(start); d > 2*time.Second { |
| + t.Errorf("stalled HasMailbox took %v", d) |
| + } |
| +} |
| + |
| +func TestKeymailHost(t *testing.T) { |
| + for in, want := range map[string]string{ |
| + "https://keymail.dev": "keymail.dev", |
| + "https://keymail.dev/": "keymail.dev", |
| + "http://127.0.0.1:8123": "127.0.0.1", |
| + "https://Keymail.Dev:8443": "keymail.dev", |
| + } { |
| + if got := NewKeymail(in, "https://console.example").Host(); got != want { |
| + t.Errorf("Host(%q) = %q, want %q", in, got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestSendFederatedClassifiesLookupErrors(t *testing.T) { |
| + fk := newFakeKeymail(t, "paul@keymail.dev") |
| + fk.lookupStatus["a@example.com"] = 409 |
| + fk.lookupStatus["b@example.com"] = 500 |
| + k := fedClient(t, fk) |
| + if _, err := k.SendFederated(context.Background(), "a@example.com", "s", "b"); !errors.Is(err, ErrNoKeymailMailbox) || errors.Is(err, ErrKeymailUnreachable) { |
| + t.Errorf("409: err = %v, want ErrNoKeymailMailbox only", err) |
| + } |
| + _, err := k.SendFederated(context.Background(), "b@example.com", "s", "b") |
| + if !errors.Is(err, ErrKeymailUnreachable) || errors.Is(err, ErrNoKeymailMailbox) { |
| + t.Errorf("500: err = %v, want ErrKeymailUnreachable only", err) |
| + } |
| + var ke *keymailError |
| + if !errors.As(err, &ke) || ke.Status != 500 { |
| + t.Errorf("500: err = %v, want the keymailError wrapped too", err) |
| + } |
| +} |
| + |
| +// deliverFixture builds a Server whose federation leg is the real |
| +// SendFederated against the fake Keymail, with a recording mailer. |
| +func deliverFixture(t *testing.T) (*consoleFixture, *fakeConsoleMail) { |
| + t.Helper() |
| + fx := newConsoleFixture(t) |
| + mailer := &fakeConsoleMail{} |
| + fx.srv.consoleMail = mailer |
| + return fx, mailer |
| +} |
| + |
| +func TestDeliverChain(t *testing.T) { |
| + kmHost := func(fx *consoleFixture) string { |
| + u, _ := url.Parse(fx.fk.srv.URL) |
| + return u.Hostname() |
| + } |
| + cases := []struct { |
| + name string |
| + to func(fx *consoleFixture) string |
| + setup func(fx *consoleFixture, to string) |
| + noMailer bool |
| + wantChannel deliveryChannel |
| + wantErr func(error) bool |
| + wantFed bool |
| + wantMail bool |
| + }{ |
| + {name: "no mailbox (404) goes to the mailer", |
| + to: func(*consoleFixture) string { return "ghost@keymail.dev" }, |
| + setup: func(fx *consoleFixture, _ string) { fx.fk.noMailbox["ghost"] = true }, |
| + wantChannel: "email", wantMail: true}, |
| + {name: "foreign domain (400) goes to the mailer", |
| + to: func(*consoleFixture) string { return "x@example.com" }, |
| + wantChannel: "email", wantMail: true}, |
| + {name: "unclaimed (409) goes to the mailer", |
| + to: func(*consoleFixture) string { return "x@example.com" }, |
| + setup: func(fx *consoleFixture, to string) { fx.fk.lookupStatus[to] = 409 }, |
| + wantChannel: "email", wantMail: true}, |
| + {name: "federation delivers, mailer untouched", |
| + to: func(*consoleFixture) string { return "sam@keymail.dev" }, |
| + wantChannel: "keymail", wantFed: true}, |
| + {name: "blocked stops", |
| + to: func(*consoleFixture) string { return "sam@keymail.dev" }, |
| + setup: func(fx *consoleFixture, _ string) { fx.fk.blocked["console@"+OriginHost(fx.ts.URL)] = true }, |
| + wantErr: IsKeymailBlocked}, |
| + {name: "price too high stops", |
| + to: func(*consoleFixture) string { return "sam@keymail.dev" }, |
| + setup: func(fx *consoleFixture, _ string) { fx.fk.powBits = maxFederationPowBits + 1 }, |
| + wantErr: func(err error) bool { return errors.Is(err, ErrFederationPriceTooHigh) }}, |
| + {name: "lookup 500 for the keymail host stops", |
| + to: func(fx *consoleFixture) string { return "x@" + kmHost(fx) }, |
| + setup: func(fx *consoleFixture, to string) { fx.fk.lookupStatus[to] = 500 }, |
| + wantErr: func(err error) bool { return errors.Is(err, ErrKeymailUnreachable) }}, |
| + {name: "lookup 500 for another domain goes to the mailer", |
| + to: func(*consoleFixture) string { return "x@example.com" }, |
| + setup: func(fx *consoleFixture, to string) { fx.fk.lookupStatus[to] = 500 }, |
| + wantChannel: "email", wantMail: true}, |
| + {name: "no mailer is an error", |
| + to: func(*consoleFixture) string { return "x@example.com" }, |
| + noMailer: true, |
| + wantErr: func(err error) bool { return err != nil }}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + fx, mailer := deliverFixture(t) |
| + to := c.to(fx) |
| + if c.setup != nil { |
| + c.setup(fx, to) |
| + } |
| + if c.noMailer { |
| + fx.srv.consoleMail = nil |
| + } |
| + ch, err := fx.srv.deliver(context.Background(), to, "subj", "the body", 5*time.Second) |
| + if c.wantErr != nil { |
| + if err == nil || !c.wantErr(err) { |
| + t.Fatalf("err = %v, want the case's error", err) |
| + } |
| + if ch != "" { |
| + t.Errorf("channel = %q on error", ch) |
| + } |
| + } else { |
| + if err != nil { |
| + t.Fatalf("err = %v", err) |
| + } |
| + if ch != c.wantChannel { |
| + t.Errorf("channel = %q, want %q", ch, c.wantChannel) |
| + } |
| + } |
| + if got := len(fx.fk.fedBodies()) > 0; got != c.wantFed { |
| + t.Errorf("federated = %v, want %v", got, c.wantFed) |
| + } |
| + if got := len(mailer.sentCopy()) > 0; got != c.wantMail { |
| + t.Errorf("mailed = %v, want %v", got, c.wantMail) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The keymailShare bounds Keymail's slice of the budget: a stalled lookup for |
| +// a foreign domain leaves time for the mailer. |
| +func TestDeliverKeymailShareLeavesTimeForTheMailer(t *testing.T) { |
| + fx, mailer := deliverFixture(t) |
| + fx.fk.lookupStall["x@example.com"] = 3 * time.Second |
| + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| + defer cancel() |
| + start := time.Now() |
| + ch, err := fx.srv.deliver(ctx, "x@example.com", "s", "b", 200*time.Millisecond) |
| + if err != nil || ch != "email" { |
| + t.Fatalf("deliver = %q, %v; want email", ch, err) |
| + } |
| + if d := time.Since(start); d > time.Second { |
| + t.Errorf("deliver took %v", d) |
| + } |
| + if len(mailer.sentCopy()) != 1 { |
| + t.Errorf("mailer calls = %v", mailer.sentCopy()) |
| + } |
| +} |
diff --git a/internal/console/invite_delivery_test.go b/internal/console/invite_delivery_test.go| index 1bd1d9457..2e34e097b 100644 |
| --- a/internal/console/invite_delivery_test.go |
| +++ b/internal/console/invite_delivery_test.go |
| @@ -66,8 +66,17 @@ func TestInviteDelivery(t *testing.T) { |
| wantKind: "ok", wantInMsg: "Keymail", wantFed: true}, |
| {name: "no mailbox, email delivers", to: "sam@gmail.com", mail: &fakeConsoleMail{}, |
| wantKind: "ok", wantInMsg: "emailed", wantMail: true}, |
| - {name: "keymail fails, email delivers", to: "sam@keymail.dev", mail: &fakeConsoleMail{}, |
| + // The address is a Keymail mailbox (the lookup said so); a price above |
| + // the cap is not a reason to email it (spec 4.3). |
| + {name: "price too high is final", to: "sam@keymail.dev", mail: &fakeConsoleMail{}, |
| keymail: func(fx *consoleFixture) { fx.fk.powBits = maxFederationPowBits + 1 }, |
| + wantKind: "err", wantInMsg: "/invite/"}, |
| + // A Keymail outage for the Keymail host's own address sends nothing. |
| + {name: "keymail host lookup 5xx is final", to: "sam@127.0.0.1", mail: &fakeConsoleMail{}, |
| + keymail: func(fx *consoleFixture) { fx.fk.lookupStatus["sam@127.0.0.1"] = 500 }, |
| + wantKind: "err", wantInMsg: "/invite/"}, |
| + {name: "other domain lookup 5xx falls to email", to: "sam@example.com", mail: &fakeConsoleMail{}, |
| + keymail: func(fx *consoleFixture) { fx.fk.lookupStatus["sam@example.com"] = 500 }, |
| wantKind: "ok", wantInMsg: "emailed", wantMail: true}, |
| // A recipient who blocked the console said no; email to the same |
| // address would go round them. Straight to the hand-off. |
| @@ -148,7 +157,7 @@ func TestInviteFallsBackWhenKeymailHangs(t *testing.T) { |
| fx.srv.inviteKeymailShare = 150 * time.Millisecond |
| fx.srv.sendFederated = func(ctx context.Context, _, _, _ string) (string, error) { |
| <-ctx.Done() |
| - return "", ctx.Err() |
| + return "", fmt.Errorf("%w: %w", ErrKeymailUnreachable, ctx.Err()) |
| } |
| owner := newBrowser(t) |
| signUp(t, fx, owner, "code-owner") |
| @@ -364,7 +373,7 @@ func TestInviteLogsNoMailboxQuietly(t *testing.T) { |
| postForm(t, owner, fx.u("/invite"), url.Values{"csrf": {csrf}, "address": {"sam@gmail.com"}}) |
| log.SetOutput(os.Stderr) |
| got := buf.String() |
| - if !strings.Contains(got, "console: invite to sam@gmail.com: no keymail mailbox, trying email") { |
| + if !strings.Contains(got, "console: delivery to sam@gmail.com: no keymail mailbox, trying email") { |
| t.Fatalf("log = %q, want the quiet no-mailbox line", got) |
| } |
| if strings.Contains(got, "by keymail:") { |
diff --git a/internal/console/keymail.go b/internal/console/keymail.go| index f85ab0f6c..0de8cb89d 100644 |
| --- a/internal/console/keymail.go |
| +++ b/internal/console/keymail.go |
| @@ -69,6 +69,17 @@ func NewKeymail(baseURL, origin string) *Keymail { |
| } |
| } |
| +// Host is the hostname of the configured Keymail deployment (keymail.dev for |
| +// https://keymail.dev), lowercased, no port. An address at this host is a |
| +// Keymail address whatever a lookup says. |
| +func (k *Keymail) Host() string { |
| + u, err := url.Parse(k.base) |
| + if err != nil { |
| + return "" |
| + } |
| + return strings.ToLower(u.Hostname()) |
| +} |
| + |
| // SetSigningKey installs the console's long-lived envelope-signing key |
| // (ECDSA P-256). With a key set the client asks for scope "identify send" |
| // and pins the key into the grant at exchange (keymail requires sign_pub |
diff --git a/internal/console/keymail_test.go b/internal/console/keymail_test.go| index 279e34bce..45b6d3538 100644 |
| --- a/internal/console/keymail_test.go |
| +++ b/internal/console/keymail_test.go |
| @@ -71,6 +71,12 @@ type fakeKeymail struct { |
| senders map[string]string |
| registrations int |
| challenges map[string]fakeChallenge |
| + |
| + // lookupStatus makes the lookup for one full address answer that HTTP |
| + // status (400/404/409/500...) instead of its usual 200; lookupStall makes |
| + // it hold the answer for that long (or until the caller gives up). |
| + lookupStatus map[string]int |
| + lookupStall map[string]time.Duration |
| } |
| // sentBodies is every message body the fake has accepted — how a test proves |
| @@ -95,10 +101,12 @@ func newFakeKeymail(t *testing.T, address string) *fakeKeymail { |
| // a test that wants the fail-closed path turns it off explicitly. |
| f := &fakeKeymail{ |
| t: t, address: address, boxPriv: boxPriv, reauth: true, |
| - powBits: 8, |
| - noMailbox: map[string]bool{}, |
| - blocked: map[string]bool{}, |
| - senders: map[string]string{}, |
| + powBits: 8, |
| + noMailbox: map[string]bool{}, |
| + blocked: map[string]bool{}, |
| + lookupStatus: map[string]int{}, |
| + lookupStall: map[string]time.Duration{}, |
| + senders: map[string]string{}, |
| } |
| mux := http.NewServeMux() |
| mux.HandleFunc("GET /.well-known/keymail", f.handleWellKnown) |
| @@ -207,9 +215,38 @@ func (f *fakeKeymail) handleRevoke(w http.ResponseWriter, r *http.Request) { |
| // unclaimed local part 404s as "no such mailbox" one level up from "no such |
| // inbox" — the fake collapses the two hops into one handler. |
| func (f *fakeKeymail) handleLookup(w http.ResponseWriter, r *http.Request) { |
| + addr := r.URL.Query().Get("addr") |
| + f.mu.Lock() |
| + stall, status := f.lookupStall[addr], f.lookupStatus[addr] |
| + f.mu.Unlock() |
| + if stall > 0 { |
| + select { |
| + case <-time.After(stall): |
| + case <-r.Context().Done(): |
| + return |
| + } |
| + } |
| + if status != 0 && status != http.StatusOK { |
| + jsonErr(w, status, http.StatusText(status)) |
| + return |
| + } |
| f.mu.Lock() |
| defer f.mu.Unlock() |
| - addr := r.URL.Query().Get("addr") |
| + if status == http.StatusOK { |
| + // A forced 200 resolves whatever the host (a Keymail-served custom |
| + // domain), so skip the keymail.dev-only routing below. |
| + bits := f.powBits |
| + if _, known := f.senders[r.URL.Query().Get("from")]; known { |
| + bits = 0 |
| + } |
| + w.Header().Set("Content-Type", "application/json") |
| + json.NewEncoder(w).Encode(map[string]any{ |
| + "addr": addr, "sign_pub": "", |
| + "box_pub": base64.StdEncoding.EncodeToString(f.boxPriv.PublicKey().Bytes()), |
| + "pow_bits": bits, |
| + }) |
| + return |
| + } |
| local, host, ok := strings.Cut(addr, "@") |
| if !ok || host != "keymail.dev" { |
| jsonErr(w, http.StatusBadRequest, "could not determine target mailbox") |
diff --git a/internal/console/keymailfed.go b/internal/console/keymailfed.go| index d3618439e..8353fe5f9 100644 |
| --- a/internal/console/keymailfed.go |
| +++ b/internal/console/keymailfed.go |
| @@ -21,6 +21,7 @@ import ( |
| "net/http" |
| "net/url" |
| "sync" |
| + "time" |
| ) |
| var ( |
| @@ -28,6 +29,10 @@ var ( |
| // Keymail host, unknown local part, or a mailbox not yet claimed). The |
| // caller should try another channel. |
| ErrNoKeymailMailbox = errors.New("keymail: no mailbox for that address") |
| + // ErrKeymailUnreachable: the lookup neither found nor ruled out a mailbox |
| + // (timeout, network error, 5xx). Unlike ErrNoKeymailMailbox it is not a |
| + // "no": a caller must not treat the address as non-Keymail on this alone. |
| + ErrKeymailUnreachable = errors.New("keymail: lookup unreachable") |
| // ErrFederationPriceTooHigh: the mailbox asks more proof of work than |
| // maxFederationPowBits. |
| ErrFederationPriceTooHigh = errors.New("keymail: the recipient's proof-of-work price is above the console's cap") |
| @@ -61,6 +66,47 @@ func federationReplyBox() (string, error) { |
| return replyBoxPub, replyBoxErr |
| } |
| +// lookupFailure classifies a federation lookup error. Only Keymail's definite |
| +// answers (400 "could not determine target mailbox", 404 "no such mailbox", |
| +// 409 "inbox not yet claimed") mean there is no mailbox; anything else |
| +// (timeout, network error, 5xx, a bad body) means we do not know. The cause |
| +// stays reachable through errors.As. |
| +func lookupFailure(err error) error { |
| + var ke *keymailError |
| + if errors.As(err, &ke) { |
| + switch ke.Status { |
| + case http.StatusBadRequest, http.StatusNotFound, http.StatusConflict: |
| + return fmt.Errorf("%w: %w", ErrNoKeymailMailbox, err) |
| + } |
| + } |
| + return fmt.Errorf("%w: %w", ErrKeymailUnreachable, err) |
| +} |
| + |
| +// hasMailboxTimeout bounds one discovery lookup (spec 4.1), inside whatever |
| +// the caller's context allows. |
| +const hasMailboxTimeout = 5 * time.Second |
| + |
| +// HasMailbox asks Keymail's public federation lookup whether addr is a |
| +// Keymail mailbox: (true, nil) on 200, (false, nil) on the definite |
| +// 400/404/409 answers, and (false, an error wrapping ErrKeymailUnreachable) |
| +// when Keymail could not say. |
| +func (k *Keymail) HasMailbox(ctx context.Context, addr string) (bool, error) { |
| + ctx, cancel := context.WithTimeout(ctx, hasMailboxTimeout) |
| + defer cancel() |
| + var lr struct { |
| + BoxPub string `json:"box_pub"` |
| + } |
| + _, err := k.do(ctx, http.MethodGet, "/api/federation/lookup?addr="+url.QueryEscape(addr), "", nil, &lr) |
| + if err == nil { |
| + return true, nil |
| + } |
| + if lerr := lookupFailure(err); errors.Is(lerr, ErrNoKeymailMailbox) { |
| + return false, nil |
| + } else { |
| + return false, lerr |
| + } |
| +} |
| + |
| // SendFederated delivers one sealed message to any Keymail mailbox as |
| // console@<origin host>. It registers the console's key with the mailbox |
| // first when Keymail asks for it. |
| @@ -75,7 +121,7 @@ func (k *Keymail) SendFederated(ctx context.Context, to, subject, body string) ( |
| } |
| lu := "/api/federation/lookup?addr=" + url.QueryEscape(to) + "&from=" + url.QueryEscape(from) |
| if _, err := k.do(ctx, http.MethodGet, lu, "", nil, &lr); err != nil { |
| - return "", fmt.Errorf("%w: %w", ErrNoKeymailMailbox, err) |
| + return "", lookupFailure(err) |
| } |
| if lr.PowBits > maxFederationPowBits { |
| return "", ErrFederationPriceTooHigh |
diff --git a/internal/console/server.go b/internal/console/server.go| index 05b4e71a3..2009ee332 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -2499,34 +2499,15 @@ func (s *Server) handleInvite(w http.ResponseWriter, r *http.Request) { |
| subject, body := inviteMessage(addr, acct.Name, to, link) |
| ctx, cancel := context.WithTimeout(r.Context(), s.inviteTimeout) |
| defer cancel() |
| - kctx, kcancel := context.WithTimeout(ctx, s.inviteKeymailShare) |
| - _, err = s.sendFederated(kctx, to, subject, body) |
| - kcancel() |
| - if err == nil { |
| - back("ok", inviteFlashKeymail(to)) |
| - return |
| - } |
| - switch { |
| - case IsKeymailBlocked(err): |
| - // The recipient blocked the console. That is their answer; email |
| - // to the same address would go round it. |
| - log.Printf("console: invite to %s: the recipient blocked the console on keymail; not emailing", to) |
| + switch ch, err := s.deliver(ctx, to, subject, body, s.inviteKeymailShare); { |
| + case err != nil: |
| + log.Printf("console: invite to %s not delivered: %v", to, err) |
| back("err", inviteFlashFailed(to, link)) |
| - return |
| - case errors.Is(err, ErrNoKeymailMailbox): |
| - log.Printf("console: invite to %s: no keymail mailbox, trying email", to) |
| + case ch == deliveredKeymail: |
| + back("ok", inviteFlashKeymail(to)) |
| default: |
| - log.Printf("console: invite to %s by keymail: %v", to, err) |
| - } |
| - if s.consoleMail != nil { |
| - merr := s.consoleMail.Send(ctx, to, subject, body) |
| - if merr == nil { |
| - back("ok", inviteFlashEmail(to)) |
| - return |
| - } |
| - log.Printf("console: invite to %s by email: %v", to, merr) |
| + back("ok", inviteFlashEmail(to)) |
| } |
| - back("err", inviteFlashFailed(to, link)) |
| } |
| // inviteMessage is the subject and body for an invitation, whichever channel |