|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { createServer, IncomingMessage, Server, ServerResponse } from "http"; |
| 7 | +import { Constants as CommonConstants, UrlString, ServerAuthorizationCodeResponse } from "@azure/msal-common"; |
| 8 | +import { ILoopbackClient } from "@azure/msal-node"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements ILoopbackClient interface to listen for authZ code response. |
| 12 | + * This custom implementation checks for a preferred port and uses it if available. |
| 13 | + */ |
| 14 | +export class CustomLoopbackClient implements ILoopbackClient { |
| 15 | + port: number = 0; // default port, which will be set to a random available port |
| 16 | + private server: Server; |
| 17 | + |
| 18 | + private constructor(port: number = 0) { |
| 19 | + this.port = port; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Initializes a loopback server with an available port |
| 24 | + * @param preferredPort |
| 25 | + * @param logger |
| 26 | + * @returns |
| 27 | + */ |
| 28 | + static async initialize(preferredPort: number | undefined): Promise<CustomLoopbackClient> { |
| 29 | + const loopbackClient = new CustomLoopbackClient(); |
| 30 | + |
| 31 | + if (preferredPort === 0 || preferredPort === undefined) { |
| 32 | + return loopbackClient; |
| 33 | + } |
| 34 | + const isPortAvailable = await loopbackClient.isPortAvailable(preferredPort); |
| 35 | + |
| 36 | + if (isPortAvailable) { |
| 37 | + loopbackClient.port = preferredPort; |
| 38 | + } |
| 39 | + |
| 40 | + return loopbackClient; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Spins up a loopback server which returns the server response when the localhost redirectUri is hit |
| 45 | + * @param successTemplate |
| 46 | + * @param errorTemplate |
| 47 | + * @returns |
| 48 | + */ |
| 49 | + async listenForAuthCode(successTemplate?: string, errorTemplate?: string): Promise<ServerAuthorizationCodeResponse> { |
| 50 | + if (!!this.server) { |
| 51 | + throw new Error('Loopback server already exists. Cannot create another.') |
| 52 | + } |
| 53 | + |
| 54 | + const authCodeListener = new Promise<ServerAuthorizationCodeResponse>((resolve, reject) => { |
| 55 | + this.server = createServer(async (req: IncomingMessage, res: ServerResponse) => { |
| 56 | + const url = req.url; |
| 57 | + if (!url) { |
| 58 | + res.end(errorTemplate || "Error occurred loading redirectUrl"); |
| 59 | + reject(new Error('Loopback server callback was invoked without a url. This is unexpected.')); |
| 60 | + return; |
| 61 | + } else if (url === CommonConstants.FORWARD_SLASH) { |
| 62 | + res.end(successTemplate || "Auth code was successfully acquired. You can close this window now."); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const authCodeResponse = UrlString.getDeserializedQueryString(url); |
| 67 | + if (authCodeResponse.code) { |
| 68 | + const redirectUri = await this.getRedirectUri(); |
| 69 | + res.writeHead(302, { location: redirectUri }); // Prevent auth code from being saved in the browser history |
| 70 | + res.end(); |
| 71 | + } |
| 72 | + resolve(authCodeResponse); |
| 73 | + }); |
| 74 | + this.server.listen(this.port); |
| 75 | + }); |
| 76 | + |
| 77 | + // Wait for server to be listening |
| 78 | + await new Promise<void>((resolve) => { |
| 79 | + let ticks = 0; |
| 80 | + const id = setInterval(() => { |
| 81 | + if ((5000 / 100) < ticks) { |
| 82 | + throw new Error('Timed out waiting for auth code listener to be registered.'); |
| 83 | + } |
| 84 | + |
| 85 | + if (this.server.listening) { |
| 86 | + clearInterval(id); |
| 87 | + resolve(); |
| 88 | + } |
| 89 | + ticks++; |
| 90 | + }, 100); |
| 91 | + }); |
| 92 | + |
| 93 | + return authCodeListener; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the port that the loopback server is running on |
| 98 | + * @returns |
| 99 | + */ |
| 100 | + getRedirectUri(): string { |
| 101 | + if (!this.server) { |
| 102 | + throw new Error('No loopback server exists yet.') |
| 103 | + } |
| 104 | + |
| 105 | + const address = this.server.address(); |
| 106 | + if (!address || typeof address === "string" || !address.port) { |
| 107 | + this.closeServer(); |
| 108 | + throw new Error('Loopback server address is not type string. This is unexpected.') |
| 109 | + } |
| 110 | + |
| 111 | + const port = address && address.port; |
| 112 | + |
| 113 | + return `http://localhost:${port}`; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Close the loopback server |
| 118 | + */ |
| 119 | + closeServer(): void { |
| 120 | + if (!!this.server) { |
| 121 | + this.server.close(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Attempts to create a server and listen on a given port |
| 127 | + * @param port |
| 128 | + * @returns |
| 129 | + */ |
| 130 | + isPortAvailable(port: number): Promise<boolean> { |
| 131 | + return new Promise(resolve => { |
| 132 | + const server = createServer() |
| 133 | + .listen(port, () => { |
| 134 | + server.close(); |
| 135 | + resolve(true); |
| 136 | + }) |
| 137 | + .on("error", () => { |
| 138 | + resolve(false); |
| 139 | + }); |
| 140 | + }); |
| 141 | + } |
| 142 | +} |
0 commit comments