|  | 
|  | 1 | +//===----------------------------------------------------------------------===// | 
|  | 2 | +// | 
|  | 3 | +// This source file is part of the VS Code Swift open source project | 
|  | 4 | +// | 
|  | 5 | +// Copyright (c) 2025 the VS Code Swift project authors | 
|  | 6 | +// Licensed under Apache License v2.0 | 
|  | 7 | +// | 
|  | 8 | +// See LICENSE.txt for license information | 
|  | 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | 
|  | 10 | +// | 
|  | 11 | +// SPDX-License-Identifier: Apache-2.0 | 
|  | 12 | +// | 
|  | 13 | +//===----------------------------------------------------------------------===// | 
|  | 14 | +import * as crypto from "crypto"; | 
|  | 15 | +import * as http from "http"; | 
|  | 16 | +import * as vscode from "vscode"; | 
|  | 17 | + | 
|  | 18 | +/** Options that can be used to configure the behavior of {@link withAskpassServer}. */ | 
|  | 19 | +export interface WithAskpassServerOptions { | 
|  | 20 | +    /** The title of the input box shown in VS Code. */ | 
|  | 21 | +    title?: string; | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +/** | 
|  | 25 | + * Creates a temporary HTTP server that can be used to handle askpass requests from various terminal | 
|  | 26 | + * applications. The server will be closed when the provided task completes. | 
|  | 27 | + * | 
|  | 28 | + * The task will be provided with a randomly generated nonce and port number used for connecting to | 
|  | 29 | + * the server. Requests without a valid nonce will be rejected with a 401 status code. | 
|  | 30 | + * | 
|  | 31 | + * @param task Function to execute while the server is listening for connections | 
|  | 32 | + * @returns Promise that resolves when the task completes and server is cleaned up | 
|  | 33 | + */ | 
|  | 34 | +export async function withAskpassServer<T>( | 
|  | 35 | +    task: (nonce: string, port: number) => Promise<T>, | 
|  | 36 | +    options: WithAskpassServerOptions = {} | 
|  | 37 | +): Promise<T> { | 
|  | 38 | +    const nonce = crypto.randomBytes(32).toString("hex"); | 
|  | 39 | +    const server = http.createServer((req, res) => { | 
|  | 40 | +        if (!req.url) { | 
|  | 41 | +            return res.writeHead(404).end(); | 
|  | 42 | +        } | 
|  | 43 | + | 
|  | 44 | +        const url = new URL(req.url, `http://localhost`); | 
|  | 45 | +        if (url.pathname !== "/askpass") { | 
|  | 46 | +            return res.writeHead(404).end(); | 
|  | 47 | +        } | 
|  | 48 | + | 
|  | 49 | +        const requestNonce = url.searchParams.get("nonce"); | 
|  | 50 | +        if (requestNonce !== nonce) { | 
|  | 51 | +            return res.writeHead(401).end(); | 
|  | 52 | +        } | 
|  | 53 | + | 
|  | 54 | +        void vscode.window | 
|  | 55 | +            .showInputBox({ | 
|  | 56 | +                password: true, | 
|  | 57 | +                title: options.title, | 
|  | 58 | +                placeHolder: "Please enter your password", | 
|  | 59 | +                ignoreFocusOut: true, | 
|  | 60 | +            }) | 
|  | 61 | +            .then(password => { | 
|  | 62 | +                res.writeHead(200, { "Content-Type": "application/json" }).end( | 
|  | 63 | +                    JSON.stringify({ password }) | 
|  | 64 | +                ); | 
|  | 65 | +            }); | 
|  | 66 | +    }); | 
|  | 67 | + | 
|  | 68 | +    return new Promise((resolve, reject) => { | 
|  | 69 | +        server.listen(0, "localhost", async () => { | 
|  | 70 | +            try { | 
|  | 71 | +                const address = server.address(); | 
|  | 72 | +                if (!address || typeof address === "string") { | 
|  | 73 | +                    throw new Error("Failed to get server port"); | 
|  | 74 | +                } | 
|  | 75 | +                const port = address.port; | 
|  | 76 | +                resolve(await task(nonce, port)); | 
|  | 77 | +            } catch (error) { | 
|  | 78 | +                reject(error); | 
|  | 79 | +            } finally { | 
|  | 80 | +                server.close(); | 
|  | 81 | +            } | 
|  | 82 | +        }); | 
|  | 83 | + | 
|  | 84 | +        server.on("error", error => { | 
|  | 85 | +            reject(error); | 
|  | 86 | +        }); | 
|  | 87 | +    }); | 
|  | 88 | +} | 
0 commit comments