Skip to content

Commit 71efb94

Browse files
authored
fix: remove account selector logic for credential manager (#350)
1 parent 0dfbca9 commit 71efb94

5 files changed

Lines changed: 4 additions & 167 deletions

File tree

src/credential-manager-core/index.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {LinuxHandler} from './credential-handlers/linux-handler.js'
66
import {MacOSHandler} from './credential-handlers/macos-handler.js'
77
import {NetrcHandler} from './credential-handlers/netrc-handler.js'
88
import {WindowsHandler} from './credential-handlers/windows-handler.js'
9-
import {selectAccount} from './lib/account-selector.js'
109
import {reportCredentialStoreError} from './lib/cli-command-telemetry.js'
1110
import {CredentialStore, getNativeCredentialStore, getStorageConfig} from './lib/credential-storage-selector.js'
1211
import {AuthEntry, NetrcAuthEntry} from './lib/types.js'
@@ -72,24 +71,11 @@ export async function getAuth(account: string | undefined, host: string, service
7271
const config = getStorageConfig()
7372
const netrcHandler = new NetrcHandler()
7473

75-
if (config.credentialStore) {
74+
if (config.credentialStore && account) {
7675
try {
7776
const handler = getCredentialHandler(config.credentialStore)
78-
79-
if (account) {
80-
const token = handler.getAuth(account, service)
81-
return {account, token}
82-
}
83-
84-
const accounts = handler.listAccounts(service)
85-
const selectedAccount = await selectAccount(accounts)
86-
87-
if (selectedAccount) {
88-
const token = handler.getAuth(selectedAccount, service)
89-
return {account: selectedAccount, token}
90-
}
91-
92-
throw new Error('No auth found')
77+
const token = handler.getAuth(account, service)
78+
return {account, token}
9379
} catch (error) {
9480
const {message} = error as Error
9581
credDebug(message)
@@ -186,7 +172,6 @@ export {LinuxHandler} from './credential-handlers/linux-handler.js'
186172
export {MacOSHandler} from './credential-handlers/macos-handler.js'
187173
export {NetrcHandler} from './credential-handlers/netrc-handler.js'
188174
export {WindowsHandler} from './credential-handlers/windows-handler.js'
189-
export {selectAccount} from './lib/account-selector.js'
190175
export {CredentialStore, getNativeCredentialStore, getStorageConfig} from './lib/credential-storage-selector.js'
191176
export type {StorageConfig} from './lib/credential-storage-selector.js'
192177
export {deleteLoginState, readLoginState, writeLoginState} from './lib/login-state.js'

src/credential-manager-core/lib/account-selector.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

test/credential-manager/acceptance/index.acceptance.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,6 @@ describe('credential-manager acceptance', function () {
160160
expect(accounts).to.include(accountB)
161161
expect(accounts).to.not.include(CREDENTIAL_ALTERNATE_SERVICE.account)
162162
})
163-
164-
it('retrieves when account is not provided and only one account is found', async function () {
165-
await saveAuth(CREDENTIAL.account, CREDENTIAL.token, [], CREDENTIAL.service)
166-
167-
const auth = await getAuth(undefined, 'missing.host.example.com', CREDENTIAL.service)
168-
expect(auth).to.deep.equal({account: CREDENTIAL.account, token: CREDENTIAL.token})
169-
})
170163
})
171164

172165
describe('native credential store + netrc fallback', function () {

test/credential-manager/index.test.ts

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {expect, use} from 'chai'
22
import chaiAsPromised from 'chai-as-promised'
3-
import inquirer from 'inquirer'
43
import sinon from 'sinon'
54
import {stderr} from 'stdout-stderr'
65

@@ -244,57 +243,7 @@ describe('credential-manager', function () {
244243
stderr.stop()
245244
})
246245

247-
it('should use the selected account when an account is not provided', async function () {
248-
const listAccountsStub = sinon.stub(MacOSHandler.prototype, 'listAccounts').returns(['user@example.com'])
249-
const getAuthStub = sinon.stub(MacOSHandler.prototype, 'getAuth').returns('keychain-token')
250-
const netrcStub = sinon.stub(NetrcHandler.prototype, 'getAuth')
251-
252-
const auth = await credentialManager.getAuth(undefined, 'api.heroku.com')
253-
254-
expect(listAccountsStub.calledOnce).to.be.true
255-
expect(listAccountsStub.firstCall.args[0]).to.equal('heroku-cli')
256-
expect(getAuthStub.calledOnce).to.be.true
257-
expect(netrcStub.notCalled).to.be.true
258-
expect(auth).to.deep.equal({account: 'user@example.com', token: 'keychain-token'})
259-
})
260-
261-
it('should use the selected account when an account is not provided and multiple accounts are found', async function () {
262-
const listAccountsStub = sinon.stub(MacOSHandler.prototype, 'listAccounts').returns(['user1@example.com', 'user2@example.com'])
263-
const promptStub = (sinon.stub(inquirer, 'prompt')).resolves({account: 'user2@example.com'})
264-
const macosStub = sinon.stub(MacOSHandler.prototype, 'getAuth').returns('keychain-token')
265-
const netrcStub = sinon.stub(NetrcHandler.prototype, 'getAuth')
266-
267-
const auth = await credentialManager.getAuth(undefined, 'api.heroku.com')
268-
269-
expect(listAccountsStub.calledOnce).to.be.true
270-
expect(promptStub.calledOnce).to.be.true
271-
expect(macosStub.calledOnce).to.be.true
272-
expect(netrcStub.notCalled).to.be.true
273-
expect(auth).to.deep.equal({account: 'user2@example.com', token: 'keychain-token'})
274-
})
275-
276-
it('should fall back to netrc when an account is not provided and no accounts are found', async function () {
277-
sinon.stub(MacOSHandler.prototype, 'listAccounts').returns([])
278-
const macosStub = sinon.stub(MacOSHandler.prototype, 'getAuth')
279-
const netrcStub = sinon.stub(NetrcHandler.prototype, 'getAuth')
280-
netrcStub.resolves({login: 'user@example.com', password: 'netrc-token'})
281-
282-
stderr.start()
283-
284-
const auth = await credentialManager.getAuth(undefined, 'api.heroku.com')
285-
286-
expect(macosStub.notCalled).to.be.true
287-
expect(netrcStub.calledOnce).to.be.true
288-
expect(auth).to.deep.equal({account: 'user@example.com', token: 'netrc-token'})
289-
expect(unwrap(stderr.output)).to.contain('Warning: We can’t retrieve the Heroku token from heroku-cli.')
290-
expect(unwrap(stderr.output)).to.contain('We\'ll try to retrieve the token from the .netrc file instead.')
291-
expect(unwrap(stderr.output)).to.contain('To turn off this warning, set HEROKU_KEYCHAIN_WARNINGS to "off".')
292-
293-
stderr.stop()
294-
})
295-
296-
it('should fall back to netrc when an account is not provided and listAccounts fails', async function () {
297-
sinon.stub(MacOSHandler.prototype, 'listAccounts').throws(new Error('Keychain error'))
246+
it('should fall back to netrc when an account is not provided', async function () {
298247
const macosStub = sinon.stub(MacOSHandler.prototype, 'getAuth')
299248
const netrcStub = sinon.stub(NetrcHandler.prototype, 'getAuth')
300249
netrcStub.resolves({login: 'user@example.com', password: 'netrc-token'})
@@ -306,9 +255,6 @@ describe('credential-manager', function () {
306255
expect(macosStub.notCalled).to.be.true
307256
expect(netrcStub.calledOnce).to.be.true
308257
expect(auth).to.deep.equal({account: 'user@example.com', token: 'netrc-token'})
309-
expect(unwrap(stderr.output)).to.contain('Warning: We can’t retrieve the Heroku token from heroku-cli.')
310-
expect(unwrap(stderr.output)).to.contain('We\'ll try to retrieve the token from the .netrc file instead.')
311-
expect(unwrap(stderr.output)).to.contain('To turn off this warning, set HEROKU_KEYCHAIN_WARNINGS to "off".')
312258

313259
stderr.stop()
314260
})

test/credential-manager/lib/account-selector.test.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)