Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/github/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ export interface CredentialStoreSessionsChangeEvent extends vscode.Authenticatio
serverChanged: boolean;
}

// Thrown by `vscode.authentication.getSession` when the enterprise provider is not registered. Some hosts
// contribute the provider but only register it once an enterprise URI is configured, which surfaces as the timeout.
const ENTERPRISE_PROVIDER_UNAVAILABLE_MESSAGES = [
`No authentication provider '${AuthProvider.githubEnterprise}' is currently registered.`,
`Timed out waiting for authentication provider '${AuthProvider.githubEnterprise}' to register.`,
];

export class CredentialStore extends Disposable {
private static readonly ID = 'Authentication';
private _githubAPI: GitHub | undefined;
Expand Down Expand Up @@ -737,7 +744,7 @@ export class CredentialStore extends Disposable {
return { session, isNew: !!session, scopes: requireScopes ? scopes : SCOPES_OLD };
} catch (error) {
if (authProviderId === AuthProvider.githubEnterprise && error instanceof Error
&& error.message === `No authentication provider '${AuthProvider.githubEnterprise}' is currently registered.`) {
&& ENTERPRISE_PROVIDER_UNAVAILABLE_MESSAGES.includes(error.message)) {
Logger.appendLine('GitHub Enterprise authentication is unavailable. Set up GitHub Enterprise to sign in.', CredentialStore.ID);
return { session: undefined, isNew: false, scopes };
}
Expand Down
39 changes: 38 additions & 1 deletion src/test/github/credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { strictEqual, deepStrictEqual } from 'assert';
import { strictEqual, deepStrictEqual, rejects } from 'assert';
import { Octokit } from '@octokit/rest';
import { ApolloClient, ApolloLink, InMemoryCache } from 'apollo-boost';
import { createSandbox, match, SinonSandbox, SinonStub } from 'sinon';
Expand Down Expand Up @@ -403,6 +403,43 @@ describe('CredentialStore', function () {
});
}

describe('GitHub Enterprise provider that is not registered', function () {
let credentialStore: CredentialStore;
let getSession: SinonStub;

beforeEach(function () {
sinon.stub(process, 'env').value({ ...process.env, GITHUB_OAUTH_TOKEN: undefined });
credentialStore = new CredentialStore(new MockTelemetry(), new MockExtensionContext());
getSession = sinon.stub(vscode.authentication, 'getSession');
getSession.withArgs(AuthProvider.github).resolves(createSession('dotcom-session', 'account', defaultScopes));
});

afterEach(function () {
credentialStore.dispose();
});

for (const message of [
`No authentication provider '${AuthProvider.githubEnterprise}' is currently registered.`,
`Timed out waiting for authentication provider '${AuthProvider.githubEnterprise}' to register.`,
]) {
it(`still signs in to GitHub.com when the enterprise lookup fails with "${message}"`, async function () {
getSession.withArgs(AuthProvider.githubEnterprise).rejects(new Error(message));

deepStrictEqual(await credentialStore.create(), { canceled: false, unavailable: false });
strictEqual(credentialStore.isAuthenticated(AuthProvider.github), true);
strictEqual(credentialStore.isAuthenticated(AuthProvider.githubEnterprise), false);
});
}

it('does not hide enterprise errors with other messages', async function () {
getSession.withArgs(AuthProvider.githubEnterprise).rejects(new Error('Timed out waiting for authentication provider \'github\' to register.'));
await rejects(credentialStore.create(), /Timed out waiting for authentication provider 'github' to register/);

getSession.withArgs(AuthProvider.githubEnterprise).rejects(new Error('Network down'));
await rejects(credentialStore.create(), /Network down/);
});
});

it('retries the current user request after a failure', async function () {
const telemetry = new MockTelemetry();
const credentialStore = new CredentialStore(telemetry, new MockExtensionContext());
Expand Down