]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.js
Implement user API (create, update, remove, list)
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.js
CommitLineData
37dc07b2
C
1'use strict'
2
37dc07b2 3const config = require('config')
1a42c9e2 4const each = require('async/each')
37dc07b2 5const mkdirp = require('mkdirp')
69b0a27c 6const mongoose = require('mongoose')
bb1e6d0c 7const passwordGenerator = require('password-generator')
37dc07b2 8const path = require('path')
1a42c9e2 9const series = require('async/series')
37dc07b2
C
10
11const checker = require('./checker')
9bd26629 12const constants = require('./constants')
37dc07b2 13const logger = require('../helpers/logger')
5f698b82 14const peertubeCrypto = require('../helpers/peertube-crypto')
69b0a27c
C
15
16const Client = mongoose.model('OAuthClient')
17const User = mongoose.model('User')
37dc07b2
C
18
19const installer = {
20 installApplication: installApplication
21}
22
23function installApplication (callback) {
1a42c9e2 24 series([
cefc718d
C
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) {
9bd26629 38 createOAuthAdminIfNotExist(callbackAsync)
cefc718d
C
39 }
40 ], callback)
37dc07b2
C
41}
42
43// ---------------------------------------------------------------------------
44
45module.exports = installer
46
47// ---------------------------------------------------------------------------
48
49function createDirectoriesIfNotExist (callback) {
50 const storages = config.get('storage')
51
1a42c9e2 52 each(Object.keys(storages), function (key, callbackEach) {
37dc07b2
C
53 const dir = storages[key]
54 mkdirp(path.join(__dirname, '..', '..', dir), callbackEach)
55 }, callback)
56}
57
58function 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
bb1e6d0c 67 const secret = passwordGenerator(32, false)
69b0a27c
C
68 const client = new Client({
69 clientSecret: secret,
2f372a86 70 grants: [ 'password', 'refresh_token' ]
69b0a27c
C
71 })
72
73 client.save(function (err, createdClient) {
37dc07b2
C
74 if (err) return callback(err)
75
69b0a27c
C
76 logger.info('Client id: ' + createdClient._id)
77 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2
C
78
79 return callback(null)
80 })
81 })
82}
83
9bd26629 84function createOAuthAdminIfNotExist (callback) {
37dc07b2
C
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
bb1e6d0c 93 const username = 'root'
9bd26629 94 const role = constants.USER_ROLES.ADMIN
d14b3e37
C
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 }
37dc07b2 107
69b0a27c
C
108 const user = new User({
109 username: username,
9bd26629
C
110 password: password,
111 role: role
69b0a27c
C
112 })
113
114 user.save(function (err, createdUser) {
37dc07b2
C
115 if (err) return callback(err)
116
69b0a27c
C
117 logger.info('Username: ' + createdUser.username)
118 logger.info('User password: ' + createdUser.password)
37dc07b2
C
119
120 return callback(null)
121 })
122 })
123}