|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2022 - 2023 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + ***********************************************************************/ |
| 18 | + |
| 19 | +import * as fs from 'fs'; |
| 20 | +import * as http from 'http'; |
| 21 | +import * as path from 'path'; |
| 22 | +import * as url from 'url'; |
| 23 | +import { ServerConfig, AuthConfig } from './configuration'; |
| 24 | + |
| 25 | +interface Deferred<T> { |
| 26 | + resolve: (result: T | Promise<T>) => void; |
| 27 | + reject: (reason: any) => void; |
| 28 | +} |
| 29 | + |
| 30 | +export function createServer(config: AuthConfig, nonce: string) { |
| 31 | + type RedirectResult = |
| 32 | + | { req: http.IncomingMessage; res: http.ServerResponse } |
| 33 | + | { err: any; res: http.ServerResponse }; |
| 34 | + let deferredRedirect: Deferred<RedirectResult>; |
| 35 | + const redirectPromise = new Promise<RedirectResult>((resolve, reject) => (deferredRedirect = { resolve, reject })); |
| 36 | + |
| 37 | + let deferredCallback: Deferred<RedirectResult>; |
| 38 | + const callbackPromise = new Promise<RedirectResult>((resolve, reject) => (deferredCallback = { resolve, reject })); |
| 39 | + |
| 40 | + const server = http.createServer(function (req, res) { |
| 41 | + const reqUrl = url.parse(req.url!, /* parseQueryString */ true); |
| 42 | + console.log(`Received ${reqUrl.pathname}`); |
| 43 | + switch (reqUrl.pathname) { |
| 44 | + case '/signin': |
| 45 | + // eslint-disable-next-line no-case-declarations |
| 46 | + const receivedNonce = ((reqUrl.query.nonce as string) || '').replace(/ /g, '+'); |
| 47 | + if (receivedNonce === nonce) { |
| 48 | + deferredRedirect.resolve({ req, res }); |
| 49 | + } else { |
| 50 | + const err = new Error('Nonce does not match.'); |
| 51 | + deferredRedirect.resolve({ err, res }); |
| 52 | + } |
| 53 | + break; |
| 54 | + case '/': |
| 55 | + sendFile(res, path.join(__dirname, '../www/success.html'), 'text/html; charset=utf-8'); |
| 56 | + break; |
| 57 | + case '/auth.css': |
| 58 | + sendFile(res, path.join(__dirname, '../www/auth.css'), 'text/css; charset=utf-8'); |
| 59 | + break; |
| 60 | + case '/favicon.ico': |
| 61 | + sendFile(res, path.join(__dirname, '../www/favicon.ico'), 'image/vnd.microsoft.icon'); |
| 62 | + break; |
| 63 | + case `/${config.serverConfig.callbackPath}`: |
| 64 | + deferredCallback.resolve({ req, res }); |
| 65 | + break; |
| 66 | + default: |
| 67 | + res.writeHead(404); |
| 68 | + res.end(); |
| 69 | + break; |
| 70 | + } |
| 71 | + }); |
| 72 | + return { server, redirectPromise, callbackPromise }; |
| 73 | +} |
| 74 | + |
| 75 | +export async function startServer(config: ServerConfig, server: http.Server): Promise<string> { |
| 76 | + let portTimer: NodeJS.Timer; |
| 77 | + |
| 78 | + function cancelPortTimer() { |
| 79 | + clearTimeout(portTimer); |
| 80 | + } |
| 81 | + |
| 82 | + const port = new Promise<string>((resolve, reject) => { |
| 83 | + portTimer = setTimeout(() => { |
| 84 | + reject(new Error('Timeout waiting for port')); |
| 85 | + }, 5000); |
| 86 | + |
| 87 | + server.on('listening', () => { |
| 88 | + const address = server.address(); |
| 89 | + if (typeof address === 'undefined' || address === null) { |
| 90 | + reject(new Error('adress is null or undefined')); |
| 91 | + } else if (typeof address === 'string') { |
| 92 | + resolve(address); |
| 93 | + } else { |
| 94 | + resolve(address.port.toString()); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + server.on('error', _ => { |
| 99 | + reject(new Error('Error listening to server')); |
| 100 | + }); |
| 101 | + |
| 102 | + server.on('close', () => { |
| 103 | + reject(new Error('Closed')); |
| 104 | + }); |
| 105 | + |
| 106 | + server.listen(config.port); |
| 107 | + }); |
| 108 | + |
| 109 | + port.then(cancelPortTimer, cancelPortTimer); |
| 110 | + return port; |
| 111 | +} |
| 112 | + |
| 113 | +function sendFile(res: http.ServerResponse, filepath: string, contentType: string) { |
| 114 | + fs.readFile(filepath, (err, body) => { |
| 115 | + if (err) { |
| 116 | + console.error(err); |
| 117 | + res.writeHead(404); |
| 118 | + res.end(); |
| 119 | + } else { |
| 120 | + res.writeHead(200, { |
| 121 | + 'Content-Length': body.length, |
| 122 | + 'Content-Type': contentType, |
| 123 | + }); |
| 124 | + res.end(body); |
| 125 | + } |
| 126 | + }); |
| 127 | +} |
0 commit comments