Skip to content

fix: Test linked git provider tokens on the Integrations page (OHE-3741) - #1006

Merged
tofarr merged 12 commits into
mainfrom
ohe-3741-git-provider-connection-check
Oct 27, 2026
Merged

tofarr merged 12 commits into
mainfrom
ohe-3741-git-provider-connection-check

Conversation

@tofarr

@tofarr tofarr commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

  • A human has tested these changes.

This helps us accommodate the case where a provider has revoked a token but the service is not yet aware.

AGENT:


Why

With ENABLE_OAUTH_V2_LOGIN on, /settings/integrations shows a git provider as connected whenever an oauth_tokens row exists, even after the provider has revoked the token. Normal traffic can't be relied on to notice. ProviderHandler.get_user() (behind git-info) stops at the first provider that works, so a dead token for a second provider is never used. A 401 inside the sandbox never reaches the server. Testing the token from the Integrations page is the cheap, reliable way to keep that page accurate.

Summary

  • Backend: new oauth_tokens.invalidated_at column (migration 198) and new route POST /oauth/git-providers/{id}/check (server/auth/git_token_check.py). The route calls the provider's get_user() once with the stored token, refreshing first only if the token has expired.
    • A 401, or a refused refresh token (invalid_grant, or GitHub's bad_refresh_token), sets the flag. Success clears it.
    • Timeouts, 5xx and rate limits return unavailable and change nothing.
    • The flag only changes if the row still holds the token that was tested.
    • Saving a new token or refreshing successfully clears the flag.
    • GET /oauth/git-providers now returns needs_reconnect.
  • Frontend:
    • Each linked provider card runs one check on mount.
    • New "Test connection" button reruns the check and shows a toast with the result.
    • A rejected token shows "Reconnect required" and a Reconnect button (the existing link flow), and hides that provider's webhook settings.
    • Six new strings, translated into all 15 languages.
flowchart LR
  A[Card mounts / Test connection] --> B[POST /oauth/git-providers/:id/check]
  B --> C{token expired?}
  C -- yes --> D[refresh]
  D -- invalid_grant / bad_refresh_token --> F[set invalidated_at → invalid]
  D -- 5xx / timeout --> U[unavailable, no change]
  C -- no --> E[GitService.get_user]
  D -- ok --> E
  E -- 401 --> F
  E -- 200 --> V[clear invalidated_at → valid]
  E -- other error --> U
Loading

Issue Number

OHE-3741 (partial: the needs_reconnect / Integrations part)

How to Test

Automated (all passing locally):

  • uv run pytest tests/unit/server/auth/test_git_token_check.py tests/unit/server/auth/test_oauth_v2_refresh.py tests/unit/server/routes/test_oauth_v2_routes.py tests/unit/storage/test_oauth_stores.py. The token store and check tests run against real PostgreSQL; only the provider API is mocked.
  • Wider run: tests/unit/server/auth, tests/unit/storage, test_users_v1.py and the middleware tests (787 passed).
  • cd frontend && npx vitest run __tests__/routes/git-settings.test.tsx __tests__/components/features/settings/ (380 passed), plus npm run lint and npm run build.

Manual (not done yet, needs a real provider):

  1. Turn on ENABLE_OAUTH_V2_LOGIN, link GitHub on /settings/integrations, then revoke the app's authorization on GitHub.
  2. Reload /settings/integrations. The GitHub card should show "Reconnect required" with a Reconnect button.
  3. Click Reconnect and finish the link flow. After the reload the card should show "Connected".
  4. "Test connection" should show a success toast.

Video/Screenshots

Screen.Recording.2026-10-27.at.4.11.34.PM.mov

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • Migration 198 adds one nullable column. The upgrade runs as part of the test database template; the downgrade wasn't run.
  • Out of scope / follow-ups for OHE-3741:
    • On the signed-in path, a failed refresh still deletes the row and logs the user out (SaasUserAuth._v2_get_provider_tokens and the middleware). That behaviour is unchanged here.
    • A 401 from git-info or other normal use doesn't set the flag yet.
    • No banner outside the Integrations page.
  • Existing refresh paths: RefreshTokenRejectedError subclasses ValueError, so existing except blocks around refresh behave exactly as before.
  • Private import: the route imports _idp_token_host from saas_user_auth so self-hosted GitLab, Bitbucket Data Center and Forgejo resolve the same host as normal token use.
  • declaration.ts: also moves AUTH$GIT_PROVIDER_LOGIN_FAILED, because the repo's prelint/build regeneration produces that order and main's copy was out of date.

This PR was created by an AI agent (OpenHands) on behalf of the user.

@tofarr can click here to continue refining the PR


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-7bc9f18

Add oauth_tokens.invalidated_at and POST /oauth/git-providers/{id}/check,
which calls the provider's API once with the stored token: a 401 or a
rejected refresh token (invalid_grant / GitHub bad_refresh_token) flags the
row, success clears it, and timeouts/5xx change nothing. GET
/oauth/git-providers now reports needs_reconnect.

The Integrations page runs the check for each linked provider on load, adds
a Test connection button, and shows Reconnect (the existing link flow) when
the token was rejected.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains migrations. Please synchronize before merging to prevent conflicts.

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  migrations/versions
  201_add_oauth_token_invalidated_at.py 25, 32
  server/auth
  git_token_check.py 65
  oauth_v2_refresh.py 88-89
  server/routes
  oauth_v2.py 772, 815, 838-870
  storage
  oauth_token.py
  oauth_token_store.py 327-332
Project Total  

This report was generated by python-coverage-comment-action

@tofarr tofarr changed the title Test linked git provider tokens on the Integrations page (OHE-3741) fix: Test linked git provider tokens on the Integrations page (OHE-3741) Oct 26, 2026
@github-actions github-actions Bot added the type: fix A bug fix label Oct 26, 2026
tofarr and others added 7 commits October 26, 2026 16:25
Main already has migrations 198-200, so this PR's 198 created a second
head. Rebase it onto 200.

Co-authored-by: openhands <openhands@all-hands.dev>
…ion check

A row is a git provider when its provider_type is in GIT_PROVIDER_TYPES,
regardless of is_idp. The check endpoint 404'd for e.g. GitHub configured
as the login IDP, even though the list endpoint shows it.

Co-authored-by: openhands <openhands@all-hands.dev>
@tofarr
tofarr marked this pull request as ready for review October 27, 2026 16:12

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

Thank you! 🙏

openhands-agent and others added 2 commits October 27, 2026 16:26
…ng it

The test pinned the head revision to '200', so it broke as soon as
migration 201 landed on top of it.

Co-authored-by: openhands <openhands@all-hands.dev>
@tofarr
tofarr merged commit d7aa6f8 into main Oct 27, 2026
29 of 31 checks passed
@tofarr
tofarr deleted the ohe-3741-git-provider-connection-check branch October 27, 2026 18:53
hieptl added a commit that referenced this pull request Oct 27, 2026
…storage-api-and

main now holds migration 201 (oauth_token.invalidated_at, #1006), so the
Model Router migration moves from 201 to 202 chaining off 201. The
agent-profile upgrade test takes main's _head_revision() lookup in place
of the literal head pin.
@openhands-release-bot openhands-release-bot Bot added the released: 1.81.0 Shipped in 1.81.0 label Oct 28, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.81.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.81.0 Shipped in 1.81.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants