simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [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 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | 'use strict'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 19 | |
simon | 7e00d61 | 2022-10-01 20:06:53 -0400 | [diff] [blame] | 20 | import dotenv from 'dotenv'; |
simon | 6c2a63d | 2022-10-11 13:51:29 -0400 | [diff] [blame] | 21 | const nodeEnv = process.env.NODE_ENV || 'development'; |
| 22 | dotenv.config({ path: `.env.${nodeEnv}` }); |
simon | 7e00d61 | 2022-10-01 20:06:53 -0400 | [diff] [blame] | 23 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 24 | import cors from 'cors'; |
| 25 | import express, { NextFunction, Request, Response } from 'express'; |
| 26 | import session from 'express-session'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 27 | import { promises as fs } from 'fs'; |
| 28 | import http from 'http'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 29 | import passport from 'passport'; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 30 | import { IVerifyOptions, Strategy as LocalStrategy } from 'passport-local'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 31 | import path from 'path'; |
| 32 | import { Server, Socket } from 'socket.io'; |
| 33 | import { ExtendedError } from 'socket.io/dist/namespace'; |
| 34 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 35 | import JamiDaemon from './JamiDaemon'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 36 | import Account from './model/Account'; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 37 | import { Session } from './model/util'; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 38 | //import { createRequire } from 'module'; |
| 39 | //const require = createRequire(import.meta.url); |
Adrien Béraud | 947e879 | 2021-04-15 18:32:44 -0400 | [diff] [blame] | 40 | //const redis = require('redis-url').connect() |
| 41 | //const RedisStore = require('connect-redis')(session) |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 42 | /*const passportSocketIo = require('passport.socketio')*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 43 | import indexRouter from './routes/index.js'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 44 | import JamiRestApi from './routes/jami.js'; |
idillon | 8e6c006 | 2022-09-16 13:34:43 -0400 | [diff] [blame] | 45 | // import { sentrySetUp } from './sentry.js' |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 46 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 47 | const configPath = 'jamiServerConfig.json'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 48 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 49 | //const sessionStore = new RedisStore({ client: redis }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 50 | const sessionStore = new session.MemoryStore(); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 51 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 52 | interface User { |
| 53 | id: string; |
| 54 | config: UserConfig; |
| 55 | username: string; |
| 56 | accountFilter?: (account: any) => boolean; |
| 57 | } |
| 58 | |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 59 | interface UserConfig { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 60 | accountId?: string; |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 61 | accounts: string; |
| 62 | password?: string; |
| 63 | username?: string; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 64 | type?: string; |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | interface AppConfig { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 68 | users: Record<string, UserConfig>; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 69 | authMethods: unknown[]; |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | const loadConfig = async (filePath: string): Promise<AppConfig> => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 73 | const config = { users: {}, authMethods: [] }; |
| 74 | try { |
| 75 | return Object.assign(config, JSON.parse((await fs.readFile(filePath)).toString())); |
| 76 | } catch (e) { |
| 77 | console.log(e); |
| 78 | return config; |
| 79 | } |
| 80 | }; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 81 | |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 82 | const saveConfig = (filePath: string, config: AppConfig) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 83 | return fs.writeFile(filePath, JSON.stringify(config)); |
| 84 | }; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 85 | |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 86 | /* |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 87 | Share sessions between Passport.js and Socket.io |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 88 | */ |
| 89 | |
| 90 | function logSuccess() { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 91 | console.log('passportSocketIo authorized user with Success 😁'); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | function logFail() { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 95 | console.log('passportSocketIo failed to authorized user 👺'); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 99 | |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 100 | tempAccounts holds users accounts while tempting to authenticate them on Jams. |
| 101 | connectedUsers holds users accounts after they got authenticated by Jams. |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 102 | |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 103 | Users should be removed from connectedUsers when receiving a disconnect |
| 104 | web socket call |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 105 | |
| 106 | */ |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 107 | const tempAccounts: Record< |
| 108 | string, |
| 109 | { |
| 110 | newUser: Express.User; |
| 111 | done: (error: any, user?: any, options?: IVerifyOptions) => void; |
| 112 | } |
| 113 | > = {}; |
| 114 | const connectedUsers: Record<string, UserConfig> = {}; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 115 | |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 116 | const createServer = async (appConfig: AppConfig) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 117 | const app = express(); |
simon | 6c2a63d | 2022-10-11 13:51:29 -0400 | [diff] [blame] | 118 | console.log(`Loading server for ${nodeEnv} with config:`); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 119 | console.log(appConfig); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 120 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 121 | const corsOptions = { |
| 122 | origin: 'http://127.0.0.1:3000', |
| 123 | }; |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 124 | |
simon | 6c2a63d | 2022-10-11 13:51:29 -0400 | [diff] [blame] | 125 | if (nodeEnv === 'development') { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 126 | const webpack = await import('webpack'); |
| 127 | const webpackDev = await import('webpack-dev-middleware'); |
| 128 | const webpackHot = await import('webpack-hot-middleware'); |
| 129 | const { default: webpackConfig } = await import('jami-web-client/webpack.config.js'); |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 130 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 131 | const compiler = webpack.default(webpackConfig); |
| 132 | app.use( |
| 133 | webpackDev.default(compiler, { |
| 134 | publicPath: webpackConfig.output?.publicPath, |
| 135 | }) |
| 136 | ); |
| 137 | app.use(webpackHot.default(compiler)); |
| 138 | } |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 139 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 140 | /* |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 141 | Configuation for Passeport Js |
| 142 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 143 | app.disable('x-powered-by'); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 144 | |
simon | 6c2a63d | 2022-10-11 13:51:29 -0400 | [diff] [blame] | 145 | const secretKey = process.env.SECRET_KEY_BASE; |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 146 | |
simon | 6c2a63d | 2022-10-11 13:51:29 -0400 | [diff] [blame] | 147 | if (!secretKey) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 148 | throw new Error('SECRET_KEY_BASE undefined'); |
| 149 | } |
| 150 | |
| 151 | const sessionMiddleware = session({ |
| 152 | store: sessionStore, |
| 153 | resave: false, |
| 154 | saveUninitialized: true, |
| 155 | cookie: { |
| 156 | secure: false, //!development, |
| 157 | maxAge: 2419200000, |
| 158 | }, |
simon | 6c2a63d | 2022-10-11 13:51:29 -0400 | [diff] [blame] | 159 | secret: secretKey, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 160 | }); |
| 161 | |
| 162 | app.use(sessionMiddleware); |
| 163 | app.use(passport.initialize()); |
| 164 | app.use(passport.session()); |
| 165 | // app.use(app.router) |
| 166 | app.use(cors(corsOptions)); |
| 167 | |
| 168 | const jami = new JamiDaemon((account: Account, conversation: any, message: any) => { |
| 169 | console.log('JamiDaemon onMessage'); |
| 170 | |
| 171 | if (conversation.listeners) { |
| 172 | Object.values(conversation.listeners).forEach((listener: any) => { |
| 173 | listener.socket.emit('newMessage', message); |
| 174 | }); |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 175 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 176 | }); |
| 177 | const apiRouter = new JamiRestApi(jami).getRouter(); |
simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 178 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 179 | /* |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 180 | io.use(passportSocketIo.authorize({ |
| 181 | key: 'connect.sid', |
| 182 | secret: process.env.SECRET_KEY_BASE, |
| 183 | store: sessionStore, |
| 184 | passport: passport, |
| 185 | cookieParser: cookieParser, |
| 186 | //success: logSuccess(), |
| 187 | // fail: logFail(), |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 188 | })) |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 189 | */ |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 190 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 191 | const isSetupComplete = () => { |
| 192 | return 'admin' in appConfig.users; |
| 193 | }; |
| 194 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 195 | const accountFilter = (filter: string | string[]) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 196 | if (typeof filter === 'string') { |
| 197 | if (filter === '*') return undefined; |
| 198 | else return (account: Account) => account.getId() === filter; |
| 199 | } else if (Array.isArray(filter)) { |
| 200 | return (account: Account) => filter.includes(account.getId()); |
| 201 | } else { |
| 202 | throw new Error('Invalid account filter string'); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 203 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 204 | }; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 205 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 206 | const user = (id: string, config: UserConfig) => { |
| 207 | return { |
| 208 | id, |
| 209 | config, |
| 210 | username: config.username || id, |
| 211 | accountFilter: accountFilter(config.accounts), |
| 212 | }; |
| 213 | }; |
| 214 | |
| 215 | passport.serializeUser((user: any, done) => { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 216 | user = user as User; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 217 | connectedUsers[user.id] = user.config; |
| 218 | console.log('=============================SerializeUser called ' + user.id); |
| 219 | console.log(user); |
| 220 | done(null, user.id); |
| 221 | }); |
| 222 | |
| 223 | const deserializeUser = (id: string, done: (err: any, user?: Express.User | false | null) => void) => { |
| 224 | console.log('=============================DeserializeUser called on: ' + id); |
| 225 | const userConfig = connectedUsers[id]; |
| 226 | console.log(userConfig); |
| 227 | if (userConfig) { |
| 228 | done(null, user(id, userConfig)); |
| 229 | } else done(404, null); |
| 230 | }; |
| 231 | passport.deserializeUser(deserializeUser); |
| 232 | |
| 233 | const jamsStrategy = new LocalStrategy(async (username, password, done) => { |
| 234 | const accountId = await jami.addAccount({ |
| 235 | managerUri: 'https://jams.savoirfairelinux.com', |
| 236 | managerUsername: username, |
| 237 | archivePassword: password, |
| 238 | }); |
| 239 | const id = `jams_${username}`; |
| 240 | const userConfig = { username, type: 'jams', accounts: accountId }; |
| 241 | const newUser = user(id, userConfig); |
| 242 | console.log('AccountId: ' + accountId); |
| 243 | tempAccounts[accountId] = { done, newUser }; |
| 244 | }); |
| 245 | jamsStrategy.name = 'jams'; |
| 246 | |
| 247 | const localStrategy = new LocalStrategy((username, password, done) => { |
| 248 | console.log('localStrategy: ' + username + ' ' + password); |
| 249 | |
| 250 | const id = username; |
| 251 | const userConfig = appConfig.users[username]; |
| 252 | if (!userConfig) { |
| 253 | return done(null, false, { message: 'Incorrect username.' }); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 254 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 255 | if (userConfig.password !== password) { |
| 256 | return done(null, false, { message: 'Incorrect password.' }); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 257 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 258 | userConfig.type = 'local'; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 259 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 260 | done(null, user(id, userConfig)); |
| 261 | }); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 262 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 263 | passport.use(jamsStrategy); |
| 264 | passport.use(localStrategy); |
| 265 | |
| 266 | const secured = (req: Request, res: Response, next: NextFunction) => { |
| 267 | if (req.user) { |
| 268 | return next(); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 269 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 270 | res.status(401).end(); |
| 271 | }; |
| 272 | const securedRedirect = (req: Request, res: Response, next: NextFunction) => { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 273 | const user = req.user as UserConfig | undefined; |
| 274 | if (user?.accountId) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 275 | return next(); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 276 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 277 | (req.session as any).returnTo = req.originalUrl; |
| 278 | res.redirect('/login'); |
| 279 | }; |
| 280 | |
| 281 | app.use(express.json()); |
| 282 | app.post('/setup', (req, res) => { |
| 283 | if (isSetupComplete()) { |
| 284 | return res.status(404).end(); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 285 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 286 | if (!req.body.password) { |
| 287 | return res.status(400).end(); |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 288 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 289 | console.log(req.body); |
| 290 | appConfig.users.admin = { |
| 291 | accounts: '*', |
| 292 | password: req.body.password, |
| 293 | }; |
| 294 | res.status(200).end(); |
| 295 | saveConfig(configPath, appConfig); |
| 296 | }); |
| 297 | app.post('/auth/jams', passport.authenticate('jams'), (req, res) => { |
| 298 | res.json({ loggedin: true }); |
| 299 | }); |
| 300 | app.post('/auth/local', passport.authenticate('local'), (req, res) => { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 301 | res.json({ loggedin: true, user: (req.user as User | undefined)?.id }); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 302 | }); |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 303 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 304 | const getState = (req: Request) => { |
| 305 | if (req.user) { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 306 | const user = req.user as UserConfig; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 307 | return { loggedin: true, username: user.username, type: user.type }; |
| 308 | } else if (isSetupComplete()) { |
| 309 | return {}; |
| 310 | } else { |
| 311 | return { setupComplete: false }; |
| 312 | } |
| 313 | }; |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 314 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 315 | // sentrySetUp(app); |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 316 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 317 | app.get('/auth', (req, res) => { |
| 318 | const state = getState(req); |
| 319 | if (req.user) { |
| 320 | res.json(state); |
| 321 | } else { |
| 322 | res.status(401).json(state); |
| 323 | } |
| 324 | }); |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 325 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 326 | app.use('/api', secured, apiRouter); |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 327 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 328 | app.use('/', indexRouter); |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 329 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 330 | /* GET React App */ |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 331 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 332 | const cwd = process.cwd(); |
| 333 | app.use(express.static(path.join(cwd, 'client/dist'))); |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 334 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 335 | app.use((req, res) => { |
| 336 | res.render(path.join(cwd, 'client/dist/index.ejs'), { |
| 337 | initdata: JSON.stringify(getState(req)), |
| 338 | }); |
| 339 | }); |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 340 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 341 | // @ts-ignore TODO: Fix the typescript error |
| 342 | const server = http.Server(app); |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 343 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 344 | const io = new Server(server, { cors: corsOptions }); |
| 345 | const wrap = (middleware: any) => (socket: Socket, next: (err?: ExtendedError) => void) => |
| 346 | middleware(socket.request, {}, next); |
| 347 | io.use(wrap(sessionMiddleware)); |
| 348 | io.use(wrap(passport.initialize())); |
| 349 | io.use(wrap(passport.session())); |
| 350 | io.use((socket, next) => { |
| 351 | if ((socket.request as any).user) { |
| 352 | next(); |
| 353 | } else { |
| 354 | next(new Error('unauthorized')); |
| 355 | } |
| 356 | }); |
| 357 | io.on('connect', (socket) => { |
| 358 | console.log(`new connection ${socket.id}`); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 359 | const session: Session = (socket.request as any).session; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 360 | console.log(`saving sid ${socket.id} in session ${session.id}`); |
| 361 | session.socketId = socket.id; |
| 362 | session.save(); |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 363 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 364 | socket.on('conversation', (data) => { |
| 365 | console.log('io conversation'); |
| 366 | console.log(data); |
| 367 | if (session.conversation) { |
| 368 | console.log(`disconnect from old conversation ${session.conversation.conversationId}`); |
| 369 | const conversation = jami.getConversation(session.conversation.accountId, session.conversation.conversationId); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 370 | if (conversation) { |
| 371 | delete conversation.listeners[socket.id]; |
| 372 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 373 | } |
| 374 | session.conversation = { accountId: data.accountId, conversationId: data.conversationId }; |
| 375 | const conversation = jami.getConversation(data.accountId, data.conversationId); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 376 | if (conversation) { |
| 377 | if (!conversation.listeners) { |
| 378 | conversation.listeners = {}; |
| 379 | } |
| 380 | conversation.listeners[socket.id] = { |
| 381 | socket, |
| 382 | session, |
| 383 | }; |
| 384 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 385 | session.save(); |
| 386 | }); |
| 387 | }); |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 388 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 389 | return server; |
| 390 | }; |
Adrien Béraud | 3b5d9a6 | 2021-04-17 18:40:27 -0400 | [diff] [blame] | 391 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 392 | loadConfig(configPath) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 393 | .then(createServer) |
| 394 | .then((server) => { |
| 395 | server.listen(3000); |
| 396 | }); |