public async getAuthorizationCode()

in src/azure/azureAuthRequest.ts [39:125]


	public async getAuthorizationCode(signInUrl: string, authComplete: Promise<void>): Promise<string> {
		let mediaPath = path.join(this.context.extensionPath, 'media');
		// media path goes here - working directory for this extension
		const sendFile = async (res: http.ServerResponse, filePath: string, contentType: string): Promise<void> => {
			let fileContents;
			try {
				fileContents = await fs.readFile(filePath);
			} catch (ex) {
				this.logger.error(ex);
				res.writeHead(400);
				res.end();
				return;
			}

			res.writeHead(200, {
				'Content-Length': fileContents.length,
				'Content-Type': contentType
			});

			res.end(fileContents);
		};

		this.simpleWebServer.on('/landing.css', (req, reqUrl, res) => {
			sendFile(res, path.join(mediaPath, 'landing.css'), 'text/css; charset=utf-8').catch(this.logger.error);
		});

		this.simpleWebServer.on('/SignIn.svg', (req, reqUrl, res) => {
			sendFile(res, path.join(mediaPath, 'SignIn.svg'), 'image/svg+xml').catch(this.logger.error);
		});

		this.simpleWebServer.on('/signin', (req, reqUrl, res) => {
			let receivedNonce: string = reqUrl.query.nonce as string;
			receivedNonce = receivedNonce.replace(/ /g, '+');

			if (receivedNonce !== encodeURIComponent(this.nonce)) {
				res.writeHead(400, { 'content-type': 'text/html' });
				// res.write(localize('azureAuth.nonceError', 'Authentication failed due to a nonce mismatch, please close Azure Data Studio and try again.'));
				res.end();
				this.logger.error('nonce no match', receivedNonce, this.nonce);
				return;
			}
			res.writeHead(302, { Location: signInUrl });
			res.end();
		});

		return new Promise<string>((resolve, reject) => {
			this.simpleWebServer.on('/callback', (req, reqUrl, res) => {
				const state = reqUrl.query.state as string ?? '';
				const code = reqUrl.query.code as string ?? '';

				const stateSplit = state.split(',');
				if (stateSplit.length !== 2) {
					res.writeHead(400, { 'content-type': 'text/html' });
					// res.write(localize('azureAuth.stateError', 'Authentication failed due to a state mismatch, please close ADS and try again.'));
					res.end();
					reject(new Error('State mismatch'));
					return;
				}

				if (stateSplit[1] !== encodeURIComponent(this.nonce)) {
					res.writeHead(400, { 'content-type': 'text/html' });
					// res.write(localize('azureAuth.nonceError', 'Authentication failed due to a nonce mismatch,
					// please close Azure Data Studio and try again.'));
					res.end();
					reject(new Error('Nonce mismatch'));
					return;
				}

				resolve(code);

				authComplete.then(() => {
					sendFile(res, path.join(mediaPath, 'landing.html'), 'text/html; charset=utf-8').catch(console.error);
				}, (ex: Error) => {
					res.writeHead(400, { 'content-type': 'text/html' });
					res.write(ex.message);
					res.end();
				});
			});
		});

		return;

		// check the state that is returned from the local web server for the server port and nonce


		// private addServerListeners(server: SimpleWebServer, )
	}