1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
4 import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
5 import { UserModel } from '../models/account/user'
6 import { ApplicationModel } from '../models/application/application'
7 import { OAuthClientModel } from '../models/oauth/oauth-client'
8 import { applicationExist, clientsExist, usersExist } from './checker'
9 import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
12 async function installApplication () {
14 await sequelizeTypescript.sync()
15 await removeCacheDirectories()
16 await createDirectoriesIfNotExist()
17 await createApplicationIfNotExist()
18 await createOAuthClientIfNotExist()
19 await createOAuthAdminIfNotExist()
21 logger.error('Cannot install application.', err)
26 // ---------------------------------------------------------------------------
32 // ---------------------------------------------------------------------------
34 function removeCacheDirectories () {
35 const cacheDirectories = CACHE.DIRECTORIES
37 const tasks: Promise<any>[] = []
40 for (const key of Object.keys(cacheDirectories)) {
41 const dir = cacheDirectories[key]
42 tasks.push(rimrafPromise(dir))
45 return Promise.all(tasks)
48 function createDirectoriesIfNotExist () {
49 const storage = CONFIG.STORAGE
50 const cacheDirectories = CACHE.DIRECTORIES
53 for (const key of Object.keys(storage)) {
54 const dir = storage[key]
55 tasks.push(mkdirpPromise(dir))
59 for (const key of Object.keys(cacheDirectories)) {
60 const dir = cacheDirectories[key]
61 tasks.push(mkdirpPromise(dir))
64 return Promise.all(tasks)
67 async function createOAuthClientIfNotExist () {
68 const exist = await clientsExist()
69 // Nothing to do, clients already exist
70 if (exist === true) return undefined
72 logger.info('Creating a default OAuth Client.')
74 const id = passwordGenerator(32, false, /[a-z0-9]/)
75 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
76 const client = new OAuthClientModel({
79 grants: [ 'password', 'refresh_token' ],
83 const createdClient = await client.save()
84 logger.info('Client id: ' + createdClient.clientId)
85 logger.info('Client secret: ' + createdClient.clientSecret)
90 async function createOAuthAdminIfNotExist () {
91 const exist = await usersExist()
92 // Nothing to do, users already exist
93 if (exist === true) return undefined
95 logger.info('Creating the administrator.')
97 const username = 'root'
98 const role = UserRole.ADMINISTRATOR
99 const email = CONFIG.ADMIN.EMAIL
100 let validatePassword = true
103 // Do not generate a random password for tests
104 if (process.env.NODE_ENV === 'test') {
107 if (process.env.NODE_APP_INSTANCE) {
108 password += process.env.NODE_APP_INSTANCE
111 // Our password is weak so do not validate it
112 validatePassword = false
114 password = passwordGenerator(8, true)
124 const user = new UserModel(userData)
126 await createUserAccountAndChannel(user, validatePassword)
127 logger.info('Username: ' + username)
128 logger.info('User password: ' + password)
131 async function createApplicationIfNotExist () {
132 const exist = await applicationExist()
133 // Nothing to do, application already exist
134 if (exist === true) return undefined
136 logger.info('Creating Application table.')
138 logger.info('Creating application account.')
140 const application = await ApplicationModel.create({
141 migrationVersion: LAST_MIGRATION_VERSION
144 return createApplicationActor(application.id)