Skip to content

Commit 7675c41

Browse files
authored
fix: [#4544] JwtTokenExtractor.getIdentity:err! FetchError: request to https://login.botframework.com/v1/.well-known/openidconfiguration (#4583)
* Provide proxy setting to openIdMetadata * Replace @azure/ms-rest-js with @azure/core-http * Add missing agent settings in getKeys call
1 parent 95f6f80 commit 7675c41

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

libraries/botframework-connector/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
"botbuilder-stdlib": "4.1.6",
3535
"botframework-schema": "4.1.6",
3636
"cross-fetch": "^3.0.5",
37+
"https-proxy-agent": "^7.0.2",
3738
"jsonwebtoken": "^9.0.0",
39+
"node-fetch": "^2.6.7",
3840
"rsa-pem-from-mod-exp": "^0.8.4",
3941
"zod": "^3.22.4",
4042
"openssl-wrapper": "^0.3.4"

libraries/botframework-connector/src/auth/jwtTokenExtractor.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { EndorsementsValidator } from './endorsementsValidator';
1212
import { OpenIdMetadata } from './openIdMetadata';
1313
import { AuthenticationError } from './authenticationError';
1414
import { StatusCodes } from 'botframework-schema';
15+
import { ProxySettings } from '@azure/core-http';
1516

1617
/**
1718
* A JWT token processing class that gets identity information and performs security token validation.
@@ -32,17 +33,23 @@ export class JwtTokenExtractor {
3233
* @param tokenValidationParameters Token validation parameters.
3334
* @param metadataUrl Metadata Url.
3435
* @param allowedSigningAlgorithms Allowed signing algorithms.
36+
* @param proxySettings The proxy settings for the request.
3537
*/
36-
constructor(tokenValidationParameters: VerifyOptions, metadataUrl: string, allowedSigningAlgorithms: string[]) {
38+
constructor(
39+
tokenValidationParameters: VerifyOptions,
40+
metadataUrl: string,
41+
allowedSigningAlgorithms: string[],
42+
proxySettings?: ProxySettings
43+
) {
3744
this.tokenValidationParameters = { ...tokenValidationParameters };
3845
this.tokenValidationParameters.algorithms = allowedSigningAlgorithms;
39-
this.openIdMetadata = JwtTokenExtractor.getOrAddOpenIdMetadata(metadataUrl);
46+
this.openIdMetadata = JwtTokenExtractor.getOrAddOpenIdMetadata(metadataUrl, proxySettings);
4047
}
4148

42-
private static getOrAddOpenIdMetadata(metadataUrl: string): OpenIdMetadata {
49+
private static getOrAddOpenIdMetadata(metadataUrl: string, proxySettings?: ProxySettings): OpenIdMetadata {
4350
let metadata = this.openIdMetadataCache.get(metadataUrl);
4451
if (!metadata) {
45-
metadata = new OpenIdMetadata(metadataUrl);
52+
metadata = new OpenIdMetadata(metadataUrl, proxySettings);
4653
this.openIdMetadataCache.set(metadataUrl, metadata);
4754
}
4855

libraries/botframework-connector/src/auth/openIdMetadata.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
import * as getPem from 'rsa-pem-from-mod-exp';
1010
import base64url from 'base64url';
11-
import fetch from 'cross-fetch';
11+
import fetch from 'node-fetch';
12+
import { HttpsProxyAgent } from 'https-proxy-agent';
1213
import { AuthenticationError } from './authenticationError';
1314
import { StatusCodes } from 'botframework-schema';
15+
import { ProxySettings } from '@azure/core-http';
1416

1517
/**
1618
* Class in charge of manage OpenId metadata.
@@ -23,8 +25,9 @@ export class OpenIdMetadata {
2325
* Initializes a new instance of the [OpenIdMetadata](xref:botframework-connector.OpenIdMetadata) class.
2426
*
2527
* @param url Metadata Url.
28+
* @param proxySettings The proxy settings for the request.
2629
*/
27-
constructor(private url: string) {}
30+
constructor(private url: string, private proxySettings?: ProxySettings) {}
2831

2932
/**
3033
* Gets the Signing key.
@@ -56,12 +59,17 @@ export class OpenIdMetadata {
5659
* @private
5760
*/
5861
private async refreshCache(): Promise<void> {
59-
const res = await fetch(this.url);
62+
let agent = null;
63+
if (this.proxySettings) {
64+
const proxyUrl = `http://${this.proxySettings.host}:${this.proxySettings.port}`;
65+
agent = new HttpsProxyAgent(proxyUrl);
66+
}
67+
const res = await fetch(this.url, { agent: agent });
6068

6169
if (res.ok) {
6270
const openIdConfig = (await res.json()) as IOpenIdConfig;
6371

64-
const getKeyResponse = await fetch(openIdConfig.jwks_uri);
72+
const getKeyResponse = await fetch(openIdConfig.jwks_uri, { agent: agent });
6573
if (getKeyResponse.ok) {
6674
this.lastUpdated = new Date().getTime();
6775
this.keys = (await getKeyResponse.json()).keys as IKey[];

libraries/botframework-connector/src/auth/parameterizedBotFrameworkAuthentication.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ export class ParameterizedBotFrameworkAuthentication extends BotFrameworkAuthent
303303
const tokenExtractor = new JwtTokenExtractor(
304304
verifyOptions,
305305
this.toBotFromEmulatorOpenIdMetadataUrl,
306-
AuthenticationConstants.AllowedSigningAlgorithms
306+
AuthenticationConstants.AllowedSigningAlgorithms,
307+
this.connectorClientOptions?.proxySettings
307308
);
308309

309310
const parts: string[] = authHeader.split(' ');
@@ -389,7 +390,8 @@ export class ParameterizedBotFrameworkAuthentication extends BotFrameworkAuthent
389390
const tokenExtractor: JwtTokenExtractor = new JwtTokenExtractor(
390391
verifyOptions,
391392
this.toBotFromEmulatorOpenIdMetadataUrl,
392-
AuthenticationConstants.AllowedSigningAlgorithms
393+
AuthenticationConstants.AllowedSigningAlgorithms,
394+
this.connectorClientOptions?.proxySettings
393395
);
394396

395397
const identity: ClaimsIdentity = await tokenExtractor.getIdentityFromAuthHeader(
@@ -475,7 +477,8 @@ export class ParameterizedBotFrameworkAuthentication extends BotFrameworkAuthent
475477
const tokenExtractor: JwtTokenExtractor = new JwtTokenExtractor(
476478
tokenValidationParameters,
477479
this.toBotFromChannelOpenIdMetadataUrl,
478-
AuthenticationConstants.AllowedSigningAlgorithms
480+
AuthenticationConstants.AllowedSigningAlgorithms,
481+
this.connectorClientOptions?.proxySettings
479482
);
480483

481484
const identity: ClaimsIdentity = await tokenExtractor.getIdentityFromAuthHeader(

yarn.lock

+15
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,13 @@ agent-base@6:
27822782
dependencies:
27832783
debug "4"
27842784

2785+
agent-base@^7.0.2:
2786+
version "7.1.0"
2787+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-7.1.0.tgz#536802b76bc0b34aa50195eb2442276d613e3434"
2788+
integrity sha512-o/zjMZRhJxny7OyEF+Op8X+efiELC7k7yOjMzgfzVqOzXqkBkWI79YoTdOtsuWd5BWhAGAuOY/Xa6xpiaWXiNg==
2789+
dependencies:
2790+
debug "^4.3.4"
2791+
27852792
agentkeepalive@^4.1.3:
27862793
version "4.1.4"
27872794
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.1.4.tgz#d928028a4862cb11718e55227872e842a44c945b"
@@ -7398,6 +7405,14 @@ [email protected], https-proxy-agent@^5.0.0:
73987405
agent-base "6"
73997406
debug "4"
74007407

7408+
https-proxy-agent@^7.0.2:
7409+
version "7.0.2"
7410+
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.2.tgz#e2645b846b90e96c6e6f347fb5b2e41f1590b09b"
7411+
integrity sha512-NmLNjm6ucYwtcUmL7JQC1ZQ57LmHP4lT15FQ8D61nak1rO6DH+fz5qNK2Ap5UN4ZapYICE3/0KodcLYSPsPbaA==
7412+
dependencies:
7413+
agent-base "^7.0.2"
7414+
debug "4"
7415+
74017416
humanize-ms@^1.2.1:
74027417
version "1.2.1"
74037418
resolved "https://registry.yarnpkg.com/humanize-ms/-/humanize-ms-1.2.1.tgz#c46e3159a293f6b896da29316d8b6fe8bb79bbed"

0 commit comments

Comments
 (0)