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