]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { join } from 'path' |
4d4e5cd4 | 2 | import * as config from 'config' |
4d4e5cd4 | 3 | import * as passwordGenerator from 'password-generator' |
6fcd19ba | 4 | import * as Promise from 'bluebird' |
37dc07b2 | 5 | |
e02643f3 | 6 | import { database as db } from './database' |
65fcc311 C |
7 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' |
8 | import { clientsExist, usersExist } from './checker' | |
6fcd19ba C |
9 | import { logger, createCertsIfNotExist, root, mkdirpPromise } from '../helpers' |
10 | ||
11 | function installApplication () { | |
12 | return db.sequelize.sync() | |
13 | .then(() => createDirectoriesIfNotExist()) | |
14 | .then(() => createCertsIfNotExist()) | |
15 | .then(() => createOAuthClientIfNotExist()) | |
16 | .then(() => createOAuthAdminIfNotExist()) | |
37dc07b2 C |
17 | } |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
65fcc311 C |
21 | export { |
22 | installApplication | |
23 | } | |
37dc07b2 C |
24 | |
25 | // --------------------------------------------------------------------------- | |
26 | ||
6fcd19ba | 27 | function createDirectoriesIfNotExist () { |
37dc07b2 C |
28 | const storages = config.get('storage') |
29 | ||
6fcd19ba C |
30 | const tasks = [] |
31 | Object.keys(storages).forEach(key => { | |
37dc07b2 | 32 | const dir = storages[key] |
6fcd19ba C |
33 | tasks.push(mkdirpPromise(join(root(), dir))) |
34 | }) | |
37dc07b2 | 35 | |
6fcd19ba C |
36 | return Promise.all(tasks) |
37 | } | |
37dc07b2 | 38 | |
6fcd19ba C |
39 | function createOAuthClientIfNotExist () { |
40 | return clientsExist().then(exist => { | |
37dc07b2 | 41 | // Nothing to do, clients already exist |
6fcd19ba | 42 | if (exist === true) return undefined |
37dc07b2 C |
43 | |
44 | logger.info('Creating a default OAuth Client.') | |
45 | ||
feb4bdfd C |
46 | const id = passwordGenerator(32, false, /[a-z0-9]/) |
47 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) | |
48 | const client = db.OAuthClient.build({ | |
49 | clientId: id, | |
69b0a27c | 50 | clientSecret: secret, |
e02643f3 C |
51 | grants: [ 'password', 'refresh_token' ], |
52 | redirectUris: null | |
69b0a27c C |
53 | }) |
54 | ||
6fcd19ba | 55 | return client.save().then(createdClient => { |
feb4bdfd | 56 | logger.info('Client id: ' + createdClient.clientId) |
69b0a27c | 57 | logger.info('Client secret: ' + createdClient.clientSecret) |
37dc07b2 | 58 | |
6fcd19ba | 59 | return undefined |
37dc07b2 C |
60 | }) |
61 | }) | |
62 | } | |
63 | ||
6fcd19ba C |
64 | function createOAuthAdminIfNotExist () { |
65 | return usersExist().then(exist => { | |
37dc07b2 | 66 | // Nothing to do, users already exist |
6fcd19ba | 67 | if (exist === true) return undefined |
37dc07b2 C |
68 | |
69 | logger.info('Creating the administrator.') | |
70 | ||
bb1e6d0c | 71 | const username = 'root' |
65fcc311 C |
72 | const role = USER_ROLES.ADMIN |
73 | const email = CONFIG.ADMIN.EMAIL | |
74 | const createOptions: { validate?: boolean } = {} | |
d14b3e37 C |
75 | let password = '' |
76 | ||
77 | // Do not generate a random password for tests | |
78 | if (process.env.NODE_ENV === 'test') { | |
79 | password = 'test' | |
80 | ||
81 | if (process.env.NODE_APP_INSTANCE) { | |
82 | password += process.env.NODE_APP_INSTANCE | |
83 | } | |
67bf9b96 C |
84 | |
85 | // Our password is weak so do not validate it | |
86 | createOptions.validate = false | |
d14b3e37 C |
87 | } else { |
88 | password = passwordGenerator(8, true) | |
89 | } | |
37dc07b2 | 90 | |
67bf9b96 | 91 | const userData = { |
c4403b29 | 92 | username, |
ad4a8a1c | 93 | email, |
c4403b29 C |
94 | password, |
95 | role | |
67bf9b96 | 96 | } |
69b0a27c | 97 | |
6fcd19ba | 98 | return db.User.create(userData, createOptions).then(createdUser => { |
26d7d31b C |
99 | logger.info('Username: ' + username) |
100 | logger.info('User password: ' + password) | |
37dc07b2 | 101 | |
b769007f | 102 | logger.info('Creating Application table.') |
6fcd19ba | 103 | return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) |
37dc07b2 C |
104 | }) |
105 | }) | |
106 | } |