Re-authorize Bitbucket Cloud import when the session token expires

What does this MR do and why?

Bitbucket Cloud access tokens last about two hours, and the import page only needs them while the user is on that page. Refreshing (and rotating) the refresh token in the session raced when more than one tab refreshed at once, and the rotated credentials were never written back, so later requests retried with a dead refresh token.

This MR does not refresh tokens in the controller, and it does not write refreshed tokens back to the session (that is the race this change avoids). The Bitbucket gem may still refresh in memory during a request. When bitbucket_expires_at has passed, or the token is missing, the controller clears the session and sends the user through authorization again:

  • HTML requests redirect to the Bitbucket authorize URL, as a missing token already did in bitbucket_auth.
  • JSON status and create requests return { error: { redirect } } with 404. The import UI follows that redirect for status, jobs, and create (fetchImportFactory is shared), so the HTML authorize flow can start. Because that action is shared, a Bitbucket Server ConnectionError on create now also redirects to the credentials page with the flash, instead of a generic alert.

realtime_changes only checks that a session token is present. Import progress polling does not call Bitbucket, so an expired-but-present token must not kick the user into the authorize flow mid-import.

create passes ProjectCreator the token, expiry, and refresh token from client.connection, so if the gem refreshed in memory mid-request the project stores the rotated credentials rather than a now-dead refresh token from the session.

The background importer still refreshes through Import::BitbucketImport::TokenRefreshStrategy on project import_data.

References

Closes #640895 (closed)

Screenshots or screen recordings

The import UI already followed { error: { redirect } } for status and jobs. This MR uses the same redirect for an expired session on create, and for a Bitbucket Server connection error, instead of a generic alert.

How to set up and validate locally

  1. Configure the Bitbucket Cloud OmniAuth provider on your GDK.
  2. Sign in, visit the Bitbucket Cloud import page, and authorize an account.
  3. Simulate an expired access token in the session (bitbucket_expires_at in the past), or wait for the token to expire, then reload the repository list (/-/import/bitbucket/status).
  4. Confirm the session credentials are cleared and you are sent through Bitbucket authorization again, after which the repository list loads.
  5. Confirm a JSON status or create request made with an expired session returns { error: { redirect } } and does not redirect the request itself to Bitbucket.
  6. While an import is in progress, simulate an expired-but-present session token and confirm realtime_changes still returns import status (it should not clear the session or redirect).

You can also run:

bin/rspec spec/controllers/import/bitbucket_controller_spec.rb

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Priyanshu Jha

Merge request reports

Loading
Loading