diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-03-21 21:11:26 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-03-21 21:11:26 +0100 |
commit | 37dc07b292ae4b24011a99146150869bb9c17c65 (patch) | |
tree | 07bd9ade440cc8dd07349a725ba7d38398628d94 | |
parent | 9457bf88079a23d28011ff7c65faa56a548b7817 (diff) | |
download | PeerTube-37dc07b292ae4b24011a99146150869bb9c17c65.tar.gz PeerTube-37dc07b292ae4b24011a99146150869bb9c17c65.tar.zst PeerTube-37dc07b292ae4b24011a99146150869bb9c17c65.zip |
Create an "installer" module that create defaults clients/users...
-rw-r--r-- | server.js | 14 | ||||
-rw-r--r-- | server/initializers/checker.js | 31 | ||||
-rw-r--r-- | server/initializers/installer.js | 94 | ||||
-rw-r--r-- | server/models/users.js | 10 |
4 files changed, 123 insertions, 26 deletions
@@ -21,15 +21,13 @@ if (miss.length !== 0) { | |||
21 | throw new Error('Miss some configurations keys : ' + miss) | 21 | throw new Error('Miss some configurations keys : ' + miss) |
22 | } | 22 | } |
23 | 23 | ||
24 | checker.createDirectoriesIfNotExist() | ||
25 | |||
26 | // ----------- PeerTube modules ----------- | 24 | // ----------- PeerTube modules ----------- |
27 | var config = require('config') | 25 | var config = require('config') |
28 | var constants = require('./server/initializers/constants') | 26 | var constants = require('./server/initializers/constants') |
29 | var customValidators = require('./server/helpers/customValidators') | 27 | var customValidators = require('./server/helpers/customValidators') |
30 | var database = require('./server/initializers/database') | 28 | var database = require('./server/initializers/database') |
29 | var installer = require('./server/initializers/installer') | ||
31 | var logger = require('./server/helpers/logger') | 30 | var logger = require('./server/helpers/logger') |
32 | var peertubeCrypto = require('./server/helpers/peertubeCrypto') | ||
33 | var poolRequests = require('./server/lib/poolRequests') | 31 | var poolRequests = require('./server/lib/poolRequests') |
34 | var routes = require('./server/controllers') | 32 | var routes = require('./server/controllers') |
35 | var utils = require('./server/helpers/utils') | 33 | var utils = require('./server/helpers/utils') |
@@ -119,17 +117,9 @@ app.use(function (err, req, res, next) { | |||
119 | res.sendStatus(err.status || 500) | 117 | res.sendStatus(err.status || 500) |
120 | }) | 118 | }) |
121 | 119 | ||
122 | // TODO: move into initializer | 120 | installer.installApplication(function (err) { |
123 | require('./server/models/users').createClient('coucou', [ 'password' ], function (err, id) { | ||
124 | if (err) throw err | 121 | if (err) throw err |
125 | logger.info('Client id: ' + id) | ||
126 | |||
127 | require('./server/models/users').createUser('floflo', 'coucou', function () {}) | ||
128 | }) | ||
129 | 122 | ||
130 | // ----------- Create the certificates if they don't already exist ----------- | ||
131 | peertubeCrypto.createCertsIfNotExist(function (err) { | ||
132 | if (err) throw err | ||
133 | // Create/activate the webtorrent module | 123 | // Create/activate the webtorrent module |
134 | webtorrent.create(function () { | 124 | webtorrent.create(function () { |
135 | function cleanForExit () { | 125 | function cleanForExit () { |
diff --git a/server/initializers/checker.js b/server/initializers/checker.js index a21e54982..cb62fabf4 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js | |||
@@ -1,12 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const mkdirp = require('mkdirp') | 4 | |
5 | const path = require('path') | 5 | const Users = require('../models/users') |
6 | 6 | ||
7 | const checker = { | 7 | const checker = { |
8 | checkConfig: checkConfig, | 8 | checkConfig: checkConfig, |
9 | createDirectoriesIfNotExist: createDirectoriesIfNotExist | 9 | clientsExist: clientsExist, |
10 | usersExist: usersExist | ||
10 | } | 11 | } |
11 | 12 | ||
12 | // Check the config files | 13 | // Check the config files |
@@ -27,18 +28,20 @@ function checkConfig () { | |||
27 | return miss | 28 | return miss |
28 | } | 29 | } |
29 | 30 | ||
30 | // Create directories for the storage if it doesn't exist | 31 | function clientsExist (callback) { |
31 | function createDirectoriesIfNotExist () { | 32 | Users.getClients(function (err, clients) { |
32 | const storages = config.get('storage') | 33 | if (err) return callback(err) |
33 | 34 | ||
34 | for (const key of Object.keys(storages)) { | 35 | return callback(null, clients.length !== 0) |
35 | const dir = storages[key] | 36 | }) |
36 | try { | 37 | } |
37 | mkdirp.sync(path.join(__dirname, '..', '..', dir)) | 38 | |
38 | } catch (error) { | 39 | function usersExist (callback) { |
39 | throw new Error('Cannot create ' + path + ':' + error) | 40 | Users.getUsers(function (err, users) { |
40 | } | 41 | if (err) return callback(err) |
41 | } | 42 | |
43 | return callback(null, users.length !== 0) | ||
44 | }) | ||
42 | } | 45 | } |
43 | 46 | ||
44 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
diff --git a/server/initializers/installer.js b/server/initializers/installer.js new file mode 100644 index 000000000..750eb2c59 --- /dev/null +++ b/server/initializers/installer.js | |||
@@ -0,0 +1,94 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const async = require('async') | ||
4 | const config = require('config') | ||
5 | const mkdirp = require('mkdirp') | ||
6 | const path = require('path') | ||
7 | |||
8 | const checker = require('./checker') | ||
9 | const logger = require('../helpers/logger') | ||
10 | const peertubeCrypto = require('../helpers/peertubeCrypto') | ||
11 | const Users = require('../models/users') | ||
12 | |||
13 | const installer = { | ||
14 | installApplication: installApplication | ||
15 | } | ||
16 | |||
17 | function installApplication (callback) { | ||
18 | // Creates directories | ||
19 | createDirectoriesIfNotExist(function (err) { | ||
20 | if (err) return callback(err) | ||
21 | |||
22 | // ----------- Create the certificates if they don't already exist ----------- | ||
23 | peertubeCrypto.createCertsIfNotExist(function (err) { | ||
24 | if (err) return callback(err) | ||
25 | |||
26 | createOAuthClientIfNotExist(function (err) { | ||
27 | if (err) return callback(err) | ||
28 | |||
29 | createOAuthUserIfNotExist(callback) | ||
30 | }) | ||
31 | }) | ||
32 | }) | ||
33 | } | ||
34 | |||
35 | // --------------------------------------------------------------------------- | ||
36 | |||
37 | module.exports = installer | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | function createDirectoriesIfNotExist (callback) { | ||
42 | const storages = config.get('storage') | ||
43 | |||
44 | async.each(Object.keys(storages), function (key, callbackEach) { | ||
45 | const dir = storages[key] | ||
46 | mkdirp(path.join(__dirname, '..', '..', dir), callbackEach) | ||
47 | }, callback) | ||
48 | } | ||
49 | |||
50 | function createOAuthClientIfNotExist (callback) { | ||
51 | checker.clientsExist(function (err, exist) { | ||
52 | if (err) return callback(err) | ||
53 | |||
54 | // Nothing to do, clients already exist | ||
55 | if (exist === true) return callback(null) | ||
56 | |||
57 | logger.info('Creating a default OAuth Client.') | ||
58 | |||
59 | // TODO: generate password | ||
60 | const password = 'megustalabanana' | ||
61 | Users.createClient(password, [ 'password' ], function (err, id) { | ||
62 | if (err) return callback(err) | ||
63 | |||
64 | logger.info('Client id: ' + id) | ||
65 | logger.info('Client password: ' + password) | ||
66 | |||
67 | return callback(null) | ||
68 | }) | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | function createOAuthUserIfNotExist (callback) { | ||
73 | checker.usersExist(function (err, exist) { | ||
74 | if (err) return callback(err) | ||
75 | |||
76 | // Nothing to do, users already exist | ||
77 | if (exist === true) return callback(null) | ||
78 | |||
79 | logger.info('Creating the administrator.') | ||
80 | |||
81 | // TODO: generate password | ||
82 | const username = 'admin' | ||
83 | const password = 'nomegustalabanana' | ||
84 | |||
85 | Users.createUser(username, password, function (err) { | ||
86 | if (err) return callback(err) | ||
87 | |||
88 | logger.info('Username: ' + username) | ||
89 | logger.info('User password: ' + password) | ||
90 | |||
91 | return callback(null) | ||
92 | }) | ||
93 | }) | ||
94 | } | ||
diff --git a/server/models/users.js b/server/models/users.js index 355d991bd..367c206ab 100644 --- a/server/models/users.js +++ b/server/models/users.js | |||
@@ -34,8 +34,10 @@ const Users = { | |||
34 | createUser: createUser, | 34 | createUser: createUser, |
35 | getAccessToken: getAccessToken, | 35 | getAccessToken: getAccessToken, |
36 | getClient: getClient, | 36 | getClient: getClient, |
37 | getClients: getClients, | ||
37 | getRefreshToken: getRefreshToken, | 38 | getRefreshToken: getRefreshToken, |
38 | getUser: getUser, | 39 | getUser: getUser, |
40 | getUsers: getUsers, | ||
39 | saveToken: saveToken | 41 | saveToken: saveToken |
40 | } | 42 | } |
41 | 43 | ||
@@ -70,6 +72,10 @@ function getClient (clientId, clientSecret) { | |||
70 | return OAuthClientsDB.findOne({ _id: mongo_id, clientSecret: clientSecret }) | 72 | return OAuthClientsDB.findOne({ _id: mongo_id, clientSecret: clientSecret }) |
71 | } | 73 | } |
72 | 74 | ||
75 | function getClients (callback) { | ||
76 | return OAuthClientsDB.find(callback) | ||
77 | } | ||
78 | |||
73 | function getRefreshToken (refreshToken) { | 79 | function getRefreshToken (refreshToken) { |
74 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') | 80 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') |
75 | 81 | ||
@@ -81,6 +87,10 @@ function getUser (username, password) { | |||
81 | return UsersDB.findOne({ username: username, password: password }) | 87 | return UsersDB.findOne({ username: username, password: password }) |
82 | } | 88 | } |
83 | 89 | ||
90 | function getUsers (callback) { | ||
91 | return UsersDB.find(callback) | ||
92 | } | ||
93 | |||
84 | function saveToken (token, client, user) { | 94 | function saveToken (token, client, user) { |
85 | logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.') | 95 | logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.') |
86 | 96 | ||