Skip to content

Commit b05b32b

Browse files
author
Rachel Macfarlane
committed
Use github auth server for provider
1 parent 6560269 commit b05b32b

4 files changed

Lines changed: 87 additions & 37 deletions

File tree

extensions/github-authentication/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
"activationEvents": [
1515
"*"
1616
],
17+
"contributes": {
18+
"commands": [
19+
{
20+
"command": "github.provide-token",
21+
"title": "Manually Provide Token"
22+
}
23+
],
24+
"menus": {
25+
"commandPalette": [
26+
{
27+
"command": "github.provide-token",
28+
"when": "false"
29+
}
30+
]
31+
}
32+
},
1733
"aiKey": "AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217",
1834
"main": "./out/extension.js",
1935
"scripts": {

extensions/github-authentication/src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export async function activate(context: vscode.ExtensionContext) {
1818

1919
await loginService.initialize();
2020

21+
context.subscriptions.push(vscode.commands.registerCommand('github.provide-token', () => {
22+
return loginService.manuallyProvideToken();
23+
}));
24+
2125
vscode.authentication.registerAuthenticationProvider({
2226
id: 'github',
2327
displayName: 'GitHub',

extensions/github-authentication/src/github.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export class GitHubAuthenticationProvider {
4444
// Ignore, network request failed
4545
}
4646

47-
await this.validateSessions();
47+
// TODO revert Cannot validate tokens from auth server, no available clientId
48+
// await this.validateSessions();
4849
this.pollForChange();
4950
}
5051

@@ -93,27 +94,6 @@ export class GitHubAuthenticationProvider {
9394
}, 1000 * 30);
9495
}
9596

96-
private async validateSessions(): Promise<void> {
97-
const validationPromises = this._sessions.map(async session => {
98-
try {
99-
await this._githubServer.validateToken(await session.getAccessToken());
100-
return session;
101-
} catch (e) {
102-
if (e === NETWORK_ERROR) {
103-
return session;
104-
}
105-
106-
return undefined;
107-
}
108-
});
109-
110-
const validSessions = (await Promise.all(validationPromises)).filter((x: vscode.AuthenticationSession | undefined): x is vscode.AuthenticationSession => !!x);
111-
if (validSessions.length !== this._sessions.length) {
112-
this._sessions = validSessions;
113-
this.storeSessions();
114-
}
115-
}
116-
11797
private async readSessions(): Promise<vscode.AuthenticationSession[]> {
11898
const storedSessions = await keychain.getToken();
11999
if (storedSessions) {
@@ -190,6 +170,10 @@ export class GitHubAuthenticationProvider {
190170
}
191171
}
192172

173+
public async manuallyProvideToken(): Promise<void> {
174+
this._githubServer.manuallyProvideToken();
175+
}
176+
193177
private async tokenToSession(token: string, scopes: string[]): Promise<vscode.AuthenticationSession> {
194178
const userInfo = await this._githubServer.getUserInfo(token);
195179
return {
@@ -216,13 +200,15 @@ export class GitHubAuthenticationProvider {
216200
public async logout(id: string) {
217201
const sessionIndex = this._sessions.findIndex(session => session.id === id);
218202
if (sessionIndex > -1) {
219-
const session = this._sessions.splice(sessionIndex, 1)[0];
220-
const token = await session.getAccessToken();
221-
try {
222-
await this._githubServer.revokeToken(token);
223-
} catch (_) {
224-
// ignore, should still remove from keychain
225-
}
203+
this._sessions.splice(sessionIndex, 1);
204+
// TODO revert
205+
// Cannot revoke tokens from auth server, no clientId available
206+
// const token = await session.getAccessToken();
207+
// try {
208+
// await this._githubServer.revokeToken(token);
209+
// } catch (_) {
210+
// // ignore, should still remove from keychain
211+
// }
226212
}
227213

228214
await this.storeSessions();

extensions/github-authentication/src/githubServer.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as https from 'https';
7+
import * as nls from 'vscode-nls';
78
import * as vscode from 'vscode';
89
import * as uuid from 'uuid';
910
import { PromiseAdapter, promiseFromEvent } from './common/utils';
1011
import Logger from './common/logger';
11-
import ClientRegistrar, { ClientDetails } from './common/clientRegistrar';
12+
import ClientRegistrar from './common/clientRegistrar';
13+
14+
const localize = nls.loadMessageBundle();
1215

1316
export const NETWORK_ERROR = 'network error';
17+
const AUTH_RELAY_SERVER = 'vscode-auth.github.com';
1418

1519
class UriEventHandler extends vscode.EventEmitter<vscode.Uri> implements vscode.UriHandler {
1620
public handleUri(uri: vscode.Uri) {
@@ -20,8 +24,8 @@ class UriEventHandler extends vscode.EventEmitter<vscode.Uri> implements vscode.
2024

2125
export const uriHandler = new UriEventHandler;
2226

23-
const exchangeCodeForToken: (state: string, clientDetails: ClientDetails) => PromiseAdapter<vscode.Uri, string> =
24-
(state, clientDetails) => async (uri, resolve, reject) => {
27+
const exchangeCodeForToken: (state: string, host: string, getPath: (code: string) => string) => PromiseAdapter<vscode.Uri, string> =
28+
(state, host, getPath) => async (uri, resolve, reject) => {
2529
Logger.info('Exchanging code for token...');
2630
const query = parseQuery(uri);
2731
const code = query.code;
@@ -32,8 +36,8 @@ const exchangeCodeForToken: (state: string, clientDetails: ClientDetails) => Pro
3236
}
3337

3438
const post = https.request({
35-
host: 'github.com',
36-
path: `/login/oauth/access_token?client_id=${clientDetails.id}&client_secret=${clientDetails.secret}&state=${query.state}&code=${code}`,
39+
host: host,
40+
path: getPath(code),
3741
method: 'POST',
3842
headers: {
3943
Accept: 'application/json'
@@ -69,15 +73,55 @@ function parseQuery(uri: vscode.Uri) {
6973
}
7074

7175
export class GitHubServer {
76+
private _statusBarItem: vscode.StatusBarItem | undefined;
77+
7278
public async login(scopes: string): Promise<string> {
7379
Logger.info('Logging in...');
80+
this.updateStatusBarItem(true);
81+
7482
const state = uuid();
7583
const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${vscode.env.uriScheme}://vscode.github-authentication/did-authenticate`));
7684
const clientDetails = scopes === 'vso' ? ClientRegistrar.getGitHubAppDetails() : ClientRegistrar.getClientDetails(callbackUri);
77-
const uri = vscode.Uri.parse(`https://github.com/login/oauth/authorize?redirect_uri=${encodeURIComponent(callbackUri.toString())}&scope=${scopes}&state=${state}&client_id=${clientDetails.id}`);
85+
const uri = scopes !== 'vso'
86+
? vscode.Uri.parse(`https://${AUTH_RELAY_SERVER}/authorize/?callbackUri=${encodeURIComponent(callbackUri.toString())}&scope=${scopes}&state=${state}&responseType=code`)
87+
: vscode.Uri.parse(`https://github.com/login/oauth/authorize?redirect_uri=${encodeURIComponent(callbackUri.toString())}&scope=${scopes}&state=${state}&client_id=${clientDetails.id}`);
7888

7989
vscode.env.openExternal(uri);
80-
return promiseFromEvent(uriHandler.event, exchangeCodeForToken(state, clientDetails));
90+
91+
return promiseFromEvent(uriHandler.event, exchangeCodeForToken(state, AUTH_RELAY_SERVER, (code) => {
92+
return scopes !== 'vso'
93+
? `/token?code=${code}&state=${state}`
94+
: `/login/oauth/access_token?client_id=${clientDetails.id}&client_secret=${clientDetails.secret}&state=${state}&code=${code}&authServer=github.com`;
95+
})).finally(() => {
96+
this.updateStatusBarItem(false);
97+
});
98+
}
99+
100+
private updateStatusBarItem(isStart?: boolean) {
101+
if (isStart && !this._statusBarItem) {
102+
this._statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
103+
this._statusBarItem.text = localize('signingIn', "$(mark-github) Signing in to github.com...");
104+
this._statusBarItem.command = 'github.provide-token';
105+
this._statusBarItem.show();
106+
}
107+
108+
if (!isStart && this._statusBarItem) {
109+
this._statusBarItem.dispose();
110+
this._statusBarItem = undefined;
111+
}
112+
}
113+
114+
public async manuallyProvideToken() {
115+
const uriOrToken = await vscode.window.showInputBox({ prompt: 'Token', ignoreFocusOut: true });
116+
if (!uriOrToken) { return; }
117+
try {
118+
const uri = vscode.Uri.parse(uriOrToken);
119+
if (!uri.scheme || uri.scheme === 'file') { throw new Error; }
120+
uriHandler.handleUri(uri);
121+
} catch (e) {
122+
Logger.error(e);
123+
vscode.window.showErrorMessage(localize('unexpectedInput', "The input did not matched the expected format"));
124+
}
81125
}
82126

83127
public async hasUserInstallation(token: string): Promise<boolean> {
@@ -122,7 +166,7 @@ export class GitHubServer {
122166
const uri = vscode.Uri.parse(`https://github.com/apps/microsoft-visual-studio-code/installations/new?state=${state}`);
123167

124168
vscode.env.openExternal(uri);
125-
return promiseFromEvent(uriHandler.event, exchangeCodeForToken(state, clientDetails));
169+
return promiseFromEvent(uriHandler.event, exchangeCodeForToken(state, 'github.com', (code) => `/login/oauth/access_token?client_id=${clientDetails.id}&client_secret=${clientDetails.secret}&state=${state}&code=${code}`));
126170
}
127171

128172
public async getUserInfo(token: string): Promise<{ id: string, accountName: string }> {

0 commit comments

Comments
 (0)