]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const config = require('config') | |
4 | const each = require('async/each') | |
5 | const mkdirp = require('mkdirp') | |
6 | const mongoose = require('mongoose') | |
7 | const passwordGenerator = require('password-generator') | |
8 | const path = require('path') | |
9 | const series = require('async/series') | |
10 | ||
11 | const checker = require('./checker') | |
12 | const constants = require('./constants') | |
13 | const logger = require('../helpers/logger') | |
14 | const peertubeCrypto = require('../helpers/peertube-crypto') | |
15 | ||
16 | const Client = mongoose.model('OAuthClient') | |
17 | const User = mongoose.model('User') | |
18 | ||
19 | const installer = { | |
20 | installApplication: installApplication | |
21 | } | |
22 | ||
23 | function installApplication (callback) { | |
24 | series([ | |
25 | function createDirectories (callbackAsync) { | |
26 | createDirectoriesIfNotExist(callbackAsync) | |
27 | }, | |
28 | ||
29 | function createCertificates (callbackAsync) { | |
30 | peertubeCrypto.createCertsIfNotExist(callbackAsync) | |
31 | }, | |
32 | ||
33 | function createOAuthClient (callbackAsync) { | |
34 | createOAuthClientIfNotExist(callbackAsync) | |
35 | }, | |
36 | ||
37 | function createOAuthUser (callbackAsync) { | |
38 | createOAuthAdminIfNotExist(callbackAsync) | |
39 | } | |
40 | ], callback) | |
41 | } | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
45 | module.exports = installer | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | function createDirectoriesIfNotExist (callback) { | |
50 | const storages = config.get('storage') | |
51 | ||
52 | each(Object.keys(storages), function (key, callbackEach) { | |
53 | const dir = storages[key] | |
54 | mkdirp(path.join(__dirname, '..', '..', dir), callbackEach) | |
55 | }, callback) | |
56 | } | |
57 | ||
58 | function createOAuthClientIfNotExist (callback) { | |
59 | checker.clientsExist(function (err, exist) { | |
60 | if (err) return callback(err) | |
61 | ||
62 | // Nothing to do, clients already exist | |
63 | if (exist === true) return callback(null) | |
64 | ||
65 | logger.info('Creating a default OAuth Client.') | |
66 | ||
67 | const secret = passwordGenerator(32, false) | |
68 | const client = new Client({ | |
69 | clientSecret: secret, | |
70 | grants: [ 'password', 'refresh_token' ] | |
71 | }) | |
72 | ||
73 | client.save(function (err, createdClient) { | |
74 | if (err) return callback(err) | |
75 | ||
76 | logger.info('Client id: ' + createdClient._id) | |
77 | logger.info('Client secret: ' + createdClient.clientSecret) | |
78 | ||
79 | return callback(null) | |
80 | }) | |
81 | }) | |
82 | } | |
83 | ||
84 | function createOAuthAdminIfNotExist (callback) { | |
85 | checker.usersExist(function (err, exist) { | |
86 | if (err) return callback(err) | |
87 | ||
88 | // Nothing to do, users already exist | |
89 | if (exist === true) return callback(null) | |
90 | ||
91 | logger.info('Creating the administrator.') | |
92 | ||
93 | const username = 'root' | |
94 | const role = constants.USER_ROLES.ADMIN | |
95 | let password = '' | |
96 | ||
97 | // Do not generate a random password for tests | |
98 | if (process.env.NODE_ENV === 'test') { | |
99 | password = 'test' | |
100 | ||
101 | if (process.env.NODE_APP_INSTANCE) { | |
102 | password += process.env.NODE_APP_INSTANCE | |
103 | } | |
104 | } else { | |
105 | password = passwordGenerator(8, true) | |
106 | } | |
107 | ||
108 | const user = new User({ | |
109 | username: username, | |
110 | password: password, | |
111 | role: role | |
112 | }) | |
113 | ||
114 | user.save(function (err, createdUser) { | |
115 | if (err) return callback(err) | |
116 | ||
117 | logger.info('Username: ' + username) | |
118 | logger.info('User password: ' + password) | |
119 | ||
120 | return callback(null) | |
121 | }) | |
122 | }) | |
123 | } |