Skip to content

Commit e400f5d

Browse files
committed
Fixes proxy issue with browser auth
1 parent fad86f7 commit e400f5d

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

desktop/src/client/core/aad/auth-provider.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { AuthObserver } from "./auth-observer";
1414
import { shell } from "electron";
1515
import { AuthLoopbackClient } from "./auth-loopback-client";
1616

17+
import "global-agent/bootstrap";
18+
import { ProxyNetworkClient } from "./proxy-network-client";
19+
1720
const MSAL_SCOPES = ["user_impersonation"];
1821

1922
export type AuthorizationResult = AuthenticationResult;
@@ -245,18 +248,21 @@ export default class AuthProvider {
245248
private async _createClient(tenantId: string):
246249
Promise<PublicClientApplication> {
247250
const proxyUrl = await this._loadProxyUrl();
251+
let networkClient;
248252

249253
if (proxyUrl) {
250254
log.info(`[${tenantId}] Proxying auth endpoints through ` +
251255
proxyUrl);
256+
process.env.GLOBAL_AGENT_HTTP_PROXY = proxyUrl;
257+
networkClient = new ProxyNetworkClient(proxyUrl);
252258
}
253259

254260
const authority =
255261
`${this.app.properties.azureEnvironment.aadUrl}${tenantId}/`;
256262

257263
return new PublicClientApplication({
258264
system: {
259-
proxyUrl
265+
networkClient
260266
},
261267
auth: {
262268
clientId: this.config.clientId,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { INetworkModule, NetworkRequestOptions, NetworkResponse } from "@azure/msal-node";
2+
import * as HttpsProxyAgent from "https-proxy-agent";
3+
import fetch from "node-fetch";
4+
5+
export class ProxyNetworkClient implements INetworkModule {
6+
private proxyAgent: HttpsProxyAgent;
7+
constructor(proxyUrl: string) {
8+
this.proxyAgent = new HttpsProxyAgent(proxyUrl);
9+
}
10+
11+
sendGetRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<NetworkResponse<T>> {
12+
return this.sendRequestAsync(url, "GET", options);
13+
}
14+
sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<NetworkResponse<T>> {
15+
return this.sendRequestAsync(url, "POST", options);
16+
}
17+
18+
private async sendRequestAsync<T>(
19+
url: string,
20+
method: "GET" | "POST",
21+
options: NetworkRequestOptions = {},
22+
): Promise<NetworkResponse<T>> {
23+
try {
24+
const requestOptions = {
25+
method: method,
26+
headers: options.headers,
27+
body: method === "POST" ? options.body : undefined,
28+
agent: this.proxyAgent,
29+
};
30+
31+
const response = await fetch(url, requestOptions);
32+
const data = await response.json() as any;
33+
34+
const headersObj: Record<string, string> = {};
35+
response.headers.forEach((value, key) => {
36+
headersObj[key] = value;
37+
});
38+
39+
return {
40+
headers: headersObj,
41+
body: data,
42+
status: response.status,
43+
};
44+
} catch (err) {
45+
console.error("CustomRequest", err);
46+
throw new Error("Custom request error");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)