Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/microsoft-authentication/src/common/loopbackClientAndOpener.ts
5241 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { ILoopbackClient, ServerAuthorizationCodeResponse } from '@azure/msal-node';
7
import type { UriEventHandler } from '../UriEventHandler';
8
import { env, LogOutputChannel, Uri } from 'vscode';
9
import { toPromise } from './async';
10
11
export interface ILoopbackClientAndOpener extends ILoopbackClient {
12
openBrowser(url: string): Promise<void>;
13
}
14
15
export class UriHandlerLoopbackClient implements ILoopbackClientAndOpener {
16
constructor(
17
private readonly _uriHandler: UriEventHandler,
18
private readonly _redirectUri: string,
19
private readonly _callbackUri: Uri,
20
private readonly _logger: LogOutputChannel
21
) { }
22
23
async listenForAuthCode(): Promise<ServerAuthorizationCodeResponse> {
24
const url = await toPromise(this._uriHandler.event);
25
this._logger.debug(`Received URL event. Authority: ${url.authority}`);
26
const result = new URL(url.toString(true));
27
return {
28
code: result.searchParams.get('code') ?? undefined,
29
state: result.searchParams.get('state') ?? undefined,
30
error: result.searchParams.get('error') ?? undefined,
31
error_description: result.searchParams.get('error_description') ?? undefined,
32
error_uri: result.searchParams.get('error_uri') ?? undefined,
33
};
34
}
35
36
getRedirectUri(): string {
37
// We always return the constant redirect URL because
38
// it will handle redirecting back to the extension
39
return this._redirectUri;
40
}
41
42
closeServer(): void {
43
// No-op
44
}
45
46
async openBrowser(url: string): Promise<void> {
47
const uri = Uri.parse(url + `&state=${encodeURI(this._callbackUri.toString(true))}`);
48
await env.openExternal(uri);
49
}
50
}
51
52