Skip to content

auth: handle rejected API tokens in AccountsStore [2/10] - #22937

Merged
sergiou87 merged 1 commit into
developmentfrom
short-lived-tokens-2-invalidation
Oct 2, 2026
Merged

sergiou87 merged 1 commit into
developmentfrom
short-lived-tokens-2-invalidation

Conversation

@sergiou87

@sergiou87 sergiou87 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Description

Moves the handling of a GitHub 401 ("token rejected") from AppStore to AccountsStore, keeping development's behavior. Later PRs make "is this still the account's token?" depend on token renewal that only AccountsStore can see, so the decision has to live there.

  • AccountsStore gets handleTokenInvalidated, the callback index.tsx registers with API.onTokenInvalidated, and emits token-invalidated with the signed-out account. Failures are reported through the store's error event.
  • The logic is unchanged: a mismatched token is ignored and logged; otherwise the account is removed and the rejected token is revoked.
  • AppStore only listens for the event and shows the InvalidatedToken dialog.
  • API.onTokenInvalidated returns an unsubscribe function.
  • The dialog takes only the dispatcher methods it uses, and says the token "is no longer valid" instead of "has been invalidated", since expiry is now a possible cause.

Stack

Merge in order, starting with 1. Each PR builds, passes tests, and keeps production behavior on its own.

  1. auth: add OAuth refresh-token protocol primitives [1/10] #22917 - OAuth protocol primitives
  2. auth: handle rejected API tokens in AccountsStore [2/10] #22937 - Token invalidation in AccountsStore (this PR)
  3. auth: add versioned secure-storage format for credentials [3/10] #22938 - Versioned credential storage
  4. auth: coordinate OAuth token renewal in AccountsStore [4/10] #22939 - Coordinated token renewal
  5. auth: hand Git fresh credentials [5/10] #22940 - Git credentials
  6. auth: resolve private image tokens through the renderer [6/10] #22941 - Private images
  7. auth: let Copilot sessions renew GitHub tokens [7/10] #22942 - Copilot SDK sessions
  8. auth: request refreshable OAuth tokens at sign-in [8/10] #22943 - Sign-in with refreshable tokens (behind flag)
  9. docs: describe refresh-token rollout and recovery [9/10] #22919 - Rollout and recovery documentation
  10. auth: log automatic sign-outs [10/10] #22959 - Log automatic sign-outs

Validation

  • tsc and lint pass.
  • Full suite: 1,928 pass, one skipped, four existing Git failures (3 git/push, 1 bare rev-parse).

Release notes

Notes: no-notes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The stale-token mismatch safety path needs regression coverage before this authentication refactor is approved.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Moves rejected-token handling into AccountsStore, preparing authentication lifecycle ownership for token renewal.

Changes:

  • Centralizes account removal, token revocation, and invalidation events.
  • Updates API subscription and recovery-dialog behavior.
  • Adds integration, error-path, and UI tests.
File Description
app/​src/​lib/​api.ts Adds token-listener unsubscription.
app/​src/​lib/​stores/​accounts-store.ts Handles rejected credentials and emits invalidation events.
app/​src/​lib/​stores/​app-store.ts Shows the invalid-token dialog from store events.
app/​src/​ui/​index.tsx Connects API invalidations to AccountsStore.
app/​src/​ui/​invalidated-token/​invalidated-token.tsx Narrows dispatcher dependencies and updates wording.
app/​test/​unit/​account-token-lifecycle-test.ts Tests callback error reporting.
app/​test/​unit/​oauth-authentication-test.ts Tests API-to-store invalidation routing.
app/​test/​unit/​ui/​invalidated-token-test.tsx Tests recovery-dialog actions.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread app/src/lib/stores/accounts-store.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The ownership transfer is coherent, preserves existing safeguards, and includes focused coverage across the API, store, and UI layers.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@sergiou87
sergiou87 marked this pull request as ready for review September 28, 2026 12:21
@sergiou87
sergiou87 requested a review from a team as a code owner September 28, 2026 12:21
@sergiou87
sergiou87 requested a review from tidy-dev September 28, 2026 12:21

@niik niik 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.

This ownership move looks coherent and preserves the existing stale-token safeguard while putting the decision in the store that will own credential renewal. The API routing, proxy/OTP exclusions, and recovery-dialog behavior are well covered.

The keychain-removal and concurrent-invalidation weaknesses appear inherited and are expected to be addressed by the coordinated credential lifecycle in #22939, so I do not consider them blocking for this refactor layer.

One non-blocking follow-up: this path now calls AccountsStore.removeAccount directly and no longer produces the successful account-removal log previously emitted by AppStore._removeAccount. Please restore an informational log when rejected credentials cause an automatic sign-out so unexpected account removal remains diagnosable from user logs.

Base automatically changed from short-lived-tokens-1-oauth to development October 2, 2026 11:28
AppStore used to subscribe to API.onTokenInvalidated directly, decide
whether the rejected token still belonged to the account, sign the
account out, and show the "token invalidated" dialog. Credential state
lives in AccountsStore, and upcoming work makes the answer to "is this
still the account's token?" depend on credential renewal that only
AccountsStore can see. Move the decision there first, as a refactor
with development's behavior.

AccountsStore now:

- exposes `handleTokenInvalidated(endpoint, token)`, the callback
  index.tsx registers with `API.onTokenInvalidated`. It runs
  `invalidateToken` and reports failures through the store's error
  event instead of leaving an unhandled rejection;
- implements `invalidateToken` with the logic AppStore had: ignore
  endpoints without an account, ignore (and log) a rejected token
  that doesn't match the account's current token, otherwise remove
  the account and revoke the rejected token remotely;
- emits `token-invalidated` with the signed-out account (its token
  cleared) through a new `onTokenInvalidated` subscription.

AppStore only listens for that event and shows the InvalidatedToken
popup, and no longer touches the API's static listener.

`API.onTokenInvalidated` now returns a function that removes the
listener, so tests (and later callers) can unsubscribe.

The InvalidatedToken dialog takes only the two dispatcher methods it
calls, which lets tests render it with a small fake. Its copy now says
the token "is no longer valid" rather than "has been invalidated": a
token can stop working because it expired, not only because it was
revoked.

Tests cover routing a GitHub 401 through API into AccountsStore,
ignoring proxy and OTP-required 401s, reporting invalidation failures
through the store, and the dialog's sign-in and dismiss actions.

Co-authored-by: Copilot <[email protected]>
@sergiou87
sergiou87 force-pushed the short-lived-tokens-2-invalidation branch from da72e93 to 3940a8a Compare October 2, 2026 11:28
@sergiou87
sergiou87 merged commit 9cf1c41 into development Oct 2, 2026
9 checks passed
@sergiou87
sergiou87 deleted the short-lived-tokens-2-invalidation branch October 2, 2026 11:42
@sergiou87 sergiou87 changed the title auth: handle rejected API tokens in AccountsStore [2/9] auth: handle rejected API tokens in AccountsStore [2/10] Oct 2, 2026
@sergiou87

Copy link
Copy Markdown
Member Author

Restored log in #22959, thanks! 🙏

sergiou87 added a commit that referenced this pull request Oct 2, 2026
On `development`, a 401 for an account's current token went through
`AppStore._removeAccount`, which logs
`[AppStore] removing account <login> (<name>) from store` before
removing it. With credential handling moved into `AccountsStore` and
`CredentialSessions`, automatic sign-outs no longer pass through
`AppStore._removeAccount`, so nothing in the user's log said which
account was removed or that it was removed at all. That makes
"why was I signed out?" reports impossible to diagnose from logs.

All automatic sign-outs funnel through `AccountsStore.requireSignIn`:

- a 401 for the account's current access token,
- a refresh token the server rejects, or a renewal response that
  cannot be used,
- a renewed credential that cannot be saved to secure storage.

`requireSignIn` now logs, at info level and only after the account
was actually retired (so stale or duplicate requests stay silent):

  [AccountsStore] signing out account <login> (<name>) because its
  credentials can no longer be used

It carries the same identifying details `development` already logged
and never any token. The specific cause is logged by the caller
(renewal failure, save failure), so this line ties those to the
account that was signed out. User-initiated sign-out still logs
through `AppStore._removeAccount` as before.

No test is added: this only adds a log line.

Addresses non-blocking review feedback from @niik on #22937.

Co-authored-by: Copilot <[email protected]>
sergiou87 added a commit that referenced this pull request Oct 5, 2026
On `development`, a 401 for an account's current token went through
`AppStore._removeAccount`, which logs
`[AppStore] removing account <login> (<name>) from store` before
removing it. With credential handling moved into `AccountsStore` and
`CredentialSessions`, automatic sign-outs no longer pass through
`AppStore._removeAccount`, so nothing in the user's log said which
account was removed or that it was removed at all. That makes
"why was I signed out?" reports impossible to diagnose from logs.

All automatic sign-outs funnel through `AccountsStore.requireSignIn`:

- a 401 for the account's current access token,
- a refresh token the server rejects, or a renewal response that
  cannot be used,
- a renewed credential that cannot be saved to secure storage.

`requireSignIn` now logs, at info level and only after the account
was actually retired (so stale or duplicate requests stay silent):

  [AccountsStore] signing out account <login> (<name>) because its
  credentials can no longer be used

It carries the same identifying details `development` already logged
and never any token. The specific cause is logged by the caller
(renewal failure, save failure), so this line ties those to the
account that was signed out. User-initiated sign-out still logs
through `AppStore._removeAccount` as before.

No test is added: this only adds a log line.

Addresses non-blocking review feedback from @niik on #22937.

Co-authored-by: Copilot <[email protected]>
niik added a commit that referenced this pull request Oct 7, 2026
Brings in short-lived token support (#22917, #22937, #22938, #22939) and
adapts it to multiple accounts per endpoint:

- Credential sessions are keyed per account (endpoint + login) instead of
  per endpoint, so renewal, invalidation and storage writes are isolated.
- AccountsStore lookups, retirement and refresh match the exact account.
- The invalidated token dialog names the signed-out login and offers a
  targeted sign-in for that account.

Co-authored-by: Copilot App <[email protected]>
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