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