@azure/msal-node 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/dist/cache/NodeStorage.cjs +1 -1
  2. package/dist/cache/NodeStorage.mjs +1 -1
  3. package/dist/cache/TokenCache.cjs +1 -1
  4. package/dist/cache/TokenCache.mjs +1 -1
  5. package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
  6. package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
  7. package/dist/cache/serializer/Deserializer.cjs +1 -1
  8. package/dist/cache/serializer/Deserializer.mjs +1 -1
  9. package/dist/cache/serializer/Serializer.cjs +1 -1
  10. package/dist/cache/serializer/Serializer.mjs +1 -1
  11. package/dist/client/ClientApplication.cjs +2 -2
  12. package/dist/client/ClientApplication.cjs.map +1 -1
  13. package/dist/client/ClientApplication.d.ts.map +1 -1
  14. package/dist/client/ClientApplication.mjs +3 -3
  15. package/dist/client/ClientApplication.mjs.map +1 -1
  16. package/dist/client/ClientAssertion.cjs +2 -2
  17. package/dist/client/ClientAssertion.cjs.map +1 -1
  18. package/dist/client/ClientAssertion.d.ts.map +1 -1
  19. package/dist/client/ClientAssertion.mjs +3 -3
  20. package/dist/client/ClientAssertion.mjs.map +1 -1
  21. package/dist/client/ClientCredentialClient.cjs +22 -23
  22. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  23. package/dist/client/ClientCredentialClient.d.ts +0 -1
  24. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  25. package/dist/client/ClientCredentialClient.mjs +23 -24
  26. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  27. package/dist/client/ConfidentialClientApplication.cjs +4 -4
  28. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  29. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  30. package/dist/client/ConfidentialClientApplication.mjs +5 -5
  31. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  32. package/dist/client/DeviceCodeClient.cjs +6 -6
  33. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  34. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  35. package/dist/client/DeviceCodeClient.mjs +7 -7
  36. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  37. package/dist/client/OnBehalfOfClient.cjs +4 -4
  38. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  39. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  40. package/dist/client/OnBehalfOfClient.mjs +5 -5
  41. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  42. package/dist/client/PublicClientApplication.cjs +56 -9
  43. package/dist/client/PublicClientApplication.cjs.map +1 -1
  44. package/dist/client/PublicClientApplication.d.ts +6 -0
  45. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  46. package/dist/client/PublicClientApplication.mjs +58 -11
  47. package/dist/client/PublicClientApplication.mjs.map +1 -1
  48. package/dist/client/UsernamePasswordClient.cjs +1 -1
  49. package/dist/client/UsernamePasswordClient.mjs +1 -1
  50. package/dist/config/Configuration.cjs +1 -1
  51. package/dist/config/Configuration.mjs +1 -1
  52. package/dist/crypto/CryptoProvider.cjs +1 -1
  53. package/dist/crypto/CryptoProvider.mjs +1 -1
  54. package/dist/crypto/GuidGenerator.cjs +1 -1
  55. package/dist/crypto/GuidGenerator.mjs +1 -1
  56. package/dist/crypto/HashUtils.cjs +1 -1
  57. package/dist/crypto/HashUtils.mjs +1 -1
  58. package/dist/crypto/PkceGenerator.cjs +1 -1
  59. package/dist/crypto/PkceGenerator.mjs +1 -1
  60. package/dist/error/NodeAuthError.cjs +1 -1
  61. package/dist/error/NodeAuthError.mjs +1 -1
  62. package/dist/index.cjs +17 -1
  63. package/dist/index.cjs.map +1 -1
  64. package/dist/index.d.ts +1 -1
  65. package/dist/index.d.ts.map +1 -1
  66. package/dist/index.mjs +2 -2
  67. package/dist/internals.cjs +1 -1
  68. package/dist/internals.mjs +1 -1
  69. package/dist/network/HttpClient.cjs +1 -1
  70. package/dist/network/HttpClient.mjs +1 -1
  71. package/dist/network/LoopbackClient.cjs +16 -24
  72. package/dist/network/LoopbackClient.cjs.map +1 -1
  73. package/dist/network/LoopbackClient.d.ts.map +1 -1
  74. package/dist/network/LoopbackClient.mjs +17 -25
  75. package/dist/network/LoopbackClient.mjs.map +1 -1
  76. package/dist/packageMetadata.cjs +2 -2
  77. package/dist/packageMetadata.d.ts +1 -1
  78. package/dist/packageMetadata.mjs +2 -2
  79. package/dist/utils/Constants.cjs +1 -1
  80. package/dist/utils/Constants.mjs +1 -1
  81. package/dist/utils/EncodingUtils.cjs +1 -1
  82. package/dist/utils/EncodingUtils.mjs +1 -1
  83. package/dist/utils/NetworkUtils.cjs +1 -1
  84. package/dist/utils/NetworkUtils.mjs +1 -1
  85. package/package.json +2 -2
  86. package/src/client/ClientApplication.ts +3 -2
  87. package/src/client/ClientAssertion.ts +7 -2
  88. package/src/client/ClientCredentialClient.ts +32 -34
  89. package/src/client/ConfidentialClientApplication.ts +11 -4
  90. package/src/client/DeviceCodeClient.ts +16 -7
  91. package/src/client/OnBehalfOfClient.ts +11 -4
  92. package/src/client/PublicClientApplication.ts +69 -13
  93. package/src/index.ts +5 -0
  94. package/src/network/LoopbackClient.ts +18 -33
  95. package/src/packageMetadata.ts +1 -1
@@ -3,7 +3,11 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { ApiId, Constants } from "../utils/Constants.js";
6
+ import {
7
+ ApiId,
8
+ Constants,
9
+ LOOPBACK_SERVER_CONSTANTS,
10
+ } from "../utils/Constants.js";
7
11
  import {
8
12
  AuthenticationResult,
9
13
  CommonDeviceCodeRequest,
@@ -26,7 +30,7 @@ import { DeviceCodeRequest } from "../request/DeviceCodeRequest.js";
26
30
  import { AuthorizationUrlRequest } from "../request/AuthorizationUrlRequest.js";
27
31
  import { AuthorizationCodeRequest } from "../request/AuthorizationCodeRequest.js";
28
32
  import { InteractiveRequest } from "../request/InteractiveRequest.js";
29
- import { NodeAuthError } from "../error/NodeAuthError.js";
33
+ import { NodeAuthError, NodeAuthErrorMessage } from "../error/NodeAuthError.js";
30
34
  import { LoopbackClient } from "../network/LoopbackClient.js";
31
35
  import { SilentFlowRequest } from "../request/SilentFlowRequest.js";
32
36
  import { SignOutRequest } from "../request/SignOutRequest.js";
@@ -167,13 +171,21 @@ export class PublicClientApplication
167
171
  const loopbackClient: ILoopbackClient =
168
172
  customLoopbackClient || new LoopbackClient();
169
173
 
170
- let authCodeListener: Promise<ServerAuthorizationCodeResponse>;
174
+ let authCodeResponse: ServerAuthorizationCodeResponse = {};
175
+ let authCodeListenerError: AuthError | null = null;
171
176
  try {
172
- authCodeListener = loopbackClient.listenForAuthCode(
173
- successTemplate,
174
- errorTemplate
175
- );
176
- const redirectUri = loopbackClient.getRedirectUri();
177
+ const authCodeListener = loopbackClient
178
+ .listenForAuthCode(successTemplate, errorTemplate)
179
+ .then((response) => {
180
+ authCodeResponse = response;
181
+ })
182
+ .catch((e) => {
183
+ // Store the promise instead of throwing so we can control when its thrown
184
+ authCodeListenerError = e;
185
+ });
186
+
187
+ // Wait for server to be listening
188
+ const redirectUri = await this.waitForRedirectUri(loopbackClient);
177
189
 
178
190
  const validRequest: AuthorizationUrlRequest = {
179
191
  ...remainingProperties,
@@ -187,9 +199,10 @@ export class PublicClientApplication
187
199
 
188
200
  const authCodeUrl = await this.getAuthCodeUrl(validRequest);
189
201
  await openBrowser(authCodeUrl);
190
- const authCodeResponse = await authCodeListener.finally(() => {
191
- loopbackClient.closeServer();
192
- });
202
+ await authCodeListener;
203
+ if (authCodeListenerError) {
204
+ throw authCodeListenerError;
205
+ }
193
206
 
194
207
  if (authCodeResponse.error) {
195
208
  throw new ServerError(
@@ -209,9 +222,8 @@ export class PublicClientApplication
209
222
  ...validRequest,
210
223
  };
211
224
  return this.acquireTokenByCode(tokenRequest);
212
- } catch (e) {
225
+ } finally {
213
226
  loopbackClient.closeServer();
214
- throw e;
215
227
  }
216
228
  }
217
229
 
@@ -280,4 +292,48 @@ export class PublicClientApplication
280
292
 
281
293
  return this.getTokenCache().getAllAccounts();
282
294
  }
295
+
296
+ /**
297
+ * Attempts to retrieve the redirectUri from the loopback server. If the loopback server does not start listening for requests within the timeout this will throw.
298
+ * @param loopbackClient
299
+ * @returns
300
+ */
301
+ private async waitForRedirectUri(
302
+ loopbackClient: ILoopbackClient
303
+ ): Promise<string> {
304
+ return new Promise<string>((resolve, reject) => {
305
+ let ticks = 0;
306
+ const id = setInterval(() => {
307
+ if (
308
+ LOOPBACK_SERVER_CONSTANTS.TIMEOUT_MS /
309
+ LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS <
310
+ ticks
311
+ ) {
312
+ clearInterval(id);
313
+ reject(NodeAuthError.createLoopbackServerTimeoutError());
314
+ return;
315
+ }
316
+
317
+ try {
318
+ const r = loopbackClient.getRedirectUri();
319
+ clearInterval(id);
320
+ resolve(r);
321
+ return;
322
+ } catch (e) {
323
+ if (
324
+ e instanceof AuthError &&
325
+ e.errorCode ===
326
+ NodeAuthErrorMessage.noLoopbackServerExists.code
327
+ ) {
328
+ // Loopback server is not listening yet
329
+ ticks++;
330
+ return;
331
+ }
332
+ clearInterval(id);
333
+ reject(e);
334
+ return;
335
+ }
336
+ }, LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS);
337
+ });
338
+ }
283
339
  }
package/src/index.ts CHANGED
@@ -90,11 +90,15 @@ export {
90
90
  // Error
91
91
  AuthError,
92
92
  AuthErrorMessage,
93
+ AuthErrorCodes,
93
94
  ClientAuthError,
95
+ ClientAuthErrorCodes,
94
96
  ClientAuthErrorMessage,
95
97
  ClientConfigurationError,
98
+ ClientConfigurationErrorCodes,
96
99
  ClientConfigurationErrorMessage,
97
100
  InteractionRequiredAuthError,
101
+ InteractionRequiredAuthErrorCodes,
98
102
  InteractionRequiredAuthErrorMessage,
99
103
  ServerError,
100
104
  // Network Interface
@@ -116,6 +120,7 @@ export {
116
120
  IAppTokenProvider,
117
121
  AppTokenProviderParameters,
118
122
  AppTokenProviderResult,
123
+ INativeBrokerPlugin,
119
124
  } from "@azure/msal-common";
120
125
 
121
126
  export { version } from "./packageMetadata.js";
@@ -11,11 +11,11 @@ import {
11
11
  } from "@azure/msal-common";
12
12
  import http from "http";
13
13
  import { NodeAuthError } from "../error/NodeAuthError.js";
14
- import { Constants, LOOPBACK_SERVER_CONSTANTS } from "../utils/Constants.js";
14
+ import { Constants } from "../utils/Constants.js";
15
15
  import { ILoopbackClient } from "./ILoopbackClient.js";
16
16
 
17
17
  export class LoopbackClient implements ILoopbackClient {
18
- private server: http.Server;
18
+ private server: http.Server | undefined;
19
19
 
20
20
  /**
21
21
  * Spins up a loopback server which returns the server response when the localhost redirectUri is hit
@@ -27,17 +27,14 @@ export class LoopbackClient implements ILoopbackClient {
27
27
  successTemplate?: string,
28
28
  errorTemplate?: string
29
29
  ): Promise<ServerAuthorizationCodeResponse> {
30
- if (!!this.server) {
30
+ if (this.server) {
31
31
  throw NodeAuthError.createLoopbackServerAlreadyExistsError();
32
32
  }
33
33
 
34
- const authCodeListener = new Promise<ServerAuthorizationCodeResponse>(
34
+ return new Promise<ServerAuthorizationCodeResponse>(
35
35
  (resolve, reject) => {
36
36
  this.server = http.createServer(
37
- async (
38
- req: http.IncomingMessage,
39
- res: http.ServerResponse
40
- ) => {
37
+ (req: http.IncomingMessage, res: http.ServerResponse) => {
41
38
  const url = req.url;
42
39
  if (!url) {
43
40
  res.end(
@@ -59,7 +56,7 @@ export class LoopbackClient implements ILoopbackClient {
59
56
  const authCodeResponse =
60
57
  UrlString.getDeserializedQueryString(url);
61
58
  if (authCodeResponse.code) {
62
- const redirectUri = await this.getRedirectUri();
59
+ const redirectUri = this.getRedirectUri();
63
60
  res.writeHead(HttpStatus.REDIRECT, {
64
61
  location: redirectUri,
65
62
  }); // Prevent auth code from being saved in the browser history
@@ -71,28 +68,6 @@ export class LoopbackClient implements ILoopbackClient {
71
68
  this.server.listen(0); // Listen on any available port
72
69
  }
73
70
  );
74
-
75
- // Wait for server to be listening
76
- await new Promise<void>((resolve) => {
77
- let ticks = 0;
78
- const id = setInterval(() => {
79
- if (
80
- LOOPBACK_SERVER_CONSTANTS.TIMEOUT_MS /
81
- LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS <
82
- ticks
83
- ) {
84
- throw NodeAuthError.createLoopbackServerTimeoutError();
85
- }
86
-
87
- if (this.server.listening) {
88
- clearInterval(id);
89
- resolve();
90
- }
91
- ticks++;
92
- }, LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS);
93
- });
94
-
95
- return authCodeListener;
96
71
  }
97
72
 
98
73
  /**
@@ -100,7 +75,7 @@ export class LoopbackClient implements ILoopbackClient {
100
75
  * @returns
101
76
  */
102
77
  getRedirectUri(): string {
103
- if (!this.server) {
78
+ if (!this.server || !this.server.listening) {
104
79
  throw NodeAuthError.createNoLoopbackServerExistsError();
105
80
  }
106
81
 
@@ -119,8 +94,18 @@ export class LoopbackClient implements ILoopbackClient {
119
94
  * Close the loopback server
120
95
  */
121
96
  closeServer(): void {
122
- if (!!this.server) {
97
+ if (this.server) {
98
+ // Only stops accepting new connections, server will close once open/idle connections are closed.
123
99
  this.server.close();
100
+
101
+ if (typeof this.server.closeAllConnections === "function") {
102
+ /*
103
+ * Close open/idle connections. This API is available in Node versions 18.2 and higher
104
+ */
105
+ this.server.closeAllConnections();
106
+ }
107
+ this.server.unref();
108
+ this.server = undefined;
124
109
  }
125
110
  }
126
111
  }
@@ -1,3 +1,3 @@
1
1
  /* eslint-disable header/header */
2
2
  export const name = "@azure/msal-node";
3
- export const version = "2.1.0";
3
+ export const version = "2.2.0";