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

[Identity] InteractiveBrowserCredential for Node now waits for the listener to start listening #15497

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions sdk/identity/identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 2.0.0-beta.4 (Unreleased)

## Bug fixes

- Fixed an issue in which `InteractiveBrowserCredential` on Node would sometimes cause the process to hang if there was no browser available.
### Breaking changes

- Removed the protected method `getAzureCliAccessToken` from the public API of the `AzureCliCredential`. While it will continue to be available as part of v1, we won't be supporting this method as part of v2's public API.
Expand Down
28 changes: 17 additions & 11 deletions sdk/identity/identity/src/msal/nodeFlows/msalOpenBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,13 @@ export class MsalOpenBrowser extends MsalNode {
cleanup();
});
};

const app = http.createServer(requestListener);
const server = stoppable(app);

const listen = app.listen(this.port, this.hostname, () =>
this.logger.info(`InteractiveBrowserCredential listening on port ${this.port}!`)
);
app.on("connection", (socket) => socketToDestroy.push(socket));
const server = stoppable(app);

this.openAuthCodeUrl(scopes).catch((e) => {
cleanup();
reject(e);
});

function cleanup(): void {
if (listen) {
Expand All @@ -166,13 +161,24 @@ export class MsalOpenBrowser extends MsalNode {
}
}

const abortSignal = options?.abortSignal;
if (abortSignal) {
abortSignal.addEventListener("abort", () => {
app.on("connection", (socket) => socketToDestroy.push(socket));

app.on("listening", () => {
const openPromise = this.openAuthCodeUrl(scopes);

const abortSignal = options?.abortSignal;
if (abortSignal) {
abortSignal.addEventListener("abort", () => {
cleanup();
reject(new Error("Aborted"));
sadasant marked this conversation as resolved.
Show resolved Hide resolved
});
}

openPromise.then().catch((e) => {
cleanup();
reject(new Error("Aborted"));
});
sadasant marked this conversation as resolved.
Show resolved Hide resolved
}
});
});
}

Expand Down