Skip to content

GitHub connector: support refresh tokens (cherry-pick dexidp/dex#4845) - #76

Merged
paurosello merged 2 commits into
mainfrom
fix-github-connector-refresh-tokens
Jul 14, 2026
Merged

paurosello merged 2 commits into
mainfrom
fix-github-connector-refresh-tokens

Conversation

@paurosello

Copy link
Copy Markdown

What

Cherry-picks upstream dexidp/dex#4845 (e8aaa98b, "GitHub Connector: Implement support for refresh tokens") onto this fork.

The GitHub connector now persists GitHub's refresh token + expiry in connectorData and renews the upstream user access token during Refresh(), instead of rebuilding the oauth2 client with only the originally-stored access token (which could never renew).

Why

Fixes giantswarm/giantswarm#37164 — muster (and any long-lived giantswarm-github-backed session) is force-logged-out after ~8h, well under the intended 30-day (720h) refresh-token lifetime.

Root cause: the giantswarm-github connector is backed by a GitHub App, whose user-to-server tokens expire after exactly 8 hours. On dex refresh the old connector could not renew the upstream token, so at login+8h GitHub returned 401 Bad credentials, dex's refresh failed, and the session died. Azure (giantswarm-ad) was unaffected because its connector already refreshes upstream tokens.

Why cherry-pick and not a full upgrade

  • The fix is merged to upstream master (2026-07-02) but in no release yet — latest release v2.45.1 predates it, so upgrading to any tagged version would not fix this.
  • A full rebase onto master is ~649 commits / ~53k insertions across 321 files, and this fork carries custom patches (group-prefixing in server/handlers.go/refreshhandlers.go/server.go, OIDC rootCA fix) in files upstream churned heavily — high risk, no extra benefit for this issue.
  • The cherry-pick is clean: production connector/github/github.go applies with zero conflicts (the fork's github connector was identical to upstream v2.43.1), builds, and the connector test suite passes.

Note on the test adaptation

The upstream fix's test added one HandleCallback call using the 3-arg signature (scopes, connData, req) introduced by an unrelated upstream PKCE PR (dexidp#3777) that is not in this fork. That single call site is adapted back to this fork's 2-arg signature. Production code is unchanged from upstream.

Rollout note

Existing sessions only benefit after each user logs in once post-rollout — older connectorData has no upstream refresh token stored. After that, GitHub-backed sessions are bounded by dex policy (720h), since GitHub App refresh tokens last ~6 months.

Test plan

  • go build ./...
  • go test ./connector/github/ — passes
  • Release as v2.43.1-gs5, bump dex-app, roll out to MCs (follow-up)

Refs giantswarm/giantswarm#37164

Cherry-pick of upstream dexidp#4845 (e8aaa98), which is merged to
upstream master but not yet in any release (latest release v2.45.1 is
from before the merge).

Adapts the new test's single HandleCallback call site from the 3-arg
signature (introduced upstream by PKCE PR dexidp#3777, not present in this
fork) back to the 2-arg signature on this branch. Production code
applies unchanged.

Fixes forced re-login after ~8h for GitHub App-backed connectors.

Signed-off-by: Matheus Pimenta <[email protected]>
(cherry picked from commit e8aaa98)

Closes: giantswarm/giantswarm#37164
@paurosello

Copy link
Copy Markdown
Author

Code review (automated, high-effort recall)

Clean cherry-pick of upstream dexidp#4845. No high-severity logic bug in the happy path — the refresh mechanism is correct and well-tested. Findings are low-severity; merging.

1. (medium/robustness) Rotated refresh token can be lost on a transient getGroups error — github.go:355

Because GitHub App refresh tokens rotate, c.user() persists the new token into identity.ConnectorData, but the later c.getGroups() step (the GiantSwarm config does use groups) can still return an error, making Refresh return (identity, err). dex discards the identity on error, so the just-rotated token is lost while GitHub has already invalidated the old one → subsequent refreshes fail permanently (the exact force-logout this PR fixes). Same ordering exists upstream; worth a follow-up (persist rotated tokens even on downstream error, or reorder).

2. (low) Notify callback error drops a rotated token — github.go:317

If f (json.Marshal) fails, Token() returns the error after GitHub rotated the refresh token, so the new token is never persisted → permanently unrefreshable session. Very low probability (marshalling strings+time), but a one-way failure mode.

3. (low) omitempty on Expiry time.Time is ineffective — github.go:136

encoding/json never treats a zero struct as empty, so the field always serializes (e.g. "expiry":"0001-01-01T00:00:00Z"). Functionally harmless; the tag is misleading. Consider *time.Time or dropping the tag.

4. (low) Redundant guard — github.go:330

AccessToken=="" && RefreshToken=="" duplicates the len(ConnectorData)==0 error above; only reachable for {}-ish payloads.

5. (low, pre-existing) Misleading error text — github.go:327

"github: unmarshal access token" unmarshals the whole connectorData struct; should read "unmarshal connector data".

puja108 commented Jul 14, 2026

Copy link
Copy Markdown
Member

Follow-up: test coverage gap on the group-backed refresh path

Both new refresh tests (TestRefreshUsesRefreshToken, TestRefreshWithAccessTokenOnlyConnectorData) use a connector with no org/orgs and groupScope=false, so groupsRequired() returns false and getGroups() is never exercised during Refresh. The actual giantswarm-github connector is group-backed, so that path — where the refreshed token round-trips through groupsForOrgs/teamsForOrg — is untested. It's also exactly where the rotated-token-loss-on-downstream-error case (finding #1 above) manifests.

A test that configures orgs plus a /user/teams + /orgs/.../members/... mock during Refresh would both lock in the fix on the path production actually uses and make finding #1 reproducible.

Not blocking — the cherry-pick is clean and this doesn't touch production code, so it's fine to merge the hotfix and add this test as a follow-up.


Generated by Claude Code

@puja108 puja108 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, clean cherry pick once the issues your own review mentioned and CI is fixed

@paurosello
paurosello merged commit 252b93c into main Jul 14, 2026
8 checks passed
@paurosello
paurosello deleted the fix-github-connector-refresh-tokens branch July 14, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants