From 18d0634d2864741b783c02ffd554d5508fc416c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Eikeland?= Date: Thu, 14 Nov 2024 10:50:05 +0100 Subject: [PATCH] feat(legacy-interopt): protected method for aquireTokenAsync --- .changeset/sweet-keys-prove.md | 32 +++++++++++++++++ .../src/LegacyAuthContainer.ts | 35 ++++++------------- 2 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 .changeset/sweet-keys-prove.md diff --git a/.changeset/sweet-keys-prove.md b/.changeset/sweet-keys-prove.md new file mode 100644 index 000000000..6d816f419 --- /dev/null +++ b/.changeset/sweet-keys-prove.md @@ -0,0 +1,32 @@ +--- +'@equinor/fusion-framework-legacy-interopt': patch +--- + +#### Updated Files: + +- `packages/react/legacy-interopt/src/create-fusion-context.ts` +- `packages/react/legacy-interopt/src/create-service-resolver.ts` + +#### Changes: + +1. **create-fusion-context.ts** + - Added a call to `authContainer.handleWindowCallbackAsync()` before initializing `TelemetryLogger`. + +```ts +const authContainer = new LegacyAuthContainer({ auth: framework.modules.auth }); + +await authContainer.handleWindowCallbackAsync(); + +const telemetryLogger = new TelemetryLogger(telemetry?.instrumentationKey ?? '', authContainer); +``` + +2. **create-service-resolver.ts** + - Changed the third parameter of authContainer.registerAppAsync from false to true. + +```ts +return authContainer.registerAppAsync( + id, + uris.map((x) => x.uri), + true, +); +``` diff --git a/packages/react/legacy-interopt/src/LegacyAuthContainer.ts b/packages/react/legacy-interopt/src/LegacyAuthContainer.ts index 00bfc5ad9..70090e1b1 100644 --- a/packages/react/legacy-interopt/src/LegacyAuthContainer.ts +++ b/packages/react/legacy-interopt/src/LegacyAuthContainer.ts @@ -100,21 +100,21 @@ export class LegacyAuthContainer extends AuthContainer { throw new FusionAuthAppNotFoundError(resource); } if (this._registeredApps[app.clientId]) { - // TODO - const defaultScope = app.clientId + '/.default'; - const res = await this.#auth.acquireToken({ scopes: [defaultScope] }); - if (res && res.accessToken) { - return res.accessToken; - } - // if (!accessToken) { - throw Error('failed to aquire token'); - // } - // return accessToken; + return this.__acquireTokenAsync(app); } console.trace(`FusionAuthContainer::acquireTokenAsync ${resource}`); return super.acquireTokenAsync(resource); } + protected async __acquireTokenAsync(app: AuthApp): Promise { + const defaultScope = app.clientId + '/.default'; + const res = await this.#auth.acquireToken({ scopes: [defaultScope] }); + if (res && res.accessToken) { + return res.accessToken; + } + throw Error('failed to aquire token'); + } + /** internal registry of 'new' apps registred for msal */ protected _registeredApps: Record = {}; async registerAppAsync(clientId: string, resources: string[], legacy = true): Promise { @@ -144,20 +144,7 @@ export class LegacyAuthContainer extends AuthContainer { const app = this.resolveApp(resource); if (app && app.clientId === global.clientId) { - const refreshUrl = `/auth/refresh`; - try { - const response = await fetch(refreshUrl, { - credentials: 'include', - method: 'POST', - }); - - if (response.status === 200) { - return response.text(); - } - } catch (err) { - // @todo AI - console.error(err); - } + return this.__acquireTokenAsync(app); } return super.refreshTokenAsync(resource);