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

This commit is contained in:
Matthew Meszaros
2026-11-19 20:16:24 +01:00
parent 745154c21b
commit bb92395872
2 changed files with 50 additions and 5 deletions
@@ -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)
}
}
+14 -5
View File
@@ -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