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