Skip to content
7 changes: 7 additions & 0 deletions .changeset/bumpy-maps-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@asgardeo/javascript': minor
'@asgardeo/browser': minor
'@asgardeo/react': minor
---

Adds multi-organization authentication context management support
12 changes: 5 additions & 7 deletions packages/browser/src/__legacy__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export class AsgardeoSPAClient {
protected _onEndUserSession: (response: any) => void = () => null;
protected _onInitialize: (response: boolean) => void = () => null;
protected _onCustomGrant: Map<string, (response: any) => void> = new Map();
protected _instanceID: number;
protected _instanceId: number;

protected constructor(id: number) {
this._instanceID = id;
this._instanceId = id;
}

public instantiateAuthHelper(authHelper?: typeof AuthenticationHelper): void {
Expand Down Expand Up @@ -309,7 +309,7 @@ export class AsgardeoSPAClient {
* @preserve
*/
public getInstanceId(): number {
return this._instanceID;
return this._instanceId;
}

/**
Expand Down Expand Up @@ -364,7 +364,7 @@ export class AsgardeoSPAClient {
// Tracker: https://github.com/asgardeo/asgardeo-auth-react-sdk/issues/240
if (!this._client || (this._client && (!_config || Object.keys(_config)?.length === 0))) {
this._client = await MainThreadClient(
this._instanceID,
this._instanceId,
mergedConfig,
(authClient: AsgardeoAuthClient<MainThreadClientConfig>, spaHelper: SPAHelper<MainThreadClientConfig>) => {
return new this._authHelper(authClient, spaHelper);
Expand Down Expand Up @@ -399,7 +399,7 @@ export class AsgardeoSPAClient {
if (!this._client || (this._client && (!_config || Object.keys(_config)?.length === 0))) {
const webWorkerClientConfig = config as AuthClientConfig<WebWorkerClientConfig>;
this._client = (await WebWorkerClient(
this._instanceID,
this._instanceId,
{
...DefaultConfig,
...webWorkerClientConfig,
Expand Down Expand Up @@ -718,8 +718,6 @@ export class AsgardeoSPAClient {
public async exchangeToken(config: TokenExchangeRequestConfig): Promise<Response | User | undefined> {
if (config.signInRequired) {
await this._validateMethod();
} else {
await this._validateMethod();
}

if (!config.id) {
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/src/__legacy__/clients/main-thread-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const initiateStore = (store: BrowserStorage | undefined): Storage => {
};

export const MainThreadClient = async (
instanceID: number,
instanceId: number,
config: AuthClientConfig<MainThreadClientConfig>,
getAuthHelper: (
authClient: AsgardeoAuthClient<MainThreadClientConfig>,
Expand All @@ -67,7 +67,7 @@ export const MainThreadClient = async (
const _store: Storage = initiateStore(config.storage as BrowserStorage);
const _cryptoUtils: SPACryptoUtils = new SPACryptoUtils();
const _authenticationClient = new AsgardeoAuthClient<MainThreadClientConfig>();
await _authenticationClient.initialize(config, _store, _cryptoUtils, instanceID);
await _authenticationClient.initialize(config, _store, _cryptoUtils, instanceId);

const _spaHelper = new SPAHelper<MainThreadClientConfig>(_authenticationClient);
const _dataLayer = _authenticationClient.getStorageManager();
Expand All @@ -85,7 +85,7 @@ export const MainThreadClient = async (

let _getSignOutURLFromSessionStorage: boolean = false;

const _httpClient: HttpClientInstance = HttpClient.getInstance(instanceID);
const _httpClient: HttpClientInstance = HttpClient.getInstance(instanceId);
let _isHttpHandlerEnabled: boolean = true;
let _httpErrorCallback: (error: HttpError) => void | Promise<void>;
let _httpFinishCallback: () => void;
Expand Down Expand Up @@ -261,7 +261,7 @@ export const MainThreadClient = async (
if ((await _authenticationClient.isSignedIn()) && !_getSignOutURLFromSessionStorage) {
location.href = await _authenticationClient.getSignOutUrl();
} else {
location.href = SPAUtils.getSignOutUrl(config.clientId, instanceID);
location.href = SPAUtils.getSignOutUrl(config.clientId, instanceId);
}

_spaHelper.clearRefreshTokenTimeout();
Expand Down
15 changes: 8 additions & 7 deletions packages/browser/src/__legacy__/clients/web-worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const initiateStore = (store: BrowserStorage | undefined): Storage => {
};

export const WebWorkerClient = async (
instanceID: number,
instanceId: number,
config: AuthClientConfig<WebWorkerClientConfig>,
webWorker: new () => Worker,
getAuthHelper: (
Expand All @@ -114,7 +114,7 @@ export const WebWorkerClient = async (
const _store: Storage = initiateStore(config.storage as BrowserStorage);
const _cryptoUtils: SPACryptoUtils = new SPACryptoUtils();
const _authenticationClient = new AsgardeoAuthClient<WebWorkerClientConfig>();
await _authenticationClient.initialize(config, _store, _cryptoUtils, instanceID);
await _authenticationClient.initialize(config, _store, _cryptoUtils, instanceId);
const _spaHelper = new SPAHelper<WebWorkerClientConfig>(_authenticationClient);

const _sessionManagementHelper = await SessionManagementHelper(
Expand All @@ -128,7 +128,7 @@ export const WebWorkerClient = async (

return signOutURL;
} catch {
return SPAUtils.getSignOutUrl(config.clientId, instanceID);
return SPAUtils.getSignOutUrl(config.clientId, instanceId);
}
},
config.storage as BrowserStorage,
Expand Down Expand Up @@ -365,8 +365,9 @@ export const WebWorkerClient = async (
}
};

const message: Message<AuthClientConfig<WebWorkerClientConfig> & {instanceID: number}> = {
data: {...config, instanceID},
const message: Message<AuthClientConfig<WebWorkerClientConfig>> = {
data: config,
instanceId,
type: INIT,
};

Expand Down Expand Up @@ -524,7 +525,7 @@ export const WebWorkerClient = async (

return communicate<null, string>(message)
.then((url: string) => {
SPAUtils.setSignOutURL(url, config.clientId, instanceID);
SPAUtils.setSignOutURL(url, config.clientId, instanceId);

// Enable OIDC Sessions Management only if it is set to true in the config.
if (config.syncSession) {
Expand Down Expand Up @@ -656,7 +657,7 @@ export const WebWorkerClient = async (
return reject(error);
});
} else {
window.location.href = SPAUtils.getSignOutUrl(config.clientId, instanceID);
window.location.href = SPAUtils.getSignOutUrl(config.clientId, instanceId);

return SPAUtils.waitTillPageRedirect().then(() => {
return Promise.resolve(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
protected _authenticationClient: AsgardeoAuthClient<T>;
protected _storageManager: StorageManager<T>;
protected _spaHelper: SPAHelper<T>;
protected _instanceID: number;
protected _instanceId: number;
protected _isTokenRefreshing: boolean;

public constructor(authClient: AsgardeoAuthClient<T>, spaHelper: SPAHelper<T>) {
this._authenticationClient = authClient;
this._storageManager = this._authenticationClient.getStorageManager();
this._spaHelper = spaHelper;
this._instanceID = this._authenticationClient.getInstanceId();
this._instanceId = this._authenticationClient.getInstanceId();
this._isTokenRefreshing = false;
}

Expand Down Expand Up @@ -486,7 +486,7 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
SPAUtils.setSignOutURL(await _authenticationClient.getSignOutUrl());
} */
if (config.storage !== BrowserStorage.WebWorker) {
SPAUtils.setSignOutURL(await this._authenticationClient.getSignOutUrl(), config.clientId, this._instanceID);
SPAUtils.setSignOutURL(await this._authenticationClient.getSignOutUrl(), config.clientId, this._instanceId);

if (this._spaHelper) {
this._spaHelper.clearRefreshTokenTimeout();
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/__legacy__/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface ResponseMessage<T> {
export interface Message<T> {
type: MessageType;
data?: T;
instanceId?: number;
}

export interface AuthorizationInfo {
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/src/__legacy__/utils/spa-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ export class SPAUtils {
sessionStorage.setItem(pkceKey, pkce);
}

public static setSignOutURL(url: string, clientId: string, instanceID: number): void {
public static setSignOutURL(url: string, clientId: string, instanceId: number): void {
sessionStorage.setItem(
`${OIDCRequestConstants.SignOut.Storage.StorageKeys.SIGN_OUT_URL}-instance_${instanceID}-${clientId}`,
`${OIDCRequestConstants.SignOut.Storage.StorageKeys.SIGN_OUT_URL}-instance_${instanceId}-${clientId}`,
url,
);
}

public static getSignOutUrl(clientId: string, instanceID: number): string {
public static getSignOutUrl(clientId: string, instanceId: number): string {
return (
sessionStorage.getItem(
`${OIDCRequestConstants.SignOut.Storage.StorageKeys.SIGN_OUT_URL}-instance_${instanceID}-${clientId}`,
`${OIDCRequestConstants.SignOut.Storage.StorageKeys.SIGN_OUT_URL}-instance_${instanceId}-${clientId}`,
) ?? ''
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/src/__legacy__/worker/worker-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {MemoryStore} from '../stores';
import {SPACryptoUtils} from '../utils/crypto-utils';

export const WebWorkerCore = async (
instanceID: number,
instanceId: number,
config: AuthClientConfig<WebWorkerClientConfig>,
getAuthHelper: (
authClient: AsgardeoAuthClient<WebWorkerClientConfig>,
Expand All @@ -52,7 +52,7 @@ export const WebWorkerCore = async (
const _store: Storage = new MemoryStore();
const _cryptoUtils: SPACryptoUtils = new SPACryptoUtils();
const _authenticationClient = new AsgardeoAuthClient<WebWorkerClientConfig>();
await _authenticationClient.initialize(config, _store, _cryptoUtils, instanceID);
await _authenticationClient.initialize(config, _store, _cryptoUtils, instanceId);

const _spaHelper = new SPAHelper<WebWorkerClientConfig>(_authenticationClient);

Expand All @@ -63,7 +63,7 @@ export const WebWorkerCore = async (

const _dataLayer = _authenticationClient.getStorageManager();

const _httpClient: HttpClientInstance = HttpClient.getInstance(instanceID);
const _httpClient: HttpClientInstance = HttpClient.getInstance(instanceId);

const attachToken = async (request: HttpRequestConfig): Promise<void> => {
await _authenticationHelper.attachTokenToRequestConfig(request);
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/src/__legacy__/worker/worker-receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export const workerReceiver = (
switch (data.type) {
case INIT:
try {
const {instanceID = 0, ...configData} = data.data;
const config: AuthClientConfig<WebWorkerClientConfig> = {...configData};
webWorker = await WebWorkerCore(instanceID, config, getAuthHelper);
const instanceId: number = data.instanceId ?? 0;
const config: AuthClientConfig<WebWorkerClientConfig> = {...data.data};
webWorker = await WebWorkerCore(instanceId, config, getAuthHelper);
webWorker.setHttpRequestFinishCallback(onRequestFinishCallback);
webWorker.setHttpRequestStartCallback(onRequestStartCallback);
webWorker.setHttpRequestSuccessCallback(onRequestSuccessCallback);
Expand Down
17 changes: 13 additions & 4 deletions packages/javascript/src/StorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ class StorageManager<T> {
await this.store.setData(key, dataToBeSavedJSON);
}

protected resolveKey(store: Stores | string, userId?: string): string {
return userId ? `${store}-${this.id}-${userId}` : `${store}-${this.id}`;
protected resolveKey(store: Stores | string, userId?: string, instanceId?: string): string {
if (userId && instanceId) {
return `${store}-${instanceId}-${userId}`;
}
if (userId) {
return `${store}-${this.id}-${userId}`;
}
if (instanceId) {
return `${store}-${instanceId}`;
}
return `${store}-${this.id}`;
}

protected static isLocalStorageAvailable(): boolean {
Expand Down Expand Up @@ -124,8 +133,8 @@ class StorageManager<T> {
return JSON.parse((await this.store.getData(this.resolveKey(Stores.TemporaryData, userId))) ?? null);
}

public async getSessionData(userId?: string): Promise<SessionData> {
return JSON.parse((await this.store.getData(this.resolveKey(Stores.SessionData, userId))) ?? null);
public async getSessionData(userId?: string, instanceId?: string): Promise<SessionData> {
return JSON.parse((await this.store.getData(this.resolveKey(Stores.SessionData, userId, instanceId))) ?? null);
}

public async getCustomData<K>(key: string, userId?: string): Promise<K> {
Expand Down
Loading
Loading