@azure/msal-browser 3.6.0 → 3.7.1

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 (147) hide show
  1. package/dist/app/IPublicClientApplication.mjs +1 -1
  2. package/dist/app/PublicClientApplication.mjs +1 -1
  3. package/dist/app/PublicClientNext.mjs +1 -1
  4. package/dist/broker/nativeBroker/NativeMessageHandler.mjs +1 -1
  5. package/dist/broker/nativeBroker/NativeStatusCodes.mjs +1 -1
  6. package/dist/cache/AsyncMemoryStorage.mjs +1 -1
  7. package/dist/cache/BrowserCacheManager.d.ts.map +1 -1
  8. package/dist/cache/BrowserCacheManager.mjs +9 -9
  9. package/dist/cache/BrowserCacheManager.mjs.map +1 -1
  10. package/dist/cache/BrowserStorage.mjs +1 -1
  11. package/dist/cache/CryptoKeyStore.mjs +1 -1
  12. package/dist/cache/DatabaseStorage.mjs +1 -1
  13. package/dist/cache/MemoryStorage.mjs +1 -1
  14. package/dist/cache/TokenCache.d.ts.map +1 -1
  15. package/dist/cache/TokenCache.mjs +6 -3
  16. package/dist/cache/TokenCache.mjs.map +1 -1
  17. package/dist/config/Configuration.mjs +1 -1
  18. package/dist/controllers/ControllerFactory.mjs +1 -1
  19. package/dist/controllers/NestedAppAuthController.mjs +1 -1
  20. package/dist/controllers/StandardController.d.ts.map +1 -1
  21. package/dist/controllers/StandardController.mjs +40 -19
  22. package/dist/controllers/StandardController.mjs.map +1 -1
  23. package/dist/controllers/UnknownOperatingContextController.mjs +1 -1
  24. package/dist/crypto/BrowserCrypto.mjs +1 -1
  25. package/dist/crypto/CryptoOps.mjs +1 -1
  26. package/dist/crypto/PkceGenerator.mjs +1 -1
  27. package/dist/crypto/SignedHttpRequest.mjs +1 -1
  28. package/dist/encode/Base64Decode.mjs +1 -1
  29. package/dist/encode/Base64Encode.mjs +1 -1
  30. package/dist/error/BrowserAuthError.mjs +1 -1
  31. package/dist/error/BrowserAuthErrorCodes.mjs +1 -1
  32. package/dist/error/BrowserConfigurationAuthError.mjs +1 -1
  33. package/dist/error/BrowserConfigurationAuthErrorCodes.mjs +1 -1
  34. package/dist/error/NativeAuthError.mjs +1 -1
  35. package/dist/error/NativeAuthErrorCodes.mjs +1 -1
  36. package/dist/error/NestedAppAuthError.mjs +1 -1
  37. package/dist/event/EventHandler.mjs +1 -1
  38. package/dist/event/EventMessage.mjs +1 -1
  39. package/dist/event/EventType.mjs +1 -1
  40. package/dist/index.mjs +1 -1
  41. package/dist/interaction_client/BaseInteractionClient.d.ts.map +1 -1
  42. package/dist/interaction_client/BaseInteractionClient.mjs +4 -7
  43. package/dist/interaction_client/BaseInteractionClient.mjs.map +1 -1
  44. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.mjs +1 -1
  45. package/dist/interaction_client/NativeInteractionClient.d.ts.map +1 -1
  46. package/dist/interaction_client/NativeInteractionClient.mjs +4 -2
  47. package/dist/interaction_client/NativeInteractionClient.mjs.map +1 -1
  48. package/dist/interaction_client/PopupClient.mjs +1 -1
  49. package/dist/interaction_client/RedirectClient.mjs +1 -1
  50. package/dist/interaction_client/SilentAuthCodeClient.mjs +1 -1
  51. package/dist/interaction_client/SilentCacheClient.mjs +1 -1
  52. package/dist/interaction_client/SilentIframeClient.mjs +1 -1
  53. package/dist/interaction_client/SilentRefreshClient.d.ts.map +1 -1
  54. package/dist/interaction_client/SilentRefreshClient.mjs +5 -1
  55. package/dist/interaction_client/SilentRefreshClient.mjs.map +1 -1
  56. package/dist/interaction_client/StandardInteractionClient.mjs +2 -2
  57. package/dist/interaction_client/StandardInteractionClient.mjs.map +1 -1
  58. package/dist/interaction_handler/InteractionHandler.mjs +1 -1
  59. package/dist/interaction_handler/RedirectHandler.mjs +1 -1
  60. package/dist/interaction_handler/SilentHandler.mjs +1 -1
  61. package/dist/naa/AuthResult.d.ts +7 -0
  62. package/dist/naa/AuthResult.d.ts.map +1 -0
  63. package/dist/naa/BridgeError.mjs +1 -1
  64. package/dist/naa/BridgeProxy.d.ts +11 -15
  65. package/dist/naa/BridgeProxy.d.ts.map +1 -1
  66. package/dist/naa/BridgeProxy.mjs +33 -31
  67. package/dist/naa/BridgeProxy.mjs.map +1 -1
  68. package/dist/naa/BridgeRequest.d.ts +3 -2
  69. package/dist/naa/BridgeRequest.d.ts.map +1 -1
  70. package/dist/naa/BridgeRequestEnvelope.d.ts +2 -3
  71. package/dist/naa/BridgeRequestEnvelope.d.ts.map +1 -1
  72. package/dist/naa/BridgeResponseEnvelope.d.ts +5 -2
  73. package/dist/naa/BridgeResponseEnvelope.d.ts.map +1 -1
  74. package/dist/naa/BridgeStatusCode.d.ts +11 -10
  75. package/dist/naa/BridgeStatusCode.d.ts.map +1 -1
  76. package/dist/naa/BridgeStatusCode.mjs +11 -12
  77. package/dist/naa/BridgeStatusCode.mjs.map +1 -1
  78. package/dist/naa/IBridgeProxy.d.ts +3 -5
  79. package/dist/naa/IBridgeProxy.d.ts.map +1 -1
  80. package/dist/naa/{InitializeBridgeResponse.d.ts → InitContext.d.ts} +2 -2
  81. package/dist/naa/InitContext.d.ts.map +1 -0
  82. package/dist/naa/TokenRequest.d.ts +1 -2
  83. package/dist/naa/TokenRequest.d.ts.map +1 -1
  84. package/dist/naa/TokenResponse.d.ts +1 -3
  85. package/dist/naa/TokenResponse.d.ts.map +1 -1
  86. package/dist/naa/mapping/NestedAppAuthAdapter.d.ts +2 -2
  87. package/dist/naa/mapping/NestedAppAuthAdapter.d.ts.map +1 -1
  88. package/dist/naa/mapping/NestedAppAuthAdapter.mjs +18 -18
  89. package/dist/naa/mapping/NestedAppAuthAdapter.mjs.map +1 -1
  90. package/dist/navigation/NavigationClient.mjs +1 -1
  91. package/dist/network/FetchClient.mjs +1 -1
  92. package/dist/operatingcontext/BaseOperatingContext.d.ts +2 -1
  93. package/dist/operatingcontext/BaseOperatingContext.d.ts.map +1 -1
  94. package/dist/operatingcontext/BaseOperatingContext.mjs +54 -3
  95. package/dist/operatingcontext/BaseOperatingContext.mjs.map +1 -1
  96. package/dist/operatingcontext/StandardOperatingContext.mjs +1 -1
  97. package/dist/operatingcontext/TeamsAppOperatingContext.mjs +1 -1
  98. package/dist/operatingcontext/UnknownOperatingContext.mjs +1 -1
  99. package/dist/packageMetadata.d.ts +1 -1
  100. package/dist/packageMetadata.mjs +2 -2
  101. package/dist/request/SilentRequest.d.ts +1 -0
  102. package/dist/request/SilentRequest.d.ts.map +1 -1
  103. package/dist/response/ResponseHandler.mjs +1 -1
  104. package/dist/telemetry/BrowserPerformanceClient.d.ts +1 -3
  105. package/dist/telemetry/BrowserPerformanceClient.d.ts.map +1 -1
  106. package/dist/telemetry/BrowserPerformanceClient.mjs +63 -26
  107. package/dist/telemetry/BrowserPerformanceClient.mjs.map +1 -1
  108. package/dist/telemetry/BrowserPerformanceMeasurement.mjs +1 -1
  109. package/dist/utils/BrowserConstants.d.ts +4 -0
  110. package/dist/utils/BrowserConstants.d.ts.map +1 -1
  111. package/dist/utils/BrowserConstants.mjs +10 -2
  112. package/dist/utils/BrowserConstants.mjs.map +1 -1
  113. package/dist/utils/BrowserProtocolUtils.mjs +1 -1
  114. package/dist/utils/BrowserUtils.mjs +1 -1
  115. package/lib/msal-browser.cjs +3470 -3857
  116. package/lib/msal-browser.cjs.map +1 -1
  117. package/lib/msal-browser.js +3470 -3857
  118. package/lib/msal-browser.js.map +1 -1
  119. package/lib/msal-browser.min.js +66 -71
  120. package/package.json +2 -2
  121. package/src/cache/BrowserCacheManager.ts +7 -19
  122. package/src/cache/TokenCache.ts +6 -3
  123. package/src/controllers/StandardController.ts +58 -23
  124. package/src/interaction_client/BaseInteractionClient.ts +6 -18
  125. package/src/interaction_client/NativeInteractionClient.ts +2 -1
  126. package/src/interaction_client/SilentRefreshClient.ts +8 -0
  127. package/src/interaction_client/StandardInteractionClient.ts +2 -2
  128. package/src/naa/AuthResult.ts +12 -0
  129. package/src/naa/BridgeProxy.ts +59 -68
  130. package/src/naa/BridgeRequest.ts +6 -2
  131. package/src/naa/BridgeRequestEnvelope.ts +1 -13
  132. package/src/naa/BridgeResponseEnvelope.ts +5 -7
  133. package/src/naa/BridgeStatusCode.ts +12 -10
  134. package/src/naa/IBridgeProxy.ts +3 -14
  135. package/src/naa/{InitializeBridgeResponse.ts → InitContext.ts} +1 -1
  136. package/src/naa/TokenRequest.ts +1 -2
  137. package/src/naa/TokenResponse.ts +1 -4
  138. package/src/naa/mapping/NestedAppAuthAdapter.ts +19 -19
  139. package/src/operatingcontext/BaseOperatingContext.ts +64 -7
  140. package/src/packageMetadata.ts +1 -1
  141. package/src/request/SilentRequest.ts +1 -0
  142. package/src/telemetry/BrowserPerformanceClient.ts +95 -42
  143. package/src/utils/BrowserConstants.ts +11 -0
  144. package/dist/naa/AccountRequests.d.ts +0 -10
  145. package/dist/naa/AccountRequests.d.ts.map +0 -1
  146. package/dist/naa/InitializeBridgeResponse.d.ts.map +0 -1
  147. package/src/naa/AccountRequests.ts +0 -16
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "type": "git",
11
11
  "url": "https://github.com/AzureAD/microsoft-authentication-library-for-js.git"
12
12
  },
13
- "version": "3.6.0",
13
+ "version": "3.7.1",
14
14
  "description": "Microsoft Authentication Library for js",
15
15
  "keywords": [
16
16
  "implicit",
@@ -101,6 +101,6 @@
101
101
  "typescript": "^4.9.5"
102
102
  },
103
103
  "dependencies": {
104
- "@azure/msal-common": "14.5.0"
104
+ "@azure/msal-common": "14.6.1"
105
105
  }
106
106
  }
@@ -866,10 +866,7 @@ export class BrowserCacheManager extends CacheManager {
866
866
  const parsedMetadata = this.validateAndParseJson(value);
867
867
  if (
868
868
  !parsedMetadata ||
869
- !AppMetadataEntity.isAppMetadataEntity(
870
- appMetadataKey,
871
- parsedMetadata
872
- )
869
+ !CacheHelpers.isAppMetadataEntity(appMetadataKey, parsedMetadata)
873
870
  ) {
874
871
  this.logger.trace(
875
872
  "BrowserCacheManager.getAppMetadata: called, no cache hit"
@@ -878,7 +875,7 @@ export class BrowserCacheManager extends CacheManager {
878
875
  }
879
876
 
880
877
  this.logger.trace("BrowserCacheManager.getAppMetadata: cache hit");
881
- return CacheManager.toObject(new AppMetadataEntity(), parsedMetadata);
878
+ return parsedMetadata as AppMetadataEntity;
882
879
  }
883
880
 
884
881
  /**
@@ -887,7 +884,7 @@ export class BrowserCacheManager extends CacheManager {
887
884
  */
888
885
  setAppMetadata(appMetadata: AppMetadataEntity): void {
889
886
  this.logger.trace("BrowserCacheManager.setAppMetadata called");
890
- const appMetadataKey = appMetadata.generateAppMetadataKey();
887
+ const appMetadataKey = CacheHelpers.generateAppMetadataKey(appMetadata);
891
888
  this.setItem(appMetadataKey, JSON.stringify(appMetadata));
892
889
  }
893
890
 
@@ -950,18 +947,12 @@ export class BrowserCacheManager extends CacheManager {
950
947
  const parsedMetadata = this.validateAndParseJson(value);
951
948
  if (
952
949
  parsedMetadata &&
953
- AuthorityMetadataEntity.isAuthorityMetadataEntity(
954
- key,
955
- parsedMetadata
956
- )
950
+ CacheHelpers.isAuthorityMetadataEntity(key, parsedMetadata)
957
951
  ) {
958
952
  this.logger.trace(
959
953
  "BrowserCacheManager.getAuthorityMetadata: cache hit"
960
954
  );
961
- return CacheManager.toObject(
962
- new AuthorityMetadataEntity(),
963
- parsedMetadata
964
- );
955
+ return parsedMetadata as AuthorityMetadataEntity;
965
956
  }
966
957
  return null;
967
958
  }
@@ -1119,7 +1110,7 @@ export class BrowserCacheManager extends CacheManager {
1119
1110
  const parsedThrottlingCache = this.validateAndParseJson(value);
1120
1111
  if (
1121
1112
  !parsedThrottlingCache ||
1122
- !ThrottlingEntity.isThrottlingEntity(
1113
+ !CacheHelpers.isThrottlingEntity(
1123
1114
  throttlingCacheKey,
1124
1115
  parsedThrottlingCache
1125
1116
  )
@@ -1131,10 +1122,7 @@ export class BrowserCacheManager extends CacheManager {
1131
1122
  }
1132
1123
 
1133
1124
  this.logger.trace("BrowserCacheManager.getThrottlingCache: cache hit");
1134
- return CacheManager.toObject(
1135
- new ThrottlingEntity(),
1136
- parsedThrottlingCache
1137
- );
1125
+ return parsedThrottlingCache as ThrottlingEntity;
1138
1126
  }
1139
1127
 
1140
1128
  /**
@@ -31,6 +31,7 @@ import {
31
31
  } from "../error/BrowserAuthError";
32
32
  import { AuthenticationResult } from "../response/AuthenticationResult";
33
33
  import { base64Decode } from "../encode/Base64Decode";
34
+ import * as BrowserCrypto from "../crypto/BrowserCrypto";
34
35
 
35
36
  export type LoadTokenOptions = {
36
37
  clientInfo?: string;
@@ -142,7 +143,8 @@ export class TokenCache implements ITokenCache {
142
143
  this.config.system.networkClient,
143
144
  this.storage,
144
145
  authorityOptions,
145
- this.logger
146
+ this.logger,
147
+ request.correlationId || BrowserCrypto.createNewGuid()
146
148
  );
147
149
 
148
150
  // "clientInfo" from options takes precedence over "clientInfo" in response
@@ -270,9 +272,10 @@ export class TokenCache implements ITokenCache {
270
272
  idTokenClaims,
271
273
  base64Decode,
272
274
  clientInfo,
275
+ authority.hostnameAndPort,
273
276
  claimsTenantId,
274
- undefined,
275
- undefined,
277
+ undefined, // authCodePayload
278
+ undefined, // nativeAccountId
276
279
  this.logger
277
280
  );
278
281
 
@@ -43,6 +43,7 @@ import {
43
43
  CacheLookupPolicy,
44
44
  DEFAULT_REQUEST,
45
45
  BrowserConstants,
46
+ iFrameRenewalPolicies,
46
47
  } from "../utils/BrowserConstants";
47
48
  import * as BrowserUtils from "../utils/BrowserUtils";
48
49
  import { RedirectRequest } from "../request/RedirectRequest";
@@ -1949,7 +1950,10 @@ export class StandardController implements IController {
1949
1950
  throw error;
1950
1951
  });
1951
1952
  this.activeSilentTokenRequests.set(silentRequestKey, response);
1952
- return response;
1953
+ return {
1954
+ ...(await response),
1955
+ state: request.state,
1956
+ };
1953
1957
  } else {
1954
1958
  this.logger.verbose(
1955
1959
  "acquireTokenSilent has been called previously, returning the result from the first call",
@@ -1957,7 +1961,10 @@ export class StandardController implements IController {
1957
1961
  );
1958
1962
  // Discard measurements for memoized calls, as they are usually only a couple of ms and will artificially deflate metrics
1959
1963
  atsMeasurement.discard();
1960
- return cachedResponse;
1964
+ return {
1965
+ ...(await cachedResponse),
1966
+ state: request.state,
1967
+ };
1961
1968
  }
1962
1969
  }
1963
1970
 
@@ -2078,29 +2085,17 @@ export class StandardController implements IController {
2078
2085
  silentRequest.correlationId
2079
2086
  )(silentRequest, cacheLookupPolicy).catch(
2080
2087
  (refreshTokenError: AuthError) => {
2081
- const isSilentlyResolvable =
2082
- (!(
2083
- refreshTokenError instanceof
2084
- InteractionRequiredAuthError
2085
- ) &&
2086
- (refreshTokenError.errorCode ===
2087
- BrowserConstants.INVALID_GRANT_ERROR ||
2088
- refreshTokenError.errorCode ===
2089
- ClientAuthErrorCodes.tokenRefreshRequired)) ||
2090
- refreshTokenError.errorCode ===
2091
- InteractionRequiredAuthErrorCodes.noTokensFound;
2092
-
2093
- const tryIframeRenewal =
2094
- cacheLookupPolicy ===
2095
- CacheLookupPolicy.Default ||
2096
- cacheLookupPolicy === CacheLookupPolicy.Skip ||
2097
- cacheLookupPolicy ===
2098
- CacheLookupPolicy.RefreshTokenAndNetwork;
2099
-
2100
- if (isSilentlyResolvable && tryIframeRenewal) {
2088
+ const shouldTryToResolveSilently =
2089
+ checkIfRefreshTokenErrorCanBeResolvedSilently(
2090
+ refreshTokenError,
2091
+ silentRequest,
2092
+ cacheLookupPolicy
2093
+ );
2094
+
2095
+ if (shouldTryToResolveSilently) {
2101
2096
  this.logger.verbose(
2102
2097
  "Refresh token expired/invalid or CacheLookupPolicy is set to Skip, attempting acquire token by iframe.",
2103
- request.correlationId
2098
+ silentRequest.correlationId
2104
2099
  );
2105
2100
  return invokeAsync(
2106
2101
  this.acquireTokenBySilentIframe.bind(this),
@@ -2110,6 +2105,7 @@ export class StandardController implements IController {
2110
2105
  silentRequest.correlationId
2111
2106
  )(silentRequest);
2112
2107
  } else {
2108
+ // Error cannot be silently resolved or iframe renewal is not allowed, interaction required
2113
2109
  throw refreshTokenError;
2114
2110
  }
2115
2111
  }
@@ -2155,3 +2151,42 @@ export class StandardController implements IController {
2155
2151
  });
2156
2152
  }
2157
2153
  }
2154
+
2155
+ /**
2156
+ * Determines whether an error thrown by the refresh token endpoint can be resolved without interaction
2157
+ * @param refreshTokenError
2158
+ * @param silentRequest
2159
+ * @param cacheLookupPolicy
2160
+ * @returns
2161
+ */
2162
+ function checkIfRefreshTokenErrorCanBeResolvedSilently(
2163
+ refreshTokenError: AuthError,
2164
+ silentRequest: CommonSilentFlowRequest,
2165
+ cacheLookupPolicy: CacheLookupPolicy
2166
+ ): boolean {
2167
+ const noInteractionRequired = !(
2168
+ refreshTokenError instanceof InteractionRequiredAuthError &&
2169
+ // For refresh token errors, bad_token does not always require interaction (silently resolvable)
2170
+ refreshTokenError.subError !==
2171
+ InteractionRequiredAuthErrorCodes.badToken
2172
+ );
2173
+
2174
+ // Errors that result when the refresh token needs to be replaced
2175
+ const refreshTokenRefreshRequired =
2176
+ refreshTokenError.errorCode === BrowserConstants.INVALID_GRANT_ERROR ||
2177
+ refreshTokenError.errorCode ===
2178
+ ClientAuthErrorCodes.tokenRefreshRequired;
2179
+
2180
+ // Errors that may be resolved before falling back to interaction (through iframe renewal)
2181
+ const isSilentlyResolvable =
2182
+ (noInteractionRequired && refreshTokenRefreshRequired) ||
2183
+ refreshTokenError.errorCode ===
2184
+ InteractionRequiredAuthErrorCodes.noTokensFound ||
2185
+ refreshTokenError.errorCode ===
2186
+ InteractionRequiredAuthErrorCodes.refreshTokenExpired;
2187
+
2188
+ // Only these policies allow for an iframe renewal attempt
2189
+ const tryIframeRenewal = iFrameRenewalPolicies.includes(cacheLookupPolicy);
2190
+
2191
+ return isSilentlyResolvable && tryIframeRenewal;
2192
+ }
@@ -279,28 +279,16 @@ export abstract class BaseInteractionClient {
279
279
  authorityMetadata: this.config.auth.authorityMetadata,
280
280
  };
281
281
 
282
- if (requestAuthority) {
283
- this.logger.verbose(
284
- "Creating discovered authority with request authority"
285
- );
286
- return AuthorityFactory.createDiscoveredInstance(
287
- requestAuthority,
288
- this.config.system.networkClient,
289
- this.browserStorage,
290
- authorityOptions,
291
- this.logger
292
- );
293
- }
294
-
295
- this.logger.verbose(
296
- "Creating discovered authority with configured authority"
297
- );
282
+ const authority = requestAuthority || this.config.auth.authority;
283
+ this.logger.verbose(`Creating discovered authority with ${authority}`);
298
284
  return AuthorityFactory.createDiscoveredInstance(
299
- this.config.auth.authority,
285
+ authority,
300
286
  this.config.system.networkClient,
301
287
  this.browserStorage,
302
288
  authorityOptions,
303
- this.logger
289
+ this.logger,
290
+ this.correlationId,
291
+ this.performanceClient
304
292
  );
305
293
  }
306
294
  }
@@ -430,8 +430,9 @@ export class NativeInteractionClient extends BaseInteractionClient {
430
430
  idTokenClaims,
431
431
  base64Decode,
432
432
  response.client_info,
433
+ undefined, // environment
433
434
  idTokenClaims.tid,
434
- undefined,
435
+ undefined, // auth code payload
435
436
  response.account.id,
436
437
  this.logger
437
438
  );
@@ -44,6 +44,14 @@ export class SilentRefreshClient extends StandardInteractionClient {
44
44
  ...request,
45
45
  ...baseRequest,
46
46
  };
47
+
48
+ if (request.redirectUri) {
49
+ // Make sure any passed redirectUri is converted to an absolute URL - redirectUri is not a required parameter for refresh token redemption so only include if explicitly provided
50
+ silentRequest.redirectUri = this.getRedirectUri(
51
+ request.redirectUri
52
+ );
53
+ }
54
+
47
55
  const serverTelemetryManager = this.initializeServerTelemetryManager(
48
56
  ApiId.acquireTokenSilent_silentFlow
49
57
  );
@@ -331,8 +331,8 @@ export abstract class StandardInteractionClient extends BaseInteractionClient {
331
331
  this.browserStorage,
332
332
  authorityOptions,
333
333
  this.logger,
334
- this.performanceClient,
335
- this.correlationId
334
+ this.correlationId,
335
+ this.performanceClient
336
336
  );
337
337
  }
338
338
 
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AccountInfo } from "./AccountInfo";
7
+ import { TokenResponse } from "./TokenResponse";
8
+
9
+ export type AuthResult = {
10
+ token: TokenResponse;
11
+ account: AccountInfo;
12
+ };
@@ -4,20 +4,17 @@
4
4
  */
5
5
 
6
6
  import { AccountInfo } from "./AccountInfo";
7
- import {
8
- AccountByHomeIdRequest,
9
- AccountByLocalIdRequest,
10
- AccountByUsernameRequest,
11
- } from "./AccountRequests";
12
7
  import { AuthBridge, AuthBridgeResponse } from "./AuthBridge";
8
+ import { AuthResult } from "./AuthResult";
13
9
  import { BridgeCapabilities } from "./BridgeCapabilities";
10
+ import { BridgeError } from "./BridgeError";
14
11
  import { BridgeRequest } from "./BridgeRequest";
15
12
  import { BridgeRequestEnvelope, BridgeMethods } from "./BridgeRequestEnvelope";
16
13
  import { BridgeResponseEnvelope } from "./BridgeResponseEnvelope";
14
+ import { BridgeStatusCode } from "./BridgeStatusCode";
17
15
  import { IBridgeProxy } from "./IBridgeProxy";
18
- import { InitializeBridgeResponse } from "./InitializeBridgeResponse";
16
+ import { InitContext } from "./InitContext";
19
17
  import { TokenRequest } from "./TokenRequest";
20
- import { TokenResponse } from "./TokenResponse";
21
18
 
22
19
  declare global {
23
20
  interface Window {
@@ -31,8 +28,7 @@ declare global {
31
28
  * platform broker
32
29
  */
33
30
  export class BridgeProxy implements IBridgeProxy {
34
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- static bridgeRequests: any[] = [];
31
+ static bridgeRequests: BridgeRequest[] = [];
36
32
  static crypto: Crypto;
37
33
  sdkName: string;
38
34
  sdkVersion: string;
@@ -44,7 +40,7 @@ export class BridgeProxy implements IBridgeProxy {
44
40
  * @remarks This method will be called by the create factory method
45
41
  * @remarks If the bridge is not available, this method will throw an error
46
42
  */
47
- protected static async initializeNestedAppAuthBridge(): Promise<InitializeBridgeResponse> {
43
+ protected static async initializeNestedAppAuthBridge(): Promise<InitContext> {
48
44
  if (window === undefined) {
49
45
  throw new Error("window is undefined");
50
46
  }
@@ -75,22 +71,22 @@ export class BridgeProxy implements IBridgeProxy {
75
71
  1
76
72
  );
77
73
  if (responseEnvelope.success) {
78
- request.resolve(responseEnvelope.body);
74
+ request.resolve(responseEnvelope);
79
75
  } else {
80
- request.reject(responseEnvelope.body);
76
+ request.reject(responseEnvelope.error);
81
77
  }
82
78
  }
83
79
  }
84
80
  );
85
81
 
86
- const promise = new Promise<InitializeBridgeResponse>(
82
+ const bridgeResponse = await new Promise<BridgeResponseEnvelope>(
87
83
  (resolve, reject) => {
88
84
  const message: BridgeRequestEnvelope = {
89
85
  messageType: "NestedAppAuthRequest",
90
86
  method: "GetInitContext",
91
87
  requestId: BridgeProxy.getRandomId(),
92
88
  };
93
- const request: BridgeRequest<InitializeBridgeResponse> = {
89
+ const request: BridgeRequest = {
94
90
  requestId: message.requestId,
95
91
  method: message.method,
96
92
  resolve: resolve,
@@ -103,7 +99,9 @@ export class BridgeProxy implements IBridgeProxy {
103
99
  }
104
100
  );
105
101
 
106
- return await promise;
102
+ return BridgeProxy.validateBridgeResultOrThrow(
103
+ bridgeResponse.initContext
104
+ );
107
105
  } catch (error) {
108
106
  window.console.log(error);
109
107
  throw error;
@@ -117,51 +115,37 @@ export class BridgeProxy implements IBridgeProxy {
117
115
  /**
118
116
  * getTokenInteractive - Attempts to get a token interactively from the bridge
119
117
  * @param request A token request
120
- * @returns a promise that resolves to a token response or rejects with a BridgeError
118
+ * @returns a promise that resolves to an auth result or rejects with a BridgeError
121
119
  */
122
- public getTokenInteractive(request: TokenRequest): Promise<TokenResponse> {
123
- return this.sendRequest<TokenResponse>("GetTokenPopup", request);
120
+ public getTokenInteractive(request: TokenRequest): Promise<AuthResult> {
121
+ return this.getToken("GetTokenPopup", request);
124
122
  }
125
123
 
126
124
  /**
127
125
  * getTokenSilent Attempts to get a token silently from the bridge
128
126
  * @param request A token request
129
- * @returns a promise that resolves to a token response or rejects with a BridgeError
127
+ * @returns a promise that resolves to an auth result or rejects with a BridgeError
130
128
  */
131
- public getTokenSilent(request: TokenRequest): Promise<TokenResponse> {
132
- return this.sendRequest<TokenResponse>("GetToken", request);
129
+ public getTokenSilent(request: TokenRequest): Promise<AuthResult> {
130
+ return this.getToken("GetToken", request);
133
131
  }
134
132
 
135
- /**
136
- * getAccountInfo - Gets account information from the bridge
137
- *
138
- * @param request A request for account information
139
- */
140
- public getAccountInfo(
141
- request:
142
- | AccountByHomeIdRequest
143
- | AccountByLocalIdRequest
144
- | AccountByUsernameRequest
145
- ): Promise<AccountInfo> {
146
- let method: BridgeMethods = "GetAccountByHomeId";
147
-
148
- if ((request as AccountByHomeIdRequest).homeAccountId !== undefined) {
149
- method = "GetAccountByHomeId";
150
- }
151
-
152
- if ((request as AccountByLocalIdRequest).localAccountId !== undefined) {
153
- method = "GetAccountByLocalId";
154
- }
155
-
156
- if ((request as AccountByUsernameRequest).username !== undefined) {
157
- method = "GetAccountByUsername";
158
- }
159
-
160
- return this.sendRequest<AccountInfo>(method, request);
133
+ private async getToken(
134
+ requestType: BridgeMethods,
135
+ request: TokenRequest
136
+ ): Promise<AuthResult> {
137
+ const result = await this.sendRequest(requestType, {
138
+ tokenParams: request,
139
+ });
140
+ return {
141
+ token: BridgeProxy.validateBridgeResultOrThrow(result.token),
142
+ account: BridgeProxy.validateBridgeResultOrThrow(result.account),
143
+ };
161
144
  }
162
145
 
163
- public getActiveAccount(): Promise<AccountInfo> {
164
- return this.sendRequest<AccountInfo>("GetActiveAccount", undefined);
146
+ public async getActiveAccount(): Promise<AccountInfo> {
147
+ const result = await this.sendRequest("GetActiveAccount");
148
+ return BridgeProxy.validateBridgeResultOrThrow(result.account);
165
149
  }
166
150
 
167
151
  public getHostCapabilities(): BridgeCapabilities | null {
@@ -173,36 +157,43 @@ export class BridgeProxy implements IBridgeProxy {
173
157
  * @param request A token request
174
158
  * @returns a promise that resolves to a response of provided type or rejects with a BridgeError
175
159
  */
176
- private sendRequest<TResponse>(
160
+ private sendRequest(
177
161
  method: BridgeMethods,
178
- request:
179
- | TokenRequest
180
- | AccountByHomeIdRequest
181
- | AccountByLocalIdRequest
182
- | AccountByUsernameRequest
183
- | undefined
184
- ): Promise<TResponse> {
162
+ requestParams?: Partial<BridgeRequestEnvelope>
163
+ ): Promise<BridgeResponseEnvelope> {
185
164
  const message: BridgeRequestEnvelope = {
186
165
  messageType: "NestedAppAuthRequest",
187
166
  method: method,
188
167
  requestId: BridgeProxy.getRandomId(),
189
- body: request,
168
+ ...requestParams,
190
169
  };
191
170
 
192
- const promise = new Promise<TResponse>((resolve, reject) => {
193
- const request: BridgeRequest<TResponse> = {
194
- requestId: message.requestId,
195
- method: message.method,
196
- resolve: resolve,
197
- reject: reject,
198
- };
199
- BridgeProxy.bridgeRequests.push(request);
200
- window.nestedAppAuthBridge.postMessage(JSON.stringify(message));
201
- });
171
+ const promise = new Promise<BridgeResponseEnvelope>(
172
+ (resolve, reject) => {
173
+ const request: BridgeRequest = {
174
+ requestId: message.requestId,
175
+ method: message.method,
176
+ resolve: resolve,
177
+ reject: reject,
178
+ };
179
+ BridgeProxy.bridgeRequests.push(request);
180
+ window.nestedAppAuthBridge.postMessage(JSON.stringify(message));
181
+ }
182
+ );
202
183
 
203
184
  return promise;
204
185
  }
205
186
 
187
+ private static validateBridgeResultOrThrow<T>(input: T | undefined): T {
188
+ if (input === undefined) {
189
+ const bridgeError: BridgeError = {
190
+ status: BridgeStatusCode.NestedAppAuthUnavailable,
191
+ };
192
+ throw bridgeError;
193
+ }
194
+ return input;
195
+ }
196
+
206
197
  /**
207
198
  * Private constructor for BridgeProxy
208
199
  * @param sdkName The name of the SDK being used to make requests on behalf of the app
@@ -3,10 +3,14 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- export type BridgeRequest<TResponse> = {
6
+ import { BridgeResponseEnvelope } from "./BridgeResponseEnvelope";
7
+
8
+ export type BridgeRequest = {
7
9
  requestId: string;
8
10
  method: string;
9
- resolve: (value: TResponse | PromiseLike<TResponse>) => void;
11
+ resolve: (
12
+ value: BridgeResponseEnvelope | PromiseLike<BridgeResponseEnvelope>
13
+ ) => void;
10
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
15
  reject: (reason?: any) => void;
12
16
  };
@@ -4,19 +4,11 @@
4
4
  */
5
5
 
6
6
  import { TokenRequest } from "./TokenRequest";
7
- import {
8
- AccountByHomeIdRequest,
9
- AccountByLocalIdRequest,
10
- AccountByUsernameRequest,
11
- } from "./AccountRequests";
12
7
 
13
8
  export type BridgeMethods =
14
9
  | "GetToken"
15
10
  | "GetActiveAccount"
16
11
  | "GetAllAccounts"
17
- | "GetAccountByHomeId"
18
- | "GetAccountByLocalId"
19
- | "GetAccountByUsername"
20
12
  | "GetInitContext"
21
13
  | "GetTokenPopup";
22
14
 
@@ -27,11 +19,7 @@ export type BridgeRequestEnvelope = {
27
19
  clientLibrary?: string;
28
20
  clientLibraryVersion?: string;
29
21
  requestId: string;
30
- body?:
31
- | TokenRequest
32
- | AccountByHomeIdRequest
33
- | AccountByLocalIdRequest
34
- | AccountByUsernameRequest;
22
+ tokenParams?: TokenRequest;
35
23
  };
36
24
 
37
25
  export function isBridgeRequestEnvelope(
@@ -6,16 +6,14 @@
6
6
  import { BridgeError } from "./BridgeError";
7
7
  import { TokenResponse } from "./TokenResponse";
8
8
  import { AccountInfo } from "./AccountInfo";
9
- import { InitializeBridgeResponse } from "./InitializeBridgeResponse";
9
+ import { InitContext } from "./InitContext";
10
10
 
11
11
  export type BridgeResponseEnvelope = {
12
12
  messageType: "NestedAppAuthResponse";
13
13
  requestId: string;
14
14
  success: boolean; // false if body is error
15
- body:
16
- | TokenResponse
17
- | BridgeError
18
- | AccountInfo
19
- | AccountInfo[]
20
- | InitializeBridgeResponse;
15
+ token?: TokenResponse;
16
+ error?: BridgeError;
17
+ account?: AccountInfo;
18
+ initContext?: InitContext;
21
19
  };
@@ -3,13 +3,15 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- export enum BridgeStatusCode {
7
- USER_INTERACTION_REQUIRED = "USER_INTERACTION_REQUIRED",
8
- USER_CANCEL = "USER_CANCEL",
9
- NO_NETWORK = "NO_NETWORK",
10
- TRANSIENT_ERROR = "TRANSIENT_ERROR",
11
- PERSISTENT_ERROR = "PERSISTENT_ERROR",
12
- DISABLED = "DISABLED",
13
- ACCOUNT_UNAVAILABLE = "ACCOUNT_UNAVAILABLE",
14
- NESTED_APP_AUTH_UNAVAILABLE = "NESTED_APP_AUTH_UNAVAILABLE", // NAA is unavailable in the current context, can retry with standard browser based auth
15
- }
6
+ export const BridgeStatusCode = {
7
+ UserInteractionRequired: "USER_INTERACTION_REQUIRED",
8
+ UserCancel: "USER_CANCEL",
9
+ NoNetwork: "NO_NETWORK",
10
+ TransientError: "TRANSIENT_ERROR",
11
+ PersistentError: "PERSISTENT_ERROR",
12
+ Disabled: "DISABLED",
13
+ AccountUnavailable: "ACCOUNT_UNAVAILABLE",
14
+ NestedAppAuthUnavailable: "NESTED_APP_AUTH_UNAVAILABLE", // NAA is unavailable in the current context, can retry with standard browser based auth
15
+ } as const;
16
+ export type BridgeStatusCode =
17
+ (typeof BridgeStatusCode)[keyof typeof BridgeStatusCode];