azure-ad-verify-token icon indicating copy to clipboard operation
azure-ad-verify-token copied to clipboard

Proxy or custom requestClient configuration (aplications running behinden enterprise proxy)

Open AlexVFornazieri opened this issue 10 months ago • 0 comments

Describe the Feature

Implement a way to informe a proxyAgent instance or proxy configurations to the node-fetch to aplications running behinden enterprise proxy;

Suggested Solution

Somenting like msal-node Configuration.system.networkClient https://azuread.github.io/microsoft-authentication-library-for-js/ref/types/_azure_msal_node.NodeSystemOptions.html#__type.networkClient

Other Information

INetworkModule example (usede in SAML)

import type { INetworkModule, NetworkRequestOptions, NetworkResponse } from "@azure/msal-node";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch from 'node-fetch';

const proxyUrl = process.env['HTTPS_PROXY'] || process.env['HTTP_PROXY'];
if (!proxyUrl) {
    throw new Error('Missing HTTP/S_PROXY env');
}

export const proxyAgent = new HttpsProxyAgent(proxyUrl);

export class CustomHttpClient implements INetworkModule {
    sendGetRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<NetworkResponse<T>> {
        return this.sendRequestAsync(url, 'GET', options);
    }
    sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<NetworkResponse<T>> {
        return this.sendRequestAsync(url, 'POST', options);
    }

    private async sendRequestAsync<T>(
        url: string,
        method: 'GET' | 'POST',
        options: NetworkRequestOptions = {},
    ): Promise<NetworkResponse<T>> {
        try {
            const requestOptions = {
                method: method,
                headers: options.headers,
                body: method === 'POST' ? options.body : undefined,
                agent: proxyAgent,
            };

            console.log('>>> url', url, requestOptions);

            const response = await fetch(url, requestOptions);
            const data = await response.json() as any;

            const headersObj: Record<string, string> = {};
            response.headers.forEach((value, key) => {
                headersObj[key] = value;
            });

            return {
                headers: headersObj,
                body: data,
                status: response.status,
            };
        } catch (err) {
            console.error('CustomRequest', err);
            throw new Error('Custom request error');
        }
    }
}

AlexVFornazieri avatar Apr 23 '24 19:04 AlexVFornazieri