Skip to content

Commit

Permalink
CLI: replace keytar with custom solution (#4750)
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg authored Jul 3, 2024
1 parent 7cc1ea5 commit d6e931a
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 47 deletions.
13 changes: 12 additions & 1 deletion agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ This is a log of all notable changes to the Cody command-line tool. [Unreleased]

### Changed

## 0.1.1
## 0.1.2

### Changed

- The "service account name" for storing secrets is now formatted as "Cody:
$SERVER_ENDPOINT ($USERNAME)" instead of "Cody" making it easier to
understand what account/endpoint is stored there.

### Fixed

- Running `cody-agent help` should work now. It was previously crashing about a missing keytar dependencies.

## 0.1.1
### Fixed

- Running `npm install -g @sourcegraph/cody-agent` should work now. It was previously crashing about a missing keytar dependency.

## 0.1.0
Expand Down
11 changes: 2 additions & 9 deletions agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sourcegraph/cody-agent",
"version": "0.1.1",
"version": "0.1.2",
"description": "Cody JSON-RPC agent for consistent cross-editor support",
"license": "Apache-2.0",
"repository": {
Expand All @@ -24,13 +24,7 @@
"prepublishOnly": "pnpm run build"
},
"bin": "dist/index.js",
"files": [
"dist/index.js",
"dist/index.js.map",
"dist/*.wasm",
"dist/win-ca-roots.exe",
"dist/keytar-*.node"
],
"files": ["dist/index.js", "dist/index.js.map", "dist/*.wasm", "dist/win-ca-roots.exe"],
"peerDependencies": {
"@inquirer/prompts": "^5.0.7",
"@pollyjs/core": "^6.0.6",
Expand All @@ -48,7 +42,6 @@
"fast-myers-diff": "^3.2.0",
"glob": "^7.2.3",
"js-levenshtein": "^1.1.6",
"keytar": "^7.9.0",
"lodash": "^4.17.21",
"mac-ca": "^2.0.3",
"minimatch": "^9.0.3",
Expand Down
9 changes: 5 additions & 4 deletions agent/src/cli/auth/AuthenticatedAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ export class AuthenticatedAccount {
return this.account.serverEndpoint
}

public static async fromUserSettings(spinner?: Ora): Promise<AuthenticatedAccount | undefined> {
public static async fromUserSettings(spinner: Ora): Promise<AuthenticatedAccount | undefined> {
const settings = loadUserSettings()
if (!settings.activeAccountID) {
return undefined
}
const account = settings.accounts?.find(({ id }) => id === settings.activeAccountID)
if (!account) {
spinner?.fail(`Failed to find active account ${settings.activeAccountID}`)
spinner.fail(`Failed to find active account ${settings.activeAccountID}`)
return undefined
}
return AuthenticatedAccount.fromUnauthenticated(account)
return AuthenticatedAccount.fromUnauthenticated(spinner, account)
}

public static async fromUnauthenticated(
spinner: Ora,
account: Account
): Promise<AuthenticatedAccount | undefined> {
const accessToken = await readCodySecret(account)
const accessToken = await readCodySecret(spinner, account)
if (!accessToken) {
return undefined
}
Expand Down
2 changes: 1 addition & 1 deletion agent/src/cli/auth/command-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const accountsCommand = new Command('accounts')
}
const t = new Table()
for (const account of settings.accounts ?? []) {
const authenticated = await AuthenticatedAccount.fromUnauthenticated(account)
const authenticated = await AuthenticatedAccount.fromUnauthenticated(spinner, account)
t.cell(chalk.bold('Name'), account.id)
t.cell(chalk.bold('Instance'), account.serverEndpoint)
const isActiveAccount = account.id === settings.activeAccountID
Expand Down
9 changes: 6 additions & 3 deletions agent/src/cli/auth/command-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const loginCommand = new Command('login')
.action(async (options: LoginOptions) => {
const spinner = ora('Logging in...').start()
const account = await AuthenticatedAccount.fromUserSettings(spinner)
if (!spinner.isSpinning) {
process.exit(1)
}
const userInfo = await account?.getCurrentUserInfo()
if (!isError(userInfo) && userInfo?.username) {
spinner.succeed('You are already logged in as ' + userInfo.username)
Expand Down Expand Up @@ -119,16 +122,16 @@ export async function loginAction(
})
const userInfo = await client.getCurrentUserInfo()
if (isError(userInfo)) {
spinner.fail('Failed to get username from GraphQL')
spinner.fail('Failed to get username from GraphQL. Error: ' + String(userInfo))
return undefined
}
const oldSettings = loadUserSettings()
const id = uniqueID(userInfo.username, oldSettings)
const account: Account = { id, serverEndpoint }
const account: Account = { id, username: userInfo.username, serverEndpoint }
const oldAccounts = oldSettings?.accounts
? oldSettings.accounts.filter(({ id }) => id !== account.id)
: []
await writeCodySecret(account, token)
await writeCodySecret(spinner, account, token)
const newAccounts = [account, ...oldAccounts]
const newSettings: UserSettings = { accounts: newAccounts, activeAccountID: account.id }
writeUserSettings(newSettings)
Expand Down
4 changes: 2 additions & 2 deletions agent/src/cli/auth/command-logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const logoutCommand = new Command('logout')
spinner.fail('You are already logged out')
process.exit(1)
}
await removeCodySecret(settings.activeAccountID)
const account = settings.accounts.find(account => account.id === settings.activeAccountID)
if (!account) {
spinner.fail(
Expand All @@ -23,11 +22,12 @@ export const logoutCommand = new Command('logout')
)
process.exit(1)
}
await removeCodySecret(spinner, account)
const newAccounts = settings.accounts.filter(
account => account.id !== settings.activeAccountID
)
writeUserSettings({ accounts: newAccounts })
spinner.succeed(`Logged out of account ${account.id} on ${account.serverEndpoint}`)
spinner.succeed(`Logged out of account ${account.username} on ${account.serverEndpoint}`)
process.exit(0)
} catch (error) {
if (error instanceof Error) {
Expand Down
3 changes: 3 additions & 0 deletions agent/src/cli/auth/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Ora } from 'ora'

export function notLoggedIn(spinner: Ora): void {
if (!spinner.isSpinning) {
return
}
spinner.fail('Not logged in. To fix this problem, run:\n\tcody auth login --web')
}
Loading

0 comments on commit d6e931a

Please sign in to comment.