From bb923958720a33baae8a8dc522a17dd03617d0a9 Mon Sep 17 00:00:00 2001 From: Matthew Meszaros Date: Thu, 19 Nov 2026 20:16:24 +0100 Subject: [PATCH] feat: register the instance's Google identity with GraphKit on its own when GraphKit refuses the Microsoft one, so Google mailboxes connect by delegation regardless --- .../app/store/delegation_fallback_test.go | 36 +++++++++++++++++++ internal/app/store/sync.go | 19 +++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 internal/app/store/delegation_fallback_test.go diff --git a/internal/app/store/delegation_fallback_test.go b/internal/app/store/delegation_fallback_test.go new file mode 100644 index 000000000..5d24c3b2d --- /dev/null +++ b/internal/app/store/delegation_fallback_test.go @@ -0,0 +1,36 @@ +package store + +import ( + "context" + "net/http" + "testing" + + "github.com/warmbly/warmbly/internal/pkg/graphkit" +) + +// refusesMicrosoft records each delegation push and refuses any that carries a Microsoft identity. +type refusesMicrosoft struct { + graphkit.API + pushed []graphkit.Delegation +} + +func (r *refusesMicrosoft) PutDelegation(_ context.Context, d graphkit.Delegation, _ string) error { + r.pushed = append(r.pushed, d) + if d.Microsoft != nil { + return &graphkit.Error{Status: http.StatusBadRequest, Code: graphkit.CodeInvalidRequest, Message: `"User.Read.All" is not supported`} + } + return nil +} + +func TestRefusedMicrosoftIdentityStillRegistersGoogle(t *testing.T) { + h := newHarness(t) + h.delegator.googleID = "1234567890" + api := &refusesMicrosoft{API: h.fake} + h.s.setAPI(api) + if err := h.s.PushDelegation(context.Background()); err != nil { + t.Fatalf("Google alone should register: %v", err) + } + if len(api.pushed) != 2 || api.pushed[1].Google == nil || api.pushed[1].Microsoft != nil { + t.Fatalf("pushes: %+v", api.pushed) + } +} diff --git a/internal/app/store/sync.go b/internal/app/store/sync.go index 1948e4a8f..20425c406 100644 --- a/internal/app/store/sync.go +++ b/internal/app/store/sync.go @@ -362,12 +362,21 @@ func (s *Service) PushDelegation(ctx context.Context) error { if d.Empty() { return nil } - raw, _ := json.Marshal(d) - if err := s.api().PutDelegation(ctx, d, "store:delegation:"+digest(raw)+":"+s.now().Format("2006-01-02")); err != nil { - logUpstream("delegation", err) - return err + err := s.putDelegation(ctx, d) + if ge, ok := graphkit.AsError(err); ok && ge.Code == graphkit.CodeInvalidRequest && d.Google != nil && d.Microsoft != nil { + // A refused Microsoft identity must not keep Google mailboxes from connecting. + logUpstream("delegation.microsoft", err) + err = s.putDelegation(ctx, graphkit.Delegation{Google: d.Google}) } - return nil + if err != nil { + logUpstream("delegation", err) + } + return err +} + +func (s *Service) putDelegation(ctx context.Context, d graphkit.Delegation) error { + raw, _ := json.Marshal(d) + return s.api().PutDelegation(ctx, d, "store:delegation:"+digest(raw)+":"+s.now().Format("2006-01-02")) } // GoogleToken brokers a Google access token for a purchased mailbox through