]>
Commit | Line | Data |
---|---|---|
1 | import { join } from 'path' | |
2 | import * as config from 'config' | |
3 | import * as passwordGenerator from 'password-generator' | |
4 | import * as Promise from 'bluebird' | |
5 | ||
6 | import { database as db } from './database' | |
7 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' | |
8 | import { clientsExist, usersExist } from './checker' | |
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()) | |
17 | } | |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
21 | export { | |
22 | installApplication | |
23 | } | |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
27 | function createDirectoriesIfNotExist () { | |
28 | const storages = config.get('storage') | |
29 | ||
30 | const tasks = [] | |
31 | Object.keys(storages).forEach(key => { | |
32 | const dir = storages[key] | |
33 | tasks.push(mkdirpPromise(join(root(), dir))) | |
34 | }) | |
35 | ||
36 | return Promise.all(tasks) | |
37 | } | |
38 | ||
39 | function createOAuthClientIfNotExist () { | |
40 | return clientsExist().then(exist => { | |
41 | // Nothing to do, clients already exist | |
42 | if (exist === true) return undefined | |
43 | ||
44 | logger.info('Creating a default OAuth Client.') | |
45 | ||
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, | |
50 | clientSecret: secret, | |
51 | grants: [ 'password', 'refresh_token' ], | |
52 | redirectUris: null | |
53 | }) | |
54 | ||
55 | return client.save().then(createdClient => { | |
56 | logger.info('Client id: ' + createdClient.clientId) | |
57 | logger.info('Client secret: ' + createdClient.clientSecret) | |
58 | ||
59 | return undefined | |
60 | }) | |
61 | }) | |
62 | } | |
63 | ||
64 | function createOAuthAdminIfNotExist () { | |
65 | return usersExist().then(exist => { | |
66 | // Nothing to do, users already exist | |
67 | if (exist === true) return undefined | |
68 | ||
69 | logger.info('Creating the administrator.') | |
70 | ||
71 | const username = 'root' | |
72 | const role = USER_ROLES.ADMIN | |
73 | const email = CONFIG.ADMIN.EMAIL | |
74 | const createOptions: { validate?: boolean } = {} | |
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 | } | |
84 | ||
85 | // Our password is weak so do not validate it | |
86 | createOptions.validate = false | |
87 | } else { | |
88 | password = passwordGenerator(8, true) | |
89 | } | |
90 | ||
91 | const userData = { | |
92 | username, | |
93 | email, | |
94 | password, | |
95 | role | |
96 | } | |
97 | ||
98 | return db.User.create(userData, createOptions).then(createdUser => { | |
99 | logger.info('Username: ' + username) | |
100 | logger.info('User password: ' + password) | |
101 | ||
102 | logger.info('Creating Application table.') | |
103 | return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) | |
104 | }) | |
105 | }) | |
106 | } |