@azure/msal-node 2.0.2 → 2.2.0-alpha.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 (218) hide show
  1. package/README.md +3 -1
  2. package/dist/cache/ITokenCache.d.ts +15 -15
  3. package/dist/cache/NodeStorage.cjs +401 -401
  4. package/dist/cache/NodeStorage.cjs.map +1 -1
  5. package/dist/cache/NodeStorage.d.ts +182 -182
  6. package/dist/cache/NodeStorage.d.ts.map +1 -1
  7. package/dist/cache/NodeStorage.mjs +401 -401
  8. package/dist/cache/NodeStorage.mjs.map +1 -1
  9. package/dist/cache/TokenCache.cjs +265 -269
  10. package/dist/cache/TokenCache.cjs.map +1 -1
  11. package/dist/cache/TokenCache.d.ts +90 -90
  12. package/dist/cache/TokenCache.d.ts.map +1 -1
  13. package/dist/cache/TokenCache.mjs +266 -270
  14. package/dist/cache/TokenCache.mjs.map +1 -1
  15. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  16. package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
  17. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  18. package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
  19. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  20. package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
  21. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  22. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  23. package/dist/cache/serializer/Deserializer.cjs +173 -175
  24. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  25. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  26. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  27. package/dist/cache/serializer/Deserializer.mjs +174 -176
  28. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  29. package/dist/cache/serializer/Serializer.cjs +134 -134
  30. package/dist/cache/serializer/Serializer.cjs.map +1 -1
  31. package/dist/cache/serializer/Serializer.d.ts +39 -39
  32. package/dist/cache/serializer/Serializer.d.ts.map +1 -1
  33. package/dist/cache/serializer/Serializer.mjs +134 -134
  34. package/dist/cache/serializer/Serializer.mjs.map +1 -1
  35. package/dist/cache/serializer/SerializerTypes.d.ts +101 -101
  36. package/dist/client/ClientApplication.cjs +333 -333
  37. package/dist/client/ClientApplication.cjs.map +1 -1
  38. package/dist/client/ClientApplication.d.ts +142 -142
  39. package/dist/client/ClientApplication.d.ts.map +1 -1
  40. package/dist/client/ClientApplication.mjs +334 -334
  41. package/dist/client/ClientApplication.mjs.map +1 -1
  42. package/dist/client/ClientAssertion.cjs +116 -116
  43. package/dist/client/ClientAssertion.cjs.map +1 -1
  44. package/dist/client/ClientAssertion.d.ts +46 -46
  45. package/dist/client/ClientAssertion.d.ts.map +1 -1
  46. package/dist/client/ClientAssertion.mjs +117 -117
  47. package/dist/client/ClientAssertion.mjs.map +1 -1
  48. package/dist/client/ClientCredentialClient.cjs +189 -168
  49. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  50. package/dist/client/ClientCredentialClient.d.ts +33 -33
  51. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  52. package/dist/client/ClientCredentialClient.mjs +190 -169
  53. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  54. package/dist/client/ConfidentialClientApplication.cjs +163 -164
  55. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  56. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  57. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  58. package/dist/client/ConfidentialClientApplication.mjs +164 -165
  59. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  60. package/dist/client/DeviceCodeClient.cjs +207 -207
  61. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  62. package/dist/client/DeviceCodeClient.d.ts +53 -53
  63. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  64. package/dist/client/DeviceCodeClient.mjs +208 -208
  65. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  66. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  67. package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
  68. package/dist/client/IPublicClientApplication.d.ts +42 -42
  69. package/dist/client/IPublicClientApplication.d.ts.map +1 -1
  70. package/dist/client/OnBehalfOfClient.cjs +203 -204
  71. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  72. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  73. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  74. package/dist/client/OnBehalfOfClient.mjs +204 -205
  75. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  76. package/dist/client/PublicClientApplication.cjs +229 -175
  77. package/dist/client/PublicClientApplication.cjs.map +1 -1
  78. package/dist/client/PublicClientApplication.d.ts +71 -65
  79. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  80. package/dist/client/PublicClientApplication.mjs +232 -178
  81. package/dist/client/PublicClientApplication.mjs.map +1 -1
  82. package/dist/client/UsernamePasswordClient.cjs +95 -95
  83. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  84. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  85. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  86. package/dist/client/UsernamePasswordClient.mjs +95 -95
  87. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  88. package/dist/config/Configuration.cjs +72 -72
  89. package/dist/config/Configuration.cjs.map +1 -1
  90. package/dist/config/Configuration.d.ts +107 -107
  91. package/dist/config/Configuration.mjs +72 -72
  92. package/dist/config/Configuration.mjs.map +1 -1
  93. package/dist/crypto/CryptoProvider.cjs +75 -75
  94. package/dist/crypto/CryptoProvider.cjs.map +1 -1
  95. package/dist/crypto/CryptoProvider.d.ts +52 -52
  96. package/dist/crypto/CryptoProvider.mjs +75 -75
  97. package/dist/crypto/CryptoProvider.mjs.map +1 -1
  98. package/dist/crypto/GuidGenerator.cjs +22 -22
  99. package/dist/crypto/GuidGenerator.cjs.map +1 -1
  100. package/dist/crypto/GuidGenerator.d.ts +14 -14
  101. package/dist/crypto/GuidGenerator.mjs +22 -22
  102. package/dist/crypto/GuidGenerator.mjs.map +1 -1
  103. package/dist/crypto/HashUtils.cjs +13 -13
  104. package/dist/crypto/HashUtils.cjs.map +1 -1
  105. package/dist/crypto/HashUtils.d.ts +8 -8
  106. package/dist/crypto/HashUtils.mjs +13 -13
  107. package/dist/crypto/HashUtils.mjs.map +1 -1
  108. package/dist/crypto/PkceGenerator.cjs +49 -49
  109. package/dist/crypto/PkceGenerator.cjs.map +1 -1
  110. package/dist/crypto/PkceGenerator.d.ts +22 -22
  111. package/dist/crypto/PkceGenerator.mjs +49 -49
  112. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  113. package/dist/error/NodeAuthError.cjs +85 -85
  114. package/dist/error/NodeAuthError.cjs.map +1 -1
  115. package/dist/error/NodeAuthError.d.ts +65 -65
  116. package/dist/error/NodeAuthError.mjs +85 -85
  117. package/dist/error/NodeAuthError.mjs.map +1 -1
  118. package/dist/index.cjs +17 -1
  119. package/dist/index.cjs.map +1 -1
  120. package/dist/index.d.ts +42 -42
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.mjs +2 -2
  123. package/dist/internals.cjs +8 -8
  124. package/dist/internals.cjs.map +1 -1
  125. package/dist/internals.d.ts +6 -6
  126. package/dist/internals.d.ts.map +1 -1
  127. package/dist/internals.mjs +8 -8
  128. package/dist/internals.mjs.map +1 -1
  129. package/dist/network/HttpClient.cjs +277 -276
  130. package/dist/network/HttpClient.cjs.map +1 -1
  131. package/dist/network/HttpClient.d.ts +25 -25
  132. package/dist/network/HttpClient.d.ts.map +1 -1
  133. package/dist/network/HttpClient.mjs +278 -277
  134. package/dist/network/HttpClient.mjs.map +1 -1
  135. package/dist/network/ILoopbackClient.d.ts +10 -10
  136. package/dist/network/LoopbackClient.cjs +75 -84
  137. package/dist/network/LoopbackClient.cjs.map +1 -1
  138. package/dist/network/LoopbackClient.d.ts +21 -21
  139. package/dist/network/LoopbackClient.d.ts.map +1 -1
  140. package/dist/network/LoopbackClient.mjs +77 -86
  141. package/dist/network/LoopbackClient.mjs.map +1 -1
  142. package/dist/packageMetadata.cjs +4 -4
  143. package/dist/packageMetadata.cjs.map +1 -1
  144. package/dist/packageMetadata.d.ts +2 -2
  145. package/dist/packageMetadata.mjs +4 -4
  146. package/dist/packageMetadata.mjs.map +1 -1
  147. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  148. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  149. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  150. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  151. package/dist/request/InteractiveRequest.d.ts +22 -22
  152. package/dist/request/InteractiveRequest.d.ts.map +1 -1
  153. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  154. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  155. package/dist/request/SignOutRequest.d.ts +5 -5
  156. package/dist/request/SilentFlowRequest.d.ts +16 -16
  157. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  158. package/dist/utils/Constants.cjs +80 -90
  159. package/dist/utils/Constants.cjs.map +1 -1
  160. package/dist/utils/Constants.d.ts +86 -96
  161. package/dist/utils/Constants.d.ts.map +1 -1
  162. package/dist/utils/Constants.mjs +81 -90
  163. package/dist/utils/Constants.mjs.map +1 -1
  164. package/dist/utils/EncodingUtils.cjs +44 -44
  165. package/dist/utils/EncodingUtils.cjs.map +1 -1
  166. package/dist/utils/EncodingUtils.d.ts +26 -26
  167. package/dist/utils/EncodingUtils.mjs +44 -44
  168. package/dist/utils/EncodingUtils.mjs.map +1 -1
  169. package/dist/utils/NetworkUtils.cjs +38 -38
  170. package/dist/utils/NetworkUtils.cjs.map +1 -1
  171. package/dist/utils/NetworkUtils.d.ts +5 -5
  172. package/dist/utils/NetworkUtils.mjs +38 -38
  173. package/dist/utils/NetworkUtils.mjs.map +1 -1
  174. package/package.json +8 -5
  175. package/src/cache/ITokenCache.ts +24 -0
  176. package/src/cache/NodeStorage.ts +533 -0
  177. package/src/cache/TokenCache.ts +358 -0
  178. package/src/cache/distributed/DistributedCachePlugin.ts +56 -0
  179. package/src/cache/distributed/ICacheClient.ts +23 -0
  180. package/src/cache/distributed/IPartitionManager.ts +35 -0
  181. package/src/cache/serializer/Deserializer.ts +218 -0
  182. package/src/cache/serializer/Serializer.ts +173 -0
  183. package/src/cache/serializer/SerializerTypes.ts +121 -0
  184. package/src/client/ClientApplication.ts +596 -0
  185. package/src/client/ClientAssertion.ts +165 -0
  186. package/src/client/ClientCredentialClient.ts +346 -0
  187. package/src/client/ConfidentialClientApplication.ts +268 -0
  188. package/src/client/DeviceCodeClient.ts +369 -0
  189. package/src/client/IConfidentialClientApplication.ts +72 -0
  190. package/src/client/IPublicClientApplication.ts +72 -0
  191. package/src/client/OnBehalfOfClient.ts +368 -0
  192. package/src/client/PublicClientApplication.ts +339 -0
  193. package/src/client/UsernamePasswordClient.ts +180 -0
  194. package/src/config/Configuration.ts +201 -0
  195. package/src/crypto/CryptoProvider.ts +98 -0
  196. package/src/crypto/GuidGenerator.ts +28 -0
  197. package/src/crypto/HashUtils.ts +17 -0
  198. package/src/crypto/PkceGenerator.ts +63 -0
  199. package/src/error/NodeAuthError.ts +117 -0
  200. package/src/index.ts +125 -0
  201. package/src/internals.ts +12 -0
  202. package/src/network/HttpClient.ts +425 -0
  203. package/src/network/ILoopbackClient.ts +19 -0
  204. package/src/network/LoopbackClient.ts +110 -0
  205. package/src/packageMetadata.ts +3 -0
  206. package/src/request/AuthorizationCodeRequest.ts +39 -0
  207. package/src/request/AuthorizationUrlRequest.ts +50 -0
  208. package/src/request/ClientCredentialRequest.ts +29 -0
  209. package/src/request/DeviceCodeRequest.ts +34 -0
  210. package/src/request/InteractiveRequest.ts +39 -0
  211. package/src/request/OnBehalfOfRequest.ts +30 -0
  212. package/src/request/RefreshTokenRequest.ts +34 -0
  213. package/src/request/SignOutRequest.ts +11 -0
  214. package/src/request/SilentFlowRequest.ts +32 -0
  215. package/src/request/UsernamePasswordRequest.ts +36 -0
  216. package/src/utils/Constants.ts +103 -0
  217. package/src/utils/EncodingUtils.ts +50 -0
  218. package/src/utils/NetworkUtils.ts +49 -0
@@ -0,0 +1,201 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ LoggerOptions,
8
+ INetworkModule,
9
+ LogLevel,
10
+ ProtocolMode,
11
+ ICachePlugin,
12
+ Constants,
13
+ AzureCloudInstance,
14
+ AzureCloudOptions,
15
+ ApplicationTelemetry,
16
+ INativeBrokerPlugin,
17
+ } from "@azure/msal-common";
18
+ import { HttpClient } from "../network/HttpClient.js";
19
+ import http from "http";
20
+ import https from "https";
21
+
22
+ /**
23
+ * - clientId - Client id of the application.
24
+ * - authority - Url of the authority. If no value is set, defaults to https://login.microsoftonline.com/common.
25
+ * - knownAuthorities - Needed for Azure B2C and ADFS. All authorities that will be used in the client application. Only the host of the authority should be passed in.
26
+ * - clientSecret - Secret string that the application uses when requesting a token. Only used in confidential client applications. Can be created in the Azure app registration portal.
27
+ * - clientAssertion - Assertion string that the application uses when requesting a token. Only used in confidential client applications. Assertion should be of type urn:ietf:params:oauth:client-assertion-type:jwt-bearer.
28
+ * - clientCertificate - Certificate that the application uses when requesting a token. Only used in confidential client applications. Requires hex encoded X.509 SHA-1 thumbprint of the certificiate, and the PEM encoded private key (string should contain -----BEGIN PRIVATE KEY----- ... -----END PRIVATE KEY----- )
29
+ * - protocolMode - Enum that represents the protocol that msal follows. Used for configuring proper endpoints.
30
+ * - skipAuthorityMetadataCache - A flag to choose whether to use or not use the local metadata cache during authority initialization. Defaults to false.
31
+ * @public
32
+ */
33
+ export type NodeAuthOptions = {
34
+ clientId: string;
35
+ authority?: string;
36
+ clientSecret?: string;
37
+ clientAssertion?: string;
38
+ clientCertificate?: {
39
+ thumbprint: string;
40
+ privateKey: string;
41
+ x5c?: string;
42
+ };
43
+ knownAuthorities?: Array<string>;
44
+ cloudDiscoveryMetadata?: string;
45
+ authorityMetadata?: string;
46
+ clientCapabilities?: Array<string>;
47
+ protocolMode?: ProtocolMode;
48
+ azureCloudOptions?: AzureCloudOptions;
49
+ skipAuthorityMetadataCache?: boolean;
50
+ };
51
+
52
+ /**
53
+ * Use this to configure the below cache configuration options:
54
+ *
55
+ * - cachePlugin - Plugin for reading and writing token cache to disk.
56
+ * @public
57
+ */
58
+ export type CacheOptions = {
59
+ cachePlugin?: ICachePlugin;
60
+ claimsBasedCachingEnabled?: boolean;
61
+ };
62
+
63
+ /**
64
+ * Use this to configure the below broker options:
65
+ * - nativeBrokerPlugin - Native broker implementation (should be imported from msal-node-extensions)
66
+ *
67
+ * Note: These options are only available for PublicClientApplications using the Authorization Code Flow
68
+ * @public
69
+ */
70
+ export type BrokerOptions = {
71
+ nativeBrokerPlugin?: INativeBrokerPlugin;
72
+ };
73
+
74
+ /**
75
+ * Type for configuring logger and http client options
76
+ *
77
+ * - logger - Used to initialize the Logger object; TODO: Expand on logger details or link to the documentation on logger
78
+ * - networkClient - Http client used for all http get and post calls. Defaults to using MSAL's default http client.
79
+ * @public
80
+ */
81
+ export type NodeSystemOptions = {
82
+ loggerOptions?: LoggerOptions;
83
+ networkClient?: INetworkModule;
84
+ proxyUrl?: string;
85
+ customAgentOptions?: http.AgentOptions | https.AgentOptions;
86
+ };
87
+
88
+ export type NodeTelemetryOptions = {
89
+ application?: ApplicationTelemetry;
90
+ };
91
+
92
+ /**
93
+ * Use the configuration object to configure MSAL and initialize the client application object
94
+ *
95
+ * - auth: this is where you configure auth elements like clientID, authority used for authenticating against the Microsoft Identity Platform
96
+ * - broker: this is where you configure broker options
97
+ * - cache: this is where you configure cache location
98
+ * - system: this is where you can configure the network client, logger
99
+ * - telemetry: this is where you can configure telemetry options
100
+ * @public
101
+ */
102
+ export type Configuration = {
103
+ auth: NodeAuthOptions;
104
+ broker?: BrokerOptions;
105
+ cache?: CacheOptions;
106
+ system?: NodeSystemOptions;
107
+ telemetry?: NodeTelemetryOptions;
108
+ };
109
+
110
+ const DEFAULT_AUTH_OPTIONS: Required<NodeAuthOptions> = {
111
+ clientId: Constants.EMPTY_STRING,
112
+ authority: Constants.DEFAULT_AUTHORITY,
113
+ clientSecret: Constants.EMPTY_STRING,
114
+ clientAssertion: Constants.EMPTY_STRING,
115
+ clientCertificate: {
116
+ thumbprint: Constants.EMPTY_STRING,
117
+ privateKey: Constants.EMPTY_STRING,
118
+ x5c: Constants.EMPTY_STRING,
119
+ },
120
+ knownAuthorities: [],
121
+ cloudDiscoveryMetadata: Constants.EMPTY_STRING,
122
+ authorityMetadata: Constants.EMPTY_STRING,
123
+ clientCapabilities: [],
124
+ protocolMode: ProtocolMode.AAD,
125
+ azureCloudOptions: {
126
+ azureCloudInstance: AzureCloudInstance.None,
127
+ tenant: Constants.EMPTY_STRING,
128
+ },
129
+ skipAuthorityMetadataCache: false,
130
+ };
131
+
132
+ const DEFAULT_CACHE_OPTIONS: CacheOptions = {
133
+ claimsBasedCachingEnabled: false,
134
+ };
135
+
136
+ const DEFAULT_LOGGER_OPTIONS: LoggerOptions = {
137
+ loggerCallback: (): void => {
138
+ // allow users to not set logger call back
139
+ },
140
+ piiLoggingEnabled: false,
141
+ logLevel: LogLevel.Info,
142
+ };
143
+
144
+ const DEFAULT_SYSTEM_OPTIONS: Required<NodeSystemOptions> = {
145
+ loggerOptions: DEFAULT_LOGGER_OPTIONS,
146
+ networkClient: new HttpClient(),
147
+ proxyUrl: Constants.EMPTY_STRING,
148
+ customAgentOptions: {} as http.AgentOptions | https.AgentOptions,
149
+ };
150
+
151
+ const DEFAULT_TELEMETRY_OPTIONS: Required<NodeTelemetryOptions> = {
152
+ application: {
153
+ appName: Constants.EMPTY_STRING,
154
+ appVersion: Constants.EMPTY_STRING,
155
+ },
156
+ };
157
+
158
+ /** @internal */
159
+ export type NodeConfiguration = {
160
+ auth: Required<NodeAuthOptions>;
161
+ broker: BrokerOptions;
162
+ cache: CacheOptions;
163
+ system: Required<NodeSystemOptions>;
164
+ telemetry: Required<NodeTelemetryOptions>;
165
+ };
166
+
167
+ /**
168
+ * Sets the default options when not explicitly configured from app developer
169
+ *
170
+ * @param auth - Authentication options
171
+ * @param cache - Cache options
172
+ * @param system - System options
173
+ * @param telemetry - Telemetry options
174
+ *
175
+ * @returns Configuration
176
+ * @internal
177
+ */
178
+ export function buildAppConfiguration({
179
+ auth,
180
+ broker,
181
+ cache,
182
+ system,
183
+ telemetry,
184
+ }: Configuration): NodeConfiguration {
185
+ const systemOptions: Required<NodeSystemOptions> = {
186
+ ...DEFAULT_SYSTEM_OPTIONS,
187
+ networkClient: new HttpClient(
188
+ system?.proxyUrl,
189
+ system?.customAgentOptions as http.AgentOptions | https.AgentOptions
190
+ ),
191
+ loggerOptions: system?.loggerOptions || DEFAULT_LOGGER_OPTIONS,
192
+ };
193
+
194
+ return {
195
+ auth: { ...DEFAULT_AUTH_OPTIONS, ...auth },
196
+ broker: { ...broker },
197
+ cache: { ...DEFAULT_CACHE_OPTIONS, ...cache },
198
+ system: { ...systemOptions, ...system },
199
+ telemetry: { ...DEFAULT_TELEMETRY_OPTIONS, ...telemetry },
200
+ };
201
+ }
@@ -0,0 +1,98 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ICrypto, PkceCodes } from "@azure/msal-common";
7
+ import { GuidGenerator } from "./GuidGenerator.js";
8
+ import { EncodingUtils } from "../utils/EncodingUtils.js";
9
+ import { PkceGenerator } from "./PkceGenerator.js";
10
+ import { HashUtils } from "./HashUtils.js";
11
+
12
+ /**
13
+ * This class implements MSAL node's crypto interface, which allows it to perform base64 encoding and decoding, generating cryptographically random GUIDs and
14
+ * implementing Proof Key for Code Exchange specs for the OAuth Authorization Code Flow using PKCE (rfc here: https://tools.ietf.org/html/rfc7636).
15
+ * @public
16
+ */
17
+ export class CryptoProvider implements ICrypto {
18
+ private pkceGenerator: PkceGenerator;
19
+ private guidGenerator: GuidGenerator;
20
+ private hashUtils: HashUtils;
21
+
22
+ constructor() {
23
+ // Browser crypto needs to be validated first before any other classes can be set.
24
+ this.pkceGenerator = new PkceGenerator();
25
+ this.guidGenerator = new GuidGenerator();
26
+ this.hashUtils = new HashUtils();
27
+ }
28
+
29
+ /**
30
+ * Creates a new random GUID - used to populate state and nonce.
31
+ * @returns string (GUID)
32
+ */
33
+ createNewGuid(): string {
34
+ return this.guidGenerator.generateGuid();
35
+ }
36
+
37
+ /**
38
+ * Encodes input string to base64.
39
+ * @param input - string to be encoded
40
+ */
41
+ base64Encode(input: string): string {
42
+ return EncodingUtils.base64Encode(input);
43
+ }
44
+
45
+ /**
46
+ * Decodes input string from base64.
47
+ * @param input - string to be decoded
48
+ */
49
+ base64Decode(input: string): string {
50
+ return EncodingUtils.base64Decode(input);
51
+ }
52
+
53
+ /**
54
+ * Generates PKCE codes used in Authorization Code Flow.
55
+ */
56
+ generatePkceCodes(): Promise<PkceCodes> {
57
+ return this.pkceGenerator.generatePkceCodes();
58
+ }
59
+
60
+ /**
61
+ * Generates a keypair, stores it and returns a thumbprint - not yet implemented for node
62
+ */
63
+ getPublicKeyThumbprint(): Promise<string> {
64
+ throw new Error("Method not implemented.");
65
+ }
66
+
67
+ /**
68
+ * Removes cryptographic keypair from key store matching the keyId passed in
69
+ * @param kid
70
+ */
71
+ removeTokenBindingKey(): Promise<boolean> {
72
+ throw new Error("Method not implemented.");
73
+ }
74
+
75
+ /**
76
+ * Removes all cryptographic keys from Keystore
77
+ */
78
+ clearKeystore(): Promise<boolean> {
79
+ throw new Error("Method not implemented.");
80
+ }
81
+
82
+ /**
83
+ * Signs the given object as a jwt payload with private key retrieved by given kid - currently not implemented for node
84
+ */
85
+ signJwt(): Promise<string> {
86
+ throw new Error("Method not implemented.");
87
+ }
88
+
89
+ /**
90
+ * Returns the SHA-256 hash of an input string
91
+ */
92
+ async hashString(plainText: string): Promise<string> {
93
+ return EncodingUtils.base64EncodeUrl(
94
+ this.hashUtils.sha256(plainText).toString("base64"),
95
+ "base64"
96
+ );
97
+ }
98
+ }
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { IGuidGenerator } from "@azure/msal-common";
7
+ import { v4 as uuidv4 } from "uuid";
8
+
9
+ export class GuidGenerator implements IGuidGenerator {
10
+ /**
11
+ *
12
+ * RFC4122: The version 4 UUID is meant for generating UUIDs from truly-random or pseudo-random numbers.
13
+ * uuidv4 generates guids from cryprtographically-string random
14
+ */
15
+ generateGuid(): string {
16
+ return uuidv4();
17
+ }
18
+
19
+ /**
20
+ * verifies if a string is GUID
21
+ * @param guid
22
+ */
23
+ isGuid(guid: string): boolean {
24
+ const regexGuid =
25
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
26
+ return regexGuid.test(guid);
27
+ }
28
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { Hash } from "../utils/Constants.js";
7
+ import crypto from "crypto";
8
+
9
+ export class HashUtils {
10
+ /**
11
+ * generate 'SHA256' hash
12
+ * @param buffer
13
+ */
14
+ sha256(buffer: string): Buffer {
15
+ return crypto.createHash(Hash.SHA256).update(buffer).digest();
16
+ }
17
+ }
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { Constants, PkceCodes } from "@azure/msal-common";
7
+ import { CharSet, RANDOM_OCTET_SIZE } from "../utils/Constants.js";
8
+ import { EncodingUtils } from "../utils/EncodingUtils.js";
9
+ import { HashUtils } from "./HashUtils.js";
10
+ import crypto from "crypto";
11
+
12
+ /**
13
+ * https://tools.ietf.org/html/rfc7636#page-8
14
+ */
15
+ export class PkceGenerator {
16
+ private hashUtils: HashUtils;
17
+
18
+ constructor() {
19
+ this.hashUtils = new HashUtils();
20
+ }
21
+ /**
22
+ * generates the codeVerfier and the challenge from the codeVerfier
23
+ * reference: https://tools.ietf.org/html/rfc7636#section-4.1 and https://tools.ietf.org/html/rfc7636#section-4.2
24
+ */
25
+ async generatePkceCodes(): Promise<PkceCodes> {
26
+ const verifier = this.generateCodeVerifier();
27
+ const challenge = this.generateCodeChallengeFromVerifier(verifier);
28
+ return { verifier, challenge };
29
+ }
30
+
31
+ /**
32
+ * generates the codeVerfier; reference: https://tools.ietf.org/html/rfc7636#section-4.1
33
+ */
34
+ private generateCodeVerifier(): string {
35
+ const charArr = [];
36
+ const maxNumber = 256 - (256 % CharSet.CV_CHARSET.length);
37
+ while (charArr.length <= RANDOM_OCTET_SIZE) {
38
+ const byte = crypto.randomBytes(1)[0];
39
+ if (byte >= maxNumber) {
40
+ /*
41
+ * Ignore this number to maintain randomness.
42
+ * Including it would result in an unequal distribution of characters after doing the modulo
43
+ */
44
+ continue;
45
+ }
46
+ const index = byte % CharSet.CV_CHARSET.length;
47
+ charArr.push(CharSet.CV_CHARSET[index]);
48
+ }
49
+ const verifier: string = charArr.join(Constants.EMPTY_STRING);
50
+ return EncodingUtils.base64EncodeUrl(verifier);
51
+ }
52
+
53
+ /**
54
+ * generate the challenge from the codeVerfier; reference: https://tools.ietf.org/html/rfc7636#section-4.2
55
+ * @param codeVerifier
56
+ */
57
+ private generateCodeChallengeFromVerifier(codeVerifier: string): string {
58
+ return EncodingUtils.base64EncodeUrl(
59
+ this.hashUtils.sha256(codeVerifier).toString("base64"),
60
+ "base64"
61
+ );
62
+ }
63
+ }
@@ -0,0 +1,117 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AuthError } from "@azure/msal-common";
7
+
8
+ /**
9
+ * NodeAuthErrorMessage class containing string constants used by error codes and messages.
10
+ */
11
+ export const NodeAuthErrorMessage = {
12
+ invalidLoopbackAddressType: {
13
+ code: "invalid_loopback_server_address_type",
14
+ desc: "Loopback server address is not type string. This is unexpected.",
15
+ },
16
+ unableToLoadRedirectUri: {
17
+ code: "unable_to_load_redirectUrl",
18
+ desc: "Loopback server callback was invoked without a url. This is unexpected.",
19
+ },
20
+ noAuthCodeInResponse: {
21
+ code: "no_auth_code_in_response",
22
+ desc: "No auth code found in the server response. Please check your network trace to determine what happened.",
23
+ },
24
+ noLoopbackServerExists: {
25
+ code: "no_loopback_server_exists",
26
+ desc: "No loopback server exists yet.",
27
+ },
28
+ loopbackServerAlreadyExists: {
29
+ code: "loopback_server_already_exists",
30
+ desc: "Loopback server already exists. Cannot create another.",
31
+ },
32
+ loopbackServerTimeout: {
33
+ code: "loopback_server_timeout",
34
+ desc: "Timed out waiting for auth code listener to be registered.",
35
+ },
36
+ stateNotFoundError: {
37
+ code: "state_not_found",
38
+ desc: "State not found. Please verify that the request originated from msal.",
39
+ },
40
+ };
41
+
42
+ export class NodeAuthError extends AuthError {
43
+ constructor(errorCode: string, errorMessage?: string) {
44
+ super(errorCode, errorMessage);
45
+ this.name = "NodeAuthError";
46
+ }
47
+
48
+ /**
49
+ * Creates an error thrown if loopback server address is of type string.
50
+ */
51
+ static createInvalidLoopbackAddressTypeError(): NodeAuthError {
52
+ return new NodeAuthError(
53
+ NodeAuthErrorMessage.invalidLoopbackAddressType.code,
54
+ `${NodeAuthErrorMessage.invalidLoopbackAddressType.desc}`
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Creates an error thrown if the loopback server is unable to get a url.
60
+ */
61
+ static createUnableToLoadRedirectUrlError(): NodeAuthError {
62
+ return new NodeAuthError(
63
+ NodeAuthErrorMessage.unableToLoadRedirectUri.code,
64
+ `${NodeAuthErrorMessage.unableToLoadRedirectUri.desc}`
65
+ );
66
+ }
67
+
68
+ /**
69
+ * Creates an error thrown if the server response does not contain an auth code.
70
+ */
71
+ static createNoAuthCodeInResponseError(): NodeAuthError {
72
+ return new NodeAuthError(
73
+ NodeAuthErrorMessage.noAuthCodeInResponse.code,
74
+ `${NodeAuthErrorMessage.noAuthCodeInResponse.desc}`
75
+ );
76
+ }
77
+
78
+ /**
79
+ * Creates an error thrown if the loopback server has not been spun up yet.
80
+ */
81
+ static createNoLoopbackServerExistsError(): NodeAuthError {
82
+ return new NodeAuthError(
83
+ NodeAuthErrorMessage.noLoopbackServerExists.code,
84
+ `${NodeAuthErrorMessage.noLoopbackServerExists.desc}`
85
+ );
86
+ }
87
+
88
+ /**
89
+ * Creates an error thrown if a loopback server already exists when attempting to create another one.
90
+ */
91
+ static createLoopbackServerAlreadyExistsError(): NodeAuthError {
92
+ return new NodeAuthError(
93
+ NodeAuthErrorMessage.loopbackServerAlreadyExists.code,
94
+ `${NodeAuthErrorMessage.loopbackServerAlreadyExists.desc}`
95
+ );
96
+ }
97
+
98
+ /**
99
+ * Creates an error thrown if the loopback server times out registering the auth code listener.
100
+ */
101
+ static createLoopbackServerTimeoutError(): NodeAuthError {
102
+ return new NodeAuthError(
103
+ NodeAuthErrorMessage.loopbackServerTimeout.code,
104
+ `${NodeAuthErrorMessage.loopbackServerTimeout.desc}`
105
+ );
106
+ }
107
+
108
+ /**
109
+ * Creates an error thrown when the state is not present.
110
+ */
111
+ static createStateNotFoundError(): NodeAuthError {
112
+ return new NodeAuthError(
113
+ NodeAuthErrorMessage.stateNotFoundError.code,
114
+ NodeAuthErrorMessage.stateNotFoundError.desc
115
+ );
116
+ }
117
+ }
package/src/index.ts ADDED
@@ -0,0 +1,125 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * @packageDocumentation
8
+ * @module @azure/msal-node
9
+ */
10
+
11
+ /**
12
+ * Warning: This set of exports is purely intended to be used by other MSAL libraries, and should be considered potentially unstable. We strongly discourage using them directly, you do so at your own risk.
13
+ * Breaking changes to these APIs will be shipped under a minor version, instead of a major version.
14
+ */
15
+
16
+ import * as internals from "./internals.js";
17
+ export { internals };
18
+
19
+ // Interfaces
20
+ export { IPublicClientApplication } from "./client/IPublicClientApplication.js";
21
+ export { IConfidentialClientApplication } from "./client/IConfidentialClientApplication.js";
22
+ export { ITokenCache } from "./cache/ITokenCache.js";
23
+ export { ICacheClient } from "./cache/distributed/ICacheClient.js";
24
+ export { IPartitionManager } from "./cache/distributed/IPartitionManager.js";
25
+ export { ILoopbackClient } from "./network/ILoopbackClient.js";
26
+
27
+ // Clients and Configuration
28
+ export { PublicClientApplication } from "./client/PublicClientApplication.js";
29
+ export { ConfidentialClientApplication } from "./client/ConfidentialClientApplication.js";
30
+ export { ClientApplication } from "./client/ClientApplication.js";
31
+ export { ClientCredentialClient } from "./client/ClientCredentialClient.js";
32
+ export { DeviceCodeClient } from "./client/DeviceCodeClient.js";
33
+ export { OnBehalfOfClient } from "./client/OnBehalfOfClient.js";
34
+ export { UsernamePasswordClient } from "./client/UsernamePasswordClient.js";
35
+
36
+ export {
37
+ Configuration,
38
+ buildAppConfiguration,
39
+ NodeAuthOptions,
40
+ NodeSystemOptions,
41
+ BrokerOptions,
42
+ NodeTelemetryOptions,
43
+ CacheOptions,
44
+ } from "./config/Configuration.js";
45
+ export { ClientAssertion } from "./client/ClientAssertion.js";
46
+
47
+ // Cache and Storage
48
+ export { TokenCache } from "./cache/TokenCache.js";
49
+ export { NodeStorage } from "./cache/NodeStorage.js";
50
+ export {
51
+ CacheKVStore,
52
+ JsonCache,
53
+ InMemoryCache,
54
+ SerializedAccountEntity,
55
+ SerializedIdTokenEntity,
56
+ SerializedAccessTokenEntity,
57
+ SerializedAppMetadataEntity,
58
+ SerializedRefreshTokenEntity,
59
+ } from "./cache/serializer/SerializerTypes.js";
60
+ export { DistributedCachePlugin } from "./cache/distributed/DistributedCachePlugin.js";
61
+
62
+ // Crypto
63
+ export { CryptoProvider } from "./crypto/CryptoProvider.js";
64
+
65
+ // Request objects
66
+ export type { AuthorizationCodeRequest } from "./request/AuthorizationCodeRequest.js";
67
+ export type { AuthorizationUrlRequest } from "./request/AuthorizationUrlRequest.js";
68
+ export type { ClientCredentialRequest } from "./request/ClientCredentialRequest.js";
69
+ export type { DeviceCodeRequest } from "./request/DeviceCodeRequest.js";
70
+ export type { OnBehalfOfRequest } from "./request/OnBehalfOfRequest.js";
71
+ export type { UsernamePasswordRequest } from "./request/UsernamePasswordRequest.js";
72
+ export type { RefreshTokenRequest } from "./request/RefreshTokenRequest.js";
73
+ export type { SilentFlowRequest } from "./request/SilentFlowRequest.js";
74
+ export type { InteractiveRequest } from "./request/InteractiveRequest.js";
75
+ export type { SignOutRequest } from "./request/SignOutRequest.js";
76
+
77
+ // Common Object Formats
78
+ export {
79
+ // Request
80
+ PromptValue,
81
+ ResponseMode,
82
+ AuthorizationCodePayload,
83
+ // Response
84
+ AuthenticationResult,
85
+ ServerAuthorizationCodeResponse,
86
+ IdTokenClaims,
87
+ // Cache
88
+ AccountInfo,
89
+ ValidCacheType,
90
+ // Error
91
+ AuthError,
92
+ AuthErrorMessage,
93
+ AuthErrorCodes,
94
+ ClientAuthError,
95
+ ClientAuthErrorCodes,
96
+ ClientAuthErrorMessage,
97
+ ClientConfigurationError,
98
+ ClientConfigurationErrorCodes,
99
+ ClientConfigurationErrorMessage,
100
+ InteractionRequiredAuthError,
101
+ InteractionRequiredAuthErrorCodes,
102
+ InteractionRequiredAuthErrorMessage,
103
+ ServerError,
104
+ // Network Interface
105
+ INetworkModule,
106
+ NetworkRequestOptions,
107
+ NetworkResponse,
108
+ // Logger
109
+ Logger,
110
+ LogLevel,
111
+ // ProtocolMode enum
112
+ ProtocolMode,
113
+ ICachePlugin,
114
+ TokenCacheContext,
115
+ ISerializableTokenCache,
116
+ // AzureCloudInstance enum
117
+ AzureCloudInstance,
118
+ AzureCloudOptions,
119
+ // IAppTokenProvider
120
+ IAppTokenProvider,
121
+ AppTokenProviderParameters,
122
+ AppTokenProviderResult,
123
+ } from "@azure/msal-common";
124
+
125
+ export { version } from "./packageMetadata.js";
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Warning: This set of exports is purely intended to be used by other MSAL libraries, and should be considered potentially unstable. We strongly discourage using them directly, you do so at your own risk.
8
+ * Breaking changes to these APIs will be shipped under a minor version, instead of a major version.
9
+ */
10
+
11
+ export { Serializer } from "./cache/serializer/Serializer.js";
12
+ export { Deserializer } from "./cache/serializer/Deserializer.js";