Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/legacy interopt/token from url #2574

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .changeset/sweet-keys-prove.md
Original file line number Diff line number Diff line change
@@ -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,
);
```
36 changes: 12 additions & 24 deletions packages/react/legacy-interopt/src/LegacyAuthContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
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<string, AuthApp> = {};
async registerAppAsync(clientId: string, resources: string[], legacy = true): Promise<boolean> {
Expand All @@ -131,6 +131,7 @@ export class LegacyAuthContainer extends AuthContainer {
}

const newApp = new AuthApp(clientId, resources);
this._registeredApps[clientId] = newApp;
this.apps.push(newApp);
return true;
}
Expand All @@ -143,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);
Expand Down