Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { IncomingMessage } from 'node:http'; |
| 19 | import { Duplex } from 'node:stream'; |
| 20 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 21 | import { jwtVerify } from 'jose'; |
| 22 | import log from 'loglevel'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 23 | import { Service } from 'typedi'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 24 | import { URL } from 'whatwg-url'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 25 | import { WebSocket, WebSocketServer } from 'ws'; |
| 26 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 27 | import { Vault } from './vault.js'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 28 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 29 | @Service() |
| 30 | export class Ws { |
| 31 | constructor(private readonly vault: Vault) {} |
| 32 | |
| 33 | async build() { |
| 34 | const wss = new WebSocketServer({ noServer: true }); |
| 35 | wss.on('connection', (ws: WebSocket, _req: IncomingMessage, accountId: string) => { |
| 36 | log.info('New connection', accountId); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 37 | // TODO: Add the account ID here to a map of accountId -> WebSocket connections |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 38 | |
| 39 | ws.on('message', (_data) => { |
| 40 | ws.send(JSON.stringify({ accountId })); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 41 | }); |
| 42 | }); |
| 43 | |
| 44 | return (request: IncomingMessage, socket: Duplex, head: Buffer) => { |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 45 | // Do not use parseURL because it returns a URLRecord and not a URL. |
| 46 | const url = new URL(request.url ?? '/', 'http://localhost/'); |
| 47 | const accessToken = url.searchParams.get('accessToken'); |
| 48 | if (!accessToken) { |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 49 | socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 50 | socket.destroy(); |
| 51 | return; |
| 52 | } |
| 53 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 54 | jwtVerify(accessToken, this.vault.publicKey, { |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 55 | issuer: 'urn:example:issuer', |
| 56 | audience: 'urn:example:audience', |
| 57 | }) |
| 58 | .then(({ payload }) => { |
| 59 | const id = payload.id as string; |
| 60 | log.info('Authentication successful', id); |
| 61 | wss.handleUpgrade(request, socket, head, (ws) => wss.emit('connection', ws, request, id)); |
| 62 | }) |
| 63 | .catch((reason) => { |
| 64 | log.debug('Authentication failed', reason); |
| 65 | socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); |
| 66 | socket.destroy(); |
| 67 | }); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 68 | }; |
| 69 | } |
| 70 | } |