Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.48.2 - Unreleased

- Auth: automatic reauthorization no longer replays a stored Google Ads (`adwords`) grant unless the failing command needs Ads, so an old Ads grant cannot trigger Google's passkey gate; restore it with `gog auth add <email> --services user,ads`. (#1353)
- Dependencies: refresh the MCP and Google API clients, networking libraries, and tracking-worker tooling while preserving the Go 1.26 minimum, 24-hour worker cooldown, and security overrides.

## 0.48.1 - 2026-11-15
Expand Down
50 changes: 47 additions & 3 deletions internal/googleauth/reauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"os"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -135,6 +136,18 @@ func Reauth(ctx context.Context, opts ReauthOptions) (secrets.Token, error) {
}
}

// Google can require passkey enrollment before granting the Ads scope, which
// blocks the whole consent screen. Replay it only when this request needs Ads.
droppedAds := false
if opts.StoredToken != nil && !requestsAds(opts.Scopes, opts.Services) {
reauthScopes, droppedAds = withoutAdsScopes(reauthScopes)
reauthServices = slices.DeleteFunc(slices.Clone(reauthServices), isAdsService)

if len(reauthScopes) == 0 {
reauthScopes = opts.Scopes
}
}

// Convert service strings to Service types.
services := make([]Service, 0, len(reauthServices))
for _, s := range reauthServices {
Expand All @@ -156,10 +169,14 @@ func Reauth(ctx context.Context, opts ReauthOptions) (secrets.Token, error) {

fmt.Fprintln(stderr, "Re-authorizing…")

// A narrowed Gmail, Calendar, or Photos grant must
if droppedAds {
fmt.Fprintf(stderr, "Skipping the Google Ads scope because Google may require a passkey for it; run `gog auth add %s --services user,ads --force-consent` if you need Ads access.\n", opts.Email)
}

// A narrowed Gmail, Calendar, or Photos grant, or a dropped Ads grant, must
// not ask Google to include previously granted scopes, or wider access returns.
narrowed := opts.StoredToken != nil &&
(hasLimitedGmailGrant(opts.StoredToken.Scopes) || hasLimitedCalendarGrant(opts.StoredToken.Scopes) || hasLimitedPhotosGrant(opts.StoredToken.Scopes))
narrowed := droppedAds || (opts.StoredToken != nil &&
(hasLimitedGmailGrant(opts.StoredToken.Scopes) || hasLimitedCalendarGrant(opts.StoredToken.Scopes) || hasLimitedPhotosGrant(opts.StoredToken.Scopes)))

authorizeOpts := AuthorizeOptions{
Services: services,
Expand Down Expand Up @@ -267,6 +284,33 @@ func hasLimitedGmailGrant(scopes []string) bool {
return limited
}

func isAdsService(service string) bool {
return strings.EqualFold(strings.TrimSpace(service), string(ServiceAds))
}

func requestsAds(scopes []string, services []string) bool {
if slices.ContainsFunc(services, isAdsService) {
return true
}

for _, scope := range serviceInfoByService[ServiceAds].scopes {
if slices.Contains(scopes, scope) {
return true
}
}

return false
}

func withoutAdsScopes(scopes []string) ([]string, bool) {
adsScopes := serviceInfoByService[ServiceAds].scopes
kept := slices.DeleteFunc(slices.Clone(scopes), func(scope string) bool {
return slices.Contains(adsScopes, scope)
})

return kept, len(kept) != len(scopes)
}

// servicesFromScopes attempts to derive the service list from a set of
// OAuth scopes. This is a best-effort fallback when the service label
// is not available or cannot be parsed.
Expand Down
92 changes: 92 additions & 0 deletions internal/googleauth/reauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,98 @@ func TestReauthPreservesStoredScopes(t *testing.T) {
}
}

func TestReauthReplaysStoredAdsScopeOnlyForAdsRequests(t *testing.T) {
const (
gmailScope = "https://www.googleapis.com/auth/gmail.modify"
adsScope = "https://www.googleapis.com/auth/adwords"
)

for _, tc := range []struct {
name string
services []string
scopes []string
wantScopes []string
wantServices []string
wantNarrowed bool
wantNotice bool
}{
{
name: "gmail request drops stored ads",
services: []string{"gmail"},
scopes: []string{gmailScope},
wantScopes: []string{gmailScope},
wantServices: []string{"gmail"},
wantNarrowed: true,
wantNotice: true,
},
{
name: "ads request keeps stored ads",
services: []string{"ads"},
scopes: []string{adsScope},
wantScopes: []string{gmailScope, adsScope},
wantServices: []string{"gmail", "ads"},
},
} {
t.Run(tc.name, func(t *testing.T) {
var requestedScopes, requestedServices []string
var narrowed bool

stderr := &bytesBuffer{}
opts := ReauthOptions{
Email: "user@example.com",
Client: "default",
Services: tc.services,
Scopes: tc.scopes,
StoredToken: &secrets.Token{
Scopes: []string{gmailScope, adsScope},
Services: []string{"gmail", "ads"},
},
EnsureKeychainAccess: func(context.Context) error { return nil },
Confirm: func(context.Context, string) (bool, error) { return true, nil },
AuthorizeFunc: func(ctx context.Context, authOpts AuthorizeOptions) (string, error) {
requestedScopes = authOpts.Scopes
narrowed = authOpts.DisableIncludeGrantedScopes

for _, svc := range authOpts.Services {
requestedServices = append(requestedServices, string(svc))
}

return "new-refresh-token", nil
},
FetchIdentityFunc: func(context.Context, string, string, []string, time.Duration) (Identity, error) {
return Identity{Subject: "sub123", Email: "user@example.com"}, nil
},
Stderr: stderr,
}

tok, err := Reauth(context.Background(), opts)
if err != nil {
t.Fatalf("Reauth: %v", err)
}

if strings.Join(requestedScopes, " ") != strings.Join(tc.wantScopes, " ") {
t.Fatalf("requested scopes = %v, want %v", requestedScopes, tc.wantScopes)
}

if strings.Join(requestedServices, " ") != strings.Join(tc.wantServices, " ") {
t.Fatalf("requested services = %v, want %v", requestedServices, tc.wantServices)
}

if strings.Join(tok.Scopes, " ") != strings.Join(tc.wantScopes, " ") {
t.Fatalf("stored scopes = %v, want %v", tok.Scopes, tc.wantScopes)
}

if narrowed != tc.wantNarrowed {
t.Fatalf("DisableIncludeGrantedScopes = %v, want %v", narrowed, tc.wantNarrowed)
}

if gotNotice := strings.Contains(string(stderr.data), "--services user,ads"); gotNotice != tc.wantNotice {
t.Fatalf("ads notice shown = %v, want %v; stderr: %s", gotNotice, tc.wantNotice, stderr.data)
}
})
}
}

func TestReauthPreservesLimitedGmailGrants(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down
Loading