Skip to content

Commit 8c0ad39

Browse files
impl
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
1 parent e49a10e commit 8c0ad39

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

code/extensions/che-api/src/extension.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export async function activate(_extensionContext: vscode.ExtensionContext): Prom
8181

8282
_extensionContext.subscriptions.push(
8383
vscode.commands.registerCommand('che-api.test-github-proxy', async () => {
84+
const diag = [
85+
`NODE_EXTRA_CA_CERTS=${process.env.NODE_EXTRA_CA_CERTS || '(not set)'}`,
86+
`HTTPS_PROXY=${process.env.HTTPS_PROXY || '(not set)'}`,
87+
`Node.js=${process.version}`,
88+
`fetch patched=${'__vscodeOriginalFetch' in globalThis}`,
89+
];
90+
console.log('test-github-proxy diagnostics:', diag.join(', '));
91+
8492
try {
8593
const user = await githubService.getUser();
8694
vscode.window.showInformationMessage(`Success! GitHub user: ${user.login}`);
@@ -90,6 +98,7 @@ export async function activate(_extensionContext: vscode.ExtensionContext): Prom
9098
const fullMessage = `Failed to get GitHub user: ${e.message}${cause}${code}`;
9199
console.error('test-github-proxy error:', e);
92100
console.error('test-github-proxy cause:', e.cause);
101+
console.error('test-github-proxy diagnostics:', diag.join(', '));
93102
vscode.window.showErrorMessage(fullMessage);
94103
}
95104
})

code/extensions/che-api/src/impl/github-service-impl.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/* eslint-disable header/header */
1212

13+
import * as http from 'http';
14+
import * as https from 'https';
1315
import * as k8s from '@kubernetes/client-node';
1416
import * as fs from 'fs-extra';
1517
import { inject, injectable } from 'inversify';
@@ -67,6 +69,14 @@ export class GithubServiceImpl implements GithubService {
6769
}
6870

6971
private async fetchGithubUser(token: string): Promise<{ user: GithubUser; scopes: string[] }> {
72+
try {
73+
return await this.fetchGithubUserWithFetch(token);
74+
} catch {
75+
return await this.fetchGithubUserWithHttps(token);
76+
}
77+
}
78+
79+
private async fetchGithubUserWithFetch(token: string): Promise<{ user: GithubUser; scopes: string[] }> {
7080
const response = await fetch('https://api.github.com/user', {
7181
headers: { Authorization: `Bearer ${token}` },
7282
});
@@ -83,6 +93,46 @@ export class GithubServiceImpl implements GithubService {
8393
return { user, scopes };
8494
}
8595

96+
private fetchGithubUserWithHttps(token: string): Promise<{ user: GithubUser; scopes: string[] }> {
97+
const url = 'https://api.github.com/user';
98+
return new Promise((resolve, reject) => {
99+
const module = url.startsWith('https:') ? https : http;
100+
const req = module.request(url, {
101+
method: 'GET',
102+
headers: {
103+
'Authorization': `Bearer ${token}`,
104+
'User-Agent': 'che-code',
105+
'Accept': 'application/json',
106+
},
107+
}, (res) => {
108+
const chunks: Buffer[] = [];
109+
res.on('data', (chunk: Buffer) => chunks.push(chunk));
110+
res.on('end', () => {
111+
const body = Buffer.concat(chunks).toString();
112+
if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) {
113+
reject(new Error(`GitHub user request failed: ${res.statusCode} ${res.statusMessage} - ${body}`));
114+
return;
115+
}
116+
try {
117+
const user = JSON.parse(body) as GithubUser;
118+
const scopesHeader = res.headers['x-oauth-scopes'] ?? '';
119+
const scopes = (Array.isArray(scopesHeader) ? scopesHeader[0] : scopesHeader)
120+
.split(', ')
121+
.map(scope => scope.trim())
122+
.filter(scope => scope.length > 0);
123+
resolve({ user, scopes });
124+
} catch (err) {
125+
reject(err);
126+
}
127+
});
128+
res.on('error', reject);
129+
});
130+
req.setTimeout(60 * 1000);
131+
req.on('error', reject);
132+
req.end();
133+
});
134+
}
135+
86136
async persistDeviceAuthToken(token: string): Promise<void> {
87137
this.token = token;
88138
this.logger.info(`Github Service: adding token to the device-authentication secret...`);

0 commit comments

Comments
 (0)