Skip to content

Commit

Permalink
Merge branch 'dev' into claims_bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie-Microsoft authored Apr 30, 2024
2 parents 94848e4 + 23280a4 commit 6722bcd
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/msal-node-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [16, 18, 20]
node: [16, 18, 20, 22]
sample:
- "auth-code"
- "auth-code-cli-app"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Export invoke and invokeAsync functions #7065",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": " Fixed inconsistencies with cancellationToken (timeout)",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": " Fixed inconsistencies with cancellationToken (timeout)",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch"
}
5 changes: 4 additions & 1 deletion lib/msal-browser/src/utils/BrowserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { UrlString } from "@azure/msal-common";
import { UrlString, invoke, invokeAsync } from "@azure/msal-common";
import {
createBrowserAuthError,
BrowserAuthErrorCodes,
Expand Down Expand Up @@ -206,3 +206,6 @@ export function preconnect(authority: string): void {
export function createGuid(): string {
return BrowserCrypto.createNewGuid();
}

export { invoke };
export { invokeAsync };
2 changes: 1 addition & 1 deletion lib/msal-common/src/network/INetworkModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface INetworkModule {
sendGetRequestAsync<T>(
url: string,
options?: NetworkRequestOptions,
cancellationToken?: number
timeout?: number
): Promise<NetworkResponse<T>>;

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/msal-common/src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ export const Constants = {
};

export const HttpStatus = {
SUCCESS_RANGE_START: 200,
SUCCESS: 200,
SUCCESS_RANGE_START: 200,
SUCCESS_RANGE_END: 299,
REDIRECT: 302,
CLIENT_ERROR: 400,
CLIENT_ERROR_RANGE_START: 400,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
NOT_FOUND: 404,
REQUEST_TIMEOUT: 408,
TOO_MANY_REQUESTS: 429,
CLIENT_ERROR_RANGE_END: 499,
SERVER_ERROR: 500,
SERVER_ERROR_RANGE_START: 500,
INTERNAL_SERVER_ERROR: 500,
SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504,
SERVER_ERROR_RANGE_END: 599,
MULTI_SIDED_ERROR: 600,
} as const;
export type HttpStatus = (typeof HttpStatus)[keyof typeof HttpStatus];

Expand Down
2 changes: 1 addition & 1 deletion lib/msal-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Any major MSAL Node release:

| MSAL Node version | MSAL support status | Supported Node versions |
| ----------------- | ------------------- | ----------------------- |
| 2.x.x | Active development | 16, 18, 20 |
| 2.x.x | Active development | 16, 18, 20, 22 |
| 1.x.x | In maintenance | 10, 12, 14, 16, 18 |

**Note:** There have been no functional changes in the MSAL Node v2 release.
Expand Down
53 changes: 23 additions & 30 deletions lib/msal-node/src/network/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,25 @@ export class HttpClient implements INetworkModule {
*/
async sendGetRequestAsync<T>(
url: string,
options?: NetworkRequestOptions
options?: NetworkRequestOptions,
timeout?: number
): Promise<NetworkResponse<T>> {
if (this.proxyUrl) {
return networkRequestViaProxy(
url,
this.proxyUrl,
HttpMethod.GET,
options,
this.customAgentOptions as http.AgentOptions
this.customAgentOptions as http.AgentOptions,
timeout
);
} else {
return networkRequestViaHttps(
url,
HttpMethod.GET,
options,
this.customAgentOptions as https.AgentOptions,
undefined
timeout
);
}
}
Expand All @@ -64,25 +66,22 @@ export class HttpClient implements INetworkModule {
*/
async sendPostRequestAsync<T>(
url: string,
options?: NetworkRequestOptions,
cancellationToken?: number
options?: NetworkRequestOptions
): Promise<NetworkResponse<T>> {
if (this.proxyUrl) {
return networkRequestViaProxy(
url,
this.proxyUrl,
HttpMethod.POST,
options,
this.customAgentOptions as http.AgentOptions,
cancellationToken
this.customAgentOptions as http.AgentOptions
);
} else {
return networkRequestViaHttps(
url,
HttpMethod.POST,
options,
this.customAgentOptions as https.AgentOptions,
cancellationToken
this.customAgentOptions as https.AgentOptions
);
}
}
Expand All @@ -109,10 +108,6 @@ const networkRequestViaProxy = <T>(
headers: headers,
};

if (timeout) {
tunnelRequestOptions.timeout = timeout;
}

if (agentOptions && Object.keys(agentOptions).length) {
tunnelRequestOptions.agent = new http.Agent(agentOptions);
}
Expand All @@ -125,6 +120,11 @@ const networkRequestViaProxy = <T>(
"Content-Type: application/x-www-form-urlencoded\r\n" +
`Content-Length: ${body.length}\r\n` +
`\r\n${body}`;
} else {
// optional timeout is only for get requests (regionDiscovery, for example)
if (timeout) {
tunnelRequestOptions.timeout = timeout;
}
}
const outgoingRequestString =
`${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
Expand All @@ -136,7 +136,7 @@ const networkRequestViaProxy = <T>(
return new Promise<NetworkResponse<T>>((resolve, reject) => {
const request = http.request(tunnelRequestOptions);

if (tunnelRequestOptions.timeout) {
if (timeout) {
request.on("timeout", () => {
request.destroy();
reject(new Error("Request time out"));
Expand Down Expand Up @@ -165,14 +165,6 @@ const networkRequestViaProxy = <T>(
)
);
}
if (tunnelRequestOptions.timeout) {
socket.setTimeout(tunnelRequestOptions.timeout);
socket.on("timeout", () => {
request.destroy();
socket.destroy();
reject(new Error("Request time out"));
});
}

// make a request over an HTTP tunnel
socket.write(outgoingRequestString);
Expand Down Expand Up @@ -291,10 +283,6 @@ const networkRequestViaHttps = <T>(
...NetworkUtils.urlToHttpOptions(url),
};

if (timeout) {
customOptions.timeout = timeout;
}

if (agentOptions && Object.keys(agentOptions).length) {
customOptions.agent = new https.Agent(agentOptions);
}
Expand All @@ -305,6 +293,11 @@ const networkRequestViaHttps = <T>(
...customOptions.headers,
"Content-Length": body.length,
};
} else {
// optional timeout is only for get requests (regionDiscovery, for example)
if (timeout) {
customOptions.timeout = timeout;
}
}

return new Promise<NetworkResponse<T>>((resolve, reject) => {
Expand All @@ -316,17 +309,17 @@ const networkRequestViaHttps = <T>(
request = https.request(customOptions);
}

if (isPostRequest) {
request.write(body);
}

if (timeout) {
request.on("timeout", () => {
request.destroy();
reject(new Error("Request time out"));
});
}

if (isPostRequest) {
request.write(body);
}

request.end();

request.on("response", (response) => {
Expand Down
9 changes: 5 additions & 4 deletions lib/msal-node/src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ export const HttpMethod = {
export type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];

export const ProxyStatus = {
SUCCESS_RANGE_START: 200,
SUCCESS_RANGE_END: 299,
SERVER_ERROR: 500,
SUCCESS: HttpStatus.SUCCESS,
SUCCESS_RANGE_START: HttpStatus.SUCCESS_RANGE_START,
SUCCESS_RANGE_END: HttpStatus.SUCCESS_RANGE_END,
SERVER_ERROR: HttpStatus.SERVER_ERROR,
} as const;
export type ProxyStatus = (typeof ProxyStatus)[keyof typeof ProxyStatus];

Expand Down Expand Up @@ -160,7 +161,7 @@ export const MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON = [
HttpStatus.NOT_FOUND,
HttpStatus.REQUEST_TIMEOUT,
HttpStatus.TOO_MANY_REQUESTS,
HttpStatus.INTERNAL_SERVER_ERROR,
HttpStatus.SERVER_ERROR,
HttpStatus.SERVICE_UNAVAILABLE,
HttpStatus.GATEWAY_TIMEOUT,
];
Loading

0 comments on commit 6722bcd

Please sign in to comment.