@asgardeo/javascript 0.8.3 → 0.9.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.
@@ -28,7 +28,7 @@ declare class StorageManager<T> {
28
28
  protected setDataInBulk(key: string, data: PartialData<T>): Promise<void>;
29
29
  protected setValue(key: string, attribute: keyof AuthClientConfig<T> | keyof OIDCDiscoveryApiResponse | keyof SessionData | keyof TemporaryStore, value: TemporaryStoreValue): Promise<void>;
30
30
  protected removeValue(key: string, attribute: keyof AuthClientConfig<T> | keyof OIDCDiscoveryApiResponse | keyof SessionData | keyof TemporaryStore): Promise<void>;
31
- protected resolveKey(store: Stores | string, userId?: string): string;
31
+ protected resolveKey(store: Stores | string, userId?: string, instanceId?: string): string;
32
32
  protected static isLocalStorageAvailable(): boolean;
33
33
  setConfigData(config: Partial<AuthClientConfig<T>>): Promise<void>;
34
34
  setOIDCProviderMetaData(oidcProviderMetaData: Partial<OIDCDiscoveryApiResponse>): Promise<void>;
@@ -38,7 +38,7 @@ declare class StorageManager<T> {
38
38
  getConfigData(userId?: string): Promise<AuthClientConfig<T>>;
39
39
  loadOpenIDProviderConfiguration(): Promise<OIDCDiscoveryApiResponse>;
40
40
  getTemporaryData(userId?: string): Promise<TemporaryStore>;
41
- getSessionData(userId?: string): Promise<SessionData>;
41
+ getSessionData(userId?: string, instanceId?: string): Promise<SessionData>;
42
42
  getCustomData<K>(key: string, userId?: string): Promise<K>;
43
43
  setSessionStatus(status: string): void;
44
44
  getSessionStatus(): string;
@@ -24,6 +24,18 @@ export interface DefaultAuthClientConfig {
24
24
  clientId?: string;
25
25
  clientSecret?: string;
26
26
  enablePKCE?: boolean;
27
+ organizationChain?: {
28
+ /**
29
+ * Instance ID of the source organization context to retrieve access token from for organization token exchange.
30
+ * Used in linked organization scenarios to automatically fetch the source organization's access token.
31
+ */
32
+ sourceInstanceId?: number;
33
+ /**
34
+ * Organization ID for the target organization.
35
+ * When provided with sourceInstanceId, triggers automatic organization token exchange.
36
+ */
37
+ targetOrganizationId?: string;
38
+ };
27
39
  prompt?: string;
28
40
  responseMode?: OAuthResponseMode;
29
41
  scopes?: string | string[] | undefined;
package/dist/cjs/index.js CHANGED
@@ -657,7 +657,27 @@ var AuthenticationHelper = class {
657
657
  }
658
658
  async replaceCustomGrantTemplateTags(text, userId) {
659
659
  const configData = await this.config();
660
- const sessionData = await this.storageManager.getSessionData(userId);
660
+ const sourceInstanceId = configData.organizationChain?.sourceInstanceId ?? null;
661
+ let sessionData;
662
+ if (sourceInstanceId) {
663
+ const { clientId } = configData;
664
+ let instanceKey;
665
+ if (clientId) {
666
+ instanceKey = `instance_${sourceInstanceId}-${clientId}`;
667
+ } else {
668
+ instanceKey = `instance_${sourceInstanceId}`;
669
+ }
670
+ sessionData = await this.storageManager.getSessionData(userId, instanceKey);
671
+ if (!sessionData || !sessionData.access_token) {
672
+ throw new AsgardeoAuthException(
673
+ "JS-AUTH_HELPER-RCGTT-NE01",
674
+ "No session data found for source instance.",
675
+ "Failed to retrieve session data from the source organization context."
676
+ );
677
+ }
678
+ } else {
679
+ sessionData = await this.storageManager.getSessionData(userId);
680
+ }
661
681
  const scope = processOpenIDScopes_default(configData.scopes);
662
682
  if (typeof text !== "string") {
663
683
  return text;
@@ -909,8 +929,17 @@ var StorageManager = class _StorageManager {
909
929
  const dataToBeSavedJSON = JSON.stringify(dataToBeSaved);
910
930
  await this.store.setData(key, dataToBeSavedJSON);
911
931
  }
912
- resolveKey(store, userId) {
913
- return userId ? `${store}-${this.id}-${userId}` : `${store}-${this.id}`;
932
+ resolveKey(store, userId, instanceId) {
933
+ if (userId && instanceId) {
934
+ return `${store}-${instanceId}-${userId}`;
935
+ }
936
+ if (userId) {
937
+ return `${store}-${this.id}-${userId}`;
938
+ }
939
+ if (instanceId) {
940
+ return `${store}-${instanceId}`;
941
+ }
942
+ return `${store}-${this.id}`;
914
943
  }
915
944
  static isLocalStorageAvailable() {
916
945
  try {
@@ -946,8 +975,8 @@ var StorageManager = class _StorageManager {
946
975
  async getTemporaryData(userId) {
947
976
  return JSON.parse(await this.store.getData(this.resolveKey("temporary_data" /* TemporaryData */, userId)) ?? null);
948
977
  }
949
- async getSessionData(userId) {
950
- return JSON.parse(await this.store.getData(this.resolveKey("session_data" /* SessionData */, userId)) ?? null);
978
+ async getSessionData(userId, instanceId) {
979
+ return JSON.parse(await this.store.getData(this.resolveKey("session_data" /* SessionData */, userId, instanceId)) ?? null);
951
980
  }
952
981
  async getCustomData(key, userId) {
953
982
  return JSON.parse(await this.store.getData(this.resolveKey(key, userId)) ?? null);
@@ -1346,77 +1375,74 @@ var _AsgardeoAuthClient = class _AsgardeoAuthClient {
1346
1375
  * @preserve
1347
1376
  */
1348
1377
  async requestAccessToken(authorizationCode, sessionState, state, userId, tokenRequestConfig) {
1349
- const performTokenRequest = async () => {
1350
- const tokenEndpoint = (await this.oidcProviderMetaDataProvider()).token_endpoint;
1351
- const configData = await this.configProvider();
1352
- if (!tokenEndpoint || tokenEndpoint.trim().length === 0) {
1353
- throw new AsgardeoAuthException(
1354
- "JS-AUTH_CORE-RAT1-NF01",
1355
- "Token endpoint not found.",
1356
- "No token endpoint was found in the OIDC provider meta data returned by the well-known endpoint or the token endpoint passed to the SDK is empty."
1357
- );
1358
- }
1359
- if (sessionState) {
1360
- await this.storageManager.setSessionDataParameter(
1361
- OIDCRequestConstants_default.Params.SESSION_STATE,
1362
- sessionState,
1363
- userId
1364
- );
1365
- }
1366
- const body = new URLSearchParams();
1367
- body.set("client_id", configData.clientId);
1368
- if (configData.clientSecret && configData.clientSecret.trim().length > 0) {
1369
- body.set("client_secret", configData.clientSecret);
1370
- }
1371
- const code = authorizationCode;
1372
- body.set("code", code);
1373
- body.set("grant_type", "authorization_code");
1374
- body.set("redirect_uri", configData.afterSignInUrl);
1375
- if (tokenRequestConfig?.params) {
1376
- Object.entries(tokenRequestConfig.params).forEach(([key, value]) => {
1377
- body.append(key, value);
1378
- });
1379
- }
1380
- if (configData.enablePKCE) {
1381
- body.set(
1382
- "code_verifier",
1383
- `${await this.storageManager.getTemporaryDataParameter(extractPkceStorageKeyFromState_default(state), userId)}`
1384
- );
1385
- await this.storageManager.removeTemporaryDataParameter(extractPkceStorageKeyFromState_default(state), userId);
1386
- }
1387
- let tokenResponse;
1388
- try {
1389
- tokenResponse = await fetch(tokenEndpoint, {
1390
- body,
1391
- credentials: configData.sendCookiesInRequests ? "include" : "same-origin",
1392
- headers: {
1393
- Accept: "application/json",
1394
- "Content-Type": "application/x-www-form-urlencoded"
1395
- },
1396
- method: "POST"
1397
- });
1398
- } catch (error2) {
1399
- throw new AsgardeoAuthException(
1400
- "JS-AUTH_CORE-RAT1-NE02",
1401
- "Requesting access token failed",
1402
- error2 ?? "The request to get the access token from the server failed."
1403
- );
1404
- }
1405
- if (!tokenResponse.ok) {
1406
- throw new AsgardeoAuthException(
1407
- "JS-AUTH_CORE-RAT1-HE03",
1408
- `Requesting access token failed with ${tokenResponse.statusText}`,
1409
- await tokenResponse.json()
1410
- );
1411
- }
1412
- return this.authHelper.handleTokenResponse(tokenResponse, userId);
1413
- };
1414
- if (await this.storageManager.getTemporaryDataParameter(
1378
+ if (!await this.storageManager.getTemporaryDataParameter(
1415
1379
  OIDCDiscoveryConstants_default.Storage.StorageKeys.OPENID_PROVIDER_CONFIG_INITIATED
1416
1380
  )) {
1417
- return performTokenRequest();
1381
+ await this.loadOpenIDProviderConfiguration(false);
1382
+ }
1383
+ const tokenEndpoint = (await this.oidcProviderMetaDataProvider()).token_endpoint;
1384
+ const configData = await this.configProvider();
1385
+ if (!tokenEndpoint || tokenEndpoint.trim().length === 0) {
1386
+ throw new AsgardeoAuthException(
1387
+ "JS-AUTH_CORE-RAT1-NF01",
1388
+ "Token endpoint not found.",
1389
+ "No token endpoint was found in the OIDC provider meta data returned by the well-known endpoint or the token endpoint passed to the SDK is empty."
1390
+ );
1391
+ }
1392
+ if (sessionState) {
1393
+ await this.storageManager.setSessionDataParameter(
1394
+ OIDCRequestConstants_default.Params.SESSION_STATE,
1395
+ sessionState,
1396
+ userId
1397
+ );
1398
+ }
1399
+ const body = new URLSearchParams();
1400
+ body.set("client_id", configData.clientId);
1401
+ if (configData.clientSecret && configData.clientSecret.trim().length > 0) {
1402
+ body.set("client_secret", configData.clientSecret);
1403
+ }
1404
+ const code = authorizationCode;
1405
+ body.set("code", code);
1406
+ body.set("grant_type", "authorization_code");
1407
+ body.set("redirect_uri", configData.afterSignInUrl);
1408
+ if (tokenRequestConfig?.params) {
1409
+ Object.entries(tokenRequestConfig.params).forEach(([key, value]) => {
1410
+ body.append(key, value);
1411
+ });
1412
+ }
1413
+ if (configData.enablePKCE) {
1414
+ body.set(
1415
+ "code_verifier",
1416
+ `${await this.storageManager.getTemporaryDataParameter(extractPkceStorageKeyFromState_default(state), userId)}`
1417
+ );
1418
+ await this.storageManager.removeTemporaryDataParameter(extractPkceStorageKeyFromState_default(state), userId);
1418
1419
  }
1419
- return this.loadOpenIDProviderConfiguration(false).then(() => performTokenRequest());
1420
+ let tokenResponse;
1421
+ try {
1422
+ tokenResponse = await fetch(tokenEndpoint, {
1423
+ body,
1424
+ credentials: configData.sendCookiesInRequests ? "include" : "same-origin",
1425
+ headers: {
1426
+ Accept: "application/json",
1427
+ "Content-Type": "application/x-www-form-urlencoded"
1428
+ },
1429
+ method: "POST"
1430
+ });
1431
+ } catch (error2) {
1432
+ throw new AsgardeoAuthException(
1433
+ "JS-AUTH_CORE-RAT1-NE02",
1434
+ "Requesting access token failed",
1435
+ error2 ?? "The request to get the access token from the server failed."
1436
+ );
1437
+ }
1438
+ if (!tokenResponse.ok) {
1439
+ throw new AsgardeoAuthException(
1440
+ "JS-AUTH_CORE-RAT1-HE03",
1441
+ `Requesting access token failed with ${tokenResponse.statusText}`,
1442
+ await tokenResponse.json()
1443
+ );
1444
+ }
1445
+ return this.authHelper.handleTokenResponse(tokenResponse, userId);
1420
1446
  }
1421
1447
  async loadOpenIDProviderConfiguration(forceInit) {
1422
1448
  const configData = await this.configProvider();
@@ -1862,6 +1888,11 @@ var _AsgardeoAuthClient = class _AsgardeoAuthClient {
1862
1888
  * @preserve
1863
1889
  */
1864
1890
  async exchangeToken(config, userId) {
1891
+ if (!await this.storageManager.getTemporaryDataParameter(
1892
+ OIDCDiscoveryConstants_default.Storage.StorageKeys.OPENID_PROVIDER_CONFIG_INITIATED
1893
+ )) {
1894
+ await this.loadOpenIDProviderConfiguration(false);
1895
+ }
1865
1896
  const oidcProviderMetadata = await this.oidcProviderMetaDataProvider();
1866
1897
  const configData = await this.configProvider();
1867
1898
  let tokenEndpoint;