Fix(search): auth issues with Sourcegraph VSCode extension#63175
Fix(search): auth issues with Sourcegraph VSCode extension#63175
Conversation
`requestGraphQLFromVSCode`` accepts the endpoint URL and access token as parameters, loading each from storage if they are not provided. The order in which those two parameters are resolved is important because the key to the access token is the endpoint URL. The resolution order was backward: it first loaded the storage for the access token (`session`), using the stored endpoint URL (ignoring the passed-in value) and then resolved the endpoint URL. This resulted in a mismatch between the endpoint URL and the access token, requiring the user to manually edit the settings.json file to specify those two parameters. There are still problems with the auth process, but now it will actually use the values entered in the UI.
Instead of directly accessing `vscode.workspace.getConfiguration('sourcegraph').get<string>('url')`.
This will allow for replacing where the endpoint URL is stored, and also, it may be that using `getConfiguration('sourcegraph')` to get the section of the config file was not working correctly. I was seeing some strange behavior with `readConfiguration()`, which does this internally.
It is a literal export of the context, and I'm thinking there has to be a better way to do it. It's used only in `endpointSetting.js` currently; there may be a way to register it there, but for now, this works. The context is used to access the globalState storage, in which the endpoint URL is (will be, in a subsequent commit) stored. Exporting the context, rather than `extensionCoreAPI` or `localStorageService` because it's the simplest right now.
Instead of `settings.json`. Add a migration from `settings.json` if it's being used. The order of resolving the endpoint URL is: 1. extension context 2. settings.json 3. default value
When the endpoint URL was being stored in `settings.json`, a user could modify it directly instead of going through the UI, so the extension would be reloaded every time the endpoint URL was changed in `settings.json`, re-running `activate`, in which an authentication provider is registered for the current endpoint URL. Now that the endpoint URL is managed only via the UI and is stored out of reach of the user in the extension context, register an authentication prodiver whenever the endpoint URL changes, rather than reloading the whole extension and triggering `activate`.
Avoids error messages about wrong/missing access tokens. There's probably room for improvement here (like maintaining a MRU of URLs), but this is a quick fix.
No longer need the code to keep an eye on `settings.json` to reload the extension if the endpoint URL changed. Don't need `readConfiguration` to access `settings.json` anymore.
|
Tagging @sourcegraph/security-code-review (@shivasurya) for a review, because this involves managing an access token. The original code had a listener that reloaded the extension if the endpoint URL in Also, is it possible to meddle with the extension's |
Changed to use `vscode.workspace.getConfiguration()` as it does elsewhere in the code.
There are stull instances of `vscode.workspace.getConfiguration('sourcegraph')` also; I'm nost sure which is better, but I did have some possible issues with `readConfiguration()` when I first started researching the auth issues so I prefer `vscode.workspace.getConfiguration()`.
That should be private to the extension only. |
No more invalidating because of changes to the endpoint URL
Also no longer needed because the endpoint URL is no longer stored in `settings.json`.
Remove language about restarting VSCode to accomodate new Sourcegraph URL.
|
I tried this out locally, but was unable to get it to work. I attempted to log in with S2 with an access token I just created, but got the following error: After the error, the Sourcegraph panel has no option to try to log in again and when I try to run a search, I get 401 Unauthorized errors: Anything else I should try to collect? |
|
@camdencheek Make sure you're switching to the feature branch Here's a walkthrough of how I test login. Screen.Recording.2024-06-11.at.14.12.53.mp4 |
|
Aah, I think I missed the |
|
Fixes the issues requiring the workaround described in this video.
Closes #60710
No more manual editing of
settings.json.The endpoint URL and access code can now all be managed from the UI
Test plan
First
Build and run locally.
Then
Launch extension in VSCode: open the
Run and Debugsidebar view in VS Code, then selectLaunch VS Code Extensionfrom the dropdown menu.Click on
Have an account?to open the login dialog.Enter an access token and the URL of the Sourcegraph instance to which you would like to connect.
Click
Authenticate account.In the Help and Feedback section, click your username to open the logout panel, then log out.
Repeat the login process.
You can check
settings.jsonif you'd like to confirm that it's no longer being used.If you're logging in to dotcom, you'll probably se a SQL error. The login process still works; the SQL error does not have long to live.
Changelog
settings.json