Skip to content

Commit

Permalink
CLI: fix error handling for invalid access tokens (#5427)
Browse files Browse the repository at this point in the history
Fixes Cody-3261.
Fixes CODY-2967.
Fixes CODY-2909.

Previously, Cody CLI users got a poor experience if they had provided
expired credentials with `SRC_ACCESS_TOKEN`. The `cody chat` command
reported that the user was "not logged in" and `cody auth login`
confusingly reported that the user was already logged in.

Now, Cody CLI reports a helpful error message saying the user has
invalid credentials and documents how to fix the problem by creating a
new access token.

This fix ended up being harder to implement than I original expected
because the code related to authentication was badly organized.
Specifically, we didn't distinguish between error cases (invalid token)
and unauthenticate cases (the user hasn't logged in yet). I used this
issue as an opportunity to clean up the code so that all the validation
is done in a single function that handles all cases.


## Test plan

Manually tested the following flows

- `cody chat` works with environment variable login and secret storage
login
- `export SRC_ACCESS_TOKEN=foobar`, and confirm that `cody chat` and
`cody auth login` give helpful error messages. Also confirm this happens
when the secret storage has an invalid token (manually edited keychain
item)
```
❯ export SRC_ACCESS_TOKEN=foobar
❯ pnpm agent auth login
...
✖ The provided access token is invalid. The most common cause for this is that the access token has expired. If you are using SRC_ACCESS_TOKEN, create a new token at https://sourcegraph.sourcegraph.com/user/settings/tokens/new?description=CodyCLI and update the value of SRC_ACCESS_TOKEN. If you are using `cody auth login --web`, run `cody auth logout` and try logging in again.
```
- Confirm that `cody auth logout` reports a helpful error message when
`SRC_ACCESS_TOKEN` is set
```
❯ pnpm agent auth logout
...
✖ You cannot logout when using SRC_ACCESS_TOKEN with logout. To fix this problem, run `unset SRC_ACCESS_TOKEN` and try again.
```
<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->

## Changelog

* Cody CLI now reports a helpful error message when authenticating with
an invalid access token

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
  • Loading branch information
olafurpg authored Sep 2, 2024
1 parent 9b42cbd commit e1fe6a5
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 227 deletions.
5 changes: 5 additions & 0 deletions agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ This is a log of all notable changes to the Cody command-line tool. [Unreleased]

### Changed

## 5.5.13
### Fixed

- `cody chat` and `cody auth login` now report a helpful error message when trying to authenticate with an invalid access token.

## 5.5.12

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sourcegraph/cody",
"version": "5.5.12",
"version": "5.5.13",
"description": "Cody CLI is the same technology that powers Cody in the IDE but available from the command-line.",
"license": "Apache-2.0",
"repository": {
Expand All @@ -17,7 +17,7 @@
"build:webviews": "pnpm -C ../vscode run -s _build:webviews --mode production --outDir ../../agent/dist/webviews",
"build": "pnpm run -s build:root && pnpm run -s build:webviews && pnpm run -s build:agent",
"build-minify": "pnpm run build --minify",
"agent": "pnpm run build && node --enable-source-maps dist/index.js",
"agent": "pnpm run build:for-tests && node --enable-source-maps dist/index.js",
"agent:skip-root-build": "pnpm run build:agent && node --enable-source-maps dist/index.js",
"agent:debug": "pnpm run build && CODY_AGENT_TRACE_PATH=/tmp/agent.json CODY_AGENT_DEBUG_REMOTE=true node --enable-source-maps ./dist/index.js api jsonrpc-stdio",
"build-ts": "tsc --build",
Expand Down
Loading

0 comments on commit e1fe6a5

Please sign in to comment.