diff --git a/lib/core/TokenSet.ts b/lib/core/TokenSet.ts new file mode 100644 index 000000000..041e98cd0 --- /dev/null +++ b/lib/core/TokenSet.ts @@ -0,0 +1,55 @@ +import { OktaAuthCoreInterface } from './types'; +import { TokenManager } from '../oidc'; +import { TOKEN_STORAGE_NAME } from '../constants'; +import { ServiceManager } from './ServiceManager'; + +export class TokenSet { + private sdk: OktaAuthCoreInterface; + private map: Map; // TODO: add type + + constructor(sdk: OktaAuthCoreInterface) { + this.sdk = sdk; + + this.map = new Map(); + const options = sdk.options; + const tokenManager = new TokenManager(this.sdk, options.tokenManager); + const serviceManager = new ServiceManager(this.sdk, options.services); + this.map.set(TOKEN_STORAGE_NAME, { + tokenManager, + serviceManager, + }); + } + + start(key?: string) { + // start services for one or all token sets + } + + stop(key?: string) { + // stop services for one or all token sets + } + + // call this method when need to add a new token set, like after receiving new tokens from authorize/idx + addTokenSet(tokens, options) { + const storageKey = options.storageKey; + const tokenManager = new TokenManager(this.sdk, options.tokenManager); // with specific storageKey + const serviceManager = new ServiceManager(this.sdk, options.services); + this.map.set(storageKey, { + tokenManager, + serviceManager, + }); + + tokenManager.setTokens(tokens); + + if (options.default) { + this.useTokenSet(options.tokenManager.storageKey); + } + } + + useTokenSet(key) { + const tokenSet = this.map.get(key); + this.sdk.tokenManager = tokenSet.tokenManager; + this.sdk.serviceManager = tokenSet.serviceManager; + // sync authState with new token set + this.sdk.authStateManager.updateAuthState(); + } +} diff --git a/lib/core/mixin.ts b/lib/core/mixin.ts index a81c74e80..340363b56 100644 --- a/lib/core/mixin.ts +++ b/lib/core/mixin.ts @@ -11,6 +11,7 @@ import { import { AuthStateManager } from './AuthStateManager'; import { ServiceManager } from './ServiceManager'; import { OktaAuthCoreInterface, OktaAuthCoreOptions } from './types'; +import { TokenSet } from './TokenSet'; export function mixinCore < @@ -27,6 +28,7 @@ export function mixinCore { authStateManager: AuthStateManager; serviceManager: ServiceManager; + tokenSet: TokenSet; constructor(...args: any[]) { super(...args); @@ -34,8 +36,8 @@ export function mixinCore // AuthStateManager this.authStateManager = new AuthStateManager(this); - // ServiceManager - this.serviceManager = new ServiceManager(this, this.options.services); + this.tokenSet = new TokenSet(this); + this.tokenSet.useTokenSet(this.options.tokenManager.storageKey); } async start() { diff --git a/lib/oidc/TokenManager.ts b/lib/oidc/TokenManager.ts index 07a626588..178f6c26b 100644 --- a/lib/oidc/TokenManager.ts +++ b/lib/oidc/TokenManager.ts @@ -105,7 +105,7 @@ export class TokenManager implements TokenManagerInterface { // eslint-disable-next-line complexity constructor(sdk: OktaAuthOAuthInterface, options: TokenManagerOptions = {}) { this.sdk = sdk; - this.emitter = (sdk as any).emitter; + this.emitter = new EventEmitter(); if (!this.emitter) { throw new AuthSdkError('Emitter should be initialized before TokenManager'); }