auth: use OIDC sub claim as stable account key, not email
When a user renames their Gmail address (Google enabled this on 31 March 2026), gogcli loses the link to the existing local account. Every data structure in the auth layer keys on email. The OIDC spec calls email mutable and specifies sub as the stable identifier.
What happens today
internal/googleauth/accounts_server.go:585 parses the ID token but reads only email:
var claims struct {
Email string `json:"email"`
}
Downstream, email is the primary key throughout:
internal/config/config.go:17:AccountAliases map[string]stringkeys on emailinternal/config/config.go:18:AccountClients map[string]stringkeys on emailinternal/cmd/auth_add.go:251:normalizeEmail(authorizedEmail) != normalizeEmail(c.Email)rejects the token when the authorised email doesn't match the CLI arginternal/cmd/auth_add.go:265: keyring entry stored asstore.SetToken(client, authorizedEmail, …)secrets.Tokenhas anEmailfield, noSubfield
What breaks on rename
- The keyring entry under
[email protected]is orphaned.gog auth add [email protected]creates a second entry, not an update. account_aliasesandaccount_clientsentries for the old email stay behind unless the user remembers togog auth remove [email protected]first.- Scripts and agent invocations hardcoding the old email silently target a stale or missing account.
- The line 251 check surfaces as
authorized as X, expected Yrather than "looks like a rename, migrate?".
Minimal fix
Add sub to the claim struct, store it in secrets.Token, use sub as the canonical key in keyring and config. Email becomes a display label.
var claims struct {
Sub string `json:"sub"`
Email string `json:"email"`
}
When a refresh returns an ID token whose sub matches an existing stored account but whose email differs, treat it as a rename: migrate the keyring entry, update account_aliases and account_clients to the new email, log a one-line notice. Existing email-keyed configs need a one-shot migration on first run after upgrade (detect by presence of Email without Sub, re-fetch the ID token, backfill).
The OIDC spec: "The sub Claim ... is never reassigned to another End-User." Google's identity docs call out sub as the stable ID: https://developers.google.com/identity/openid-connect/openid-connect#authenticationuriparameters
Prior art
ztnet landed the equivalent fix two days ago: https://github.com/sinamics/ztnet/pull/884. Their sign-in callback now looks up by provider+subject first, email second. Worth a read as a reference pattern; the Go translation is cleaner because you already have client as the per-account namespace.
Detection
The pattern is one of several flagged by authdrift, a Semgrep ruleset I wrote while auditing 2M+ repos on Sourcegraph for this specific bug. Breakdown of findings: https://neelagiri65.github.io/gmail-oauth-research/. 124 affected repos across Passport.js, NextAuth, Python/Django and Ruby/OmniAuth. Go isn't covered yet because the scan targeted web frameworks, not CLIs. gogcli is the first Go entry I've seen with this exact shape.
I can open a PR if this direction works.
Fixed on main in 532601a + follow-up 8addfce.
What changed:
- OAuth tokens now persist the OIDC
subclaim alongside email. - Subject-keyed token copies are the canonical stable key; email-keyed entries stay as compatibility aliases.
auth add/ auth-manage migrate renamed Google accounts when the samesubappears with a new email.- Normal API refresh now backfills
subjectfor old stored tokens and migrates renamed emails without deleting the subject-keyed canonical copy. auth list --jsonand token export/import includesubject.
Verification:
go test ./internal/googleapi ./internal/googleauth ./internal/secrets ./internal/cmd -run 'TestPersistingTokenSource|TestTokenSourceForAccountScopes|TestEmailFromIDToken|TestKeyringStore|TestMigrateStoredSubjectIdentity|TestAuthAdd|TestExecute_AuthAdd' -count=1make lintmake testmake ci- Live:
gog --json --account [email protected] people mesucceeded, thengog --json auth listshowedsubject: 111833477811442819246backfilled for[email protected].
This is a really thorough fix .. subject keyd canonical with email as compatibility alias is exactly the right architecture. Thanks for taking this seriously and shipping it this fast.