Path: blob/main/extensions/microsoft-authentication/src/common/loopbackClientAndOpener.ts
5241 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { ILoopbackClient, ServerAuthorizationCodeResponse } from '@azure/msal-node';6import type { UriEventHandler } from '../UriEventHandler';7import { env, LogOutputChannel, Uri } from 'vscode';8import { toPromise } from './async';910export interface ILoopbackClientAndOpener extends ILoopbackClient {11openBrowser(url: string): Promise<void>;12}1314export class UriHandlerLoopbackClient implements ILoopbackClientAndOpener {15constructor(16private readonly _uriHandler: UriEventHandler,17private readonly _redirectUri: string,18private readonly _callbackUri: Uri,19private readonly _logger: LogOutputChannel20) { }2122async listenForAuthCode(): Promise<ServerAuthorizationCodeResponse> {23const url = await toPromise(this._uriHandler.event);24this._logger.debug(`Received URL event. Authority: ${url.authority}`);25const result = new URL(url.toString(true));26return {27code: result.searchParams.get('code') ?? undefined,28state: result.searchParams.get('state') ?? undefined,29error: result.searchParams.get('error') ?? undefined,30error_description: result.searchParams.get('error_description') ?? undefined,31error_uri: result.searchParams.get('error_uri') ?? undefined,32};33}3435getRedirectUri(): string {36// We always return the constant redirect URL because37// it will handle redirecting back to the extension38return this._redirectUri;39}4041closeServer(): void {42// No-op43}4445async openBrowser(url: string): Promise<void> {46const uri = Uri.parse(url + `&state=${encodeURI(this._callbackUri.toString(true))}`);47await env.openExternal(uri);48}49}505152