diff options
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/friends.js | 34 | ||||
-rw-r--r-- | server/lib/oauth-model.js | 29 |
2 files changed, 38 insertions, 25 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js index 6e1516b94..556d2e773 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js | |||
@@ -1,6 +1,5 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const each = require('async/each') | 3 | const each = require('async/each') |
5 | const eachLimit = require('async/eachLimit') | 4 | const eachLimit = require('async/eachLimit') |
6 | const eachSeries = require('async/eachSeries') | 5 | const eachSeries = require('async/eachSeries') |
@@ -11,24 +10,20 @@ const waterfall = require('async/waterfall') | |||
11 | 10 | ||
12 | const constants = require('../initializers/constants') | 11 | const constants = require('../initializers/constants') |
13 | const logger = require('../helpers/logger') | 12 | const logger = require('../helpers/logger') |
14 | const peertubeCrypto = require('../helpers/peertube-crypto') | ||
15 | const requests = require('../helpers/requests') | 13 | const requests = require('../helpers/requests') |
16 | 14 | ||
17 | const http = config.get('webserver.https') ? 'https' : 'http' | ||
18 | const host = config.get('webserver.host') | ||
19 | const port = config.get('webserver.port') | ||
20 | const Pod = mongoose.model('Pod') | 15 | const Pod = mongoose.model('Pod') |
21 | const Request = mongoose.model('Request') | 16 | const Request = mongoose.model('Request') |
22 | const Video = mongoose.model('Video') | 17 | const Video = mongoose.model('Video') |
23 | 18 | ||
24 | const friends = { | 19 | const friends = { |
25 | addVideoToFriends: addVideoToFriends, | 20 | addVideoToFriends, |
26 | hasFriends: hasFriends, | 21 | hasFriends, |
27 | getMyCertificate: getMyCertificate, | 22 | getMyCertificate, |
28 | makeFriends: makeFriends, | 23 | makeFriends, |
29 | quitFriends: quitFriends, | 24 | quitFriends, |
30 | removeVideoToFriends: removeVideoToFriends, | 25 | removeVideoToFriends, |
31 | sendOwnedVideosToPod: sendOwnedVideosToPod | 26 | sendOwnedVideosToPod |
32 | } | 27 | } |
33 | 28 | ||
34 | function addVideoToFriends (video) { | 29 | function addVideoToFriends (video) { |
@@ -45,10 +40,10 @@ function hasFriends (callback) { | |||
45 | } | 40 | } |
46 | 41 | ||
47 | function getMyCertificate (callback) { | 42 | function getMyCertificate (callback) { |
48 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback) | 43 | fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) |
49 | } | 44 | } |
50 | 45 | ||
51 | function makeFriends (callback) { | 46 | function makeFriends (urls, callback) { |
52 | const podsScore = {} | 47 | const podsScore = {} |
53 | 48 | ||
54 | logger.info('Make friends!') | 49 | logger.info('Make friends!') |
@@ -58,8 +53,6 @@ function makeFriends (callback) { | |||
58 | return callback(err) | 53 | return callback(err) |
59 | } | 54 | } |
60 | 55 | ||
61 | const urls = config.get('network.friends') | ||
62 | |||
63 | eachSeries(urls, function (url, callbackEach) { | 56 | eachSeries(urls, function (url, callbackEach) { |
64 | computeForeignPodsList(url, podsScore, callbackEach) | 57 | computeForeignPodsList(url, podsScore, callbackEach) |
65 | }, function (err) { | 58 | }, function (err) { |
@@ -205,7 +198,12 @@ function getForeignPodsList (url, callback) { | |||
205 | request.get(url + path, function (err, response, body) { | 198 | request.get(url + path, function (err, response, body) { |
206 | if (err) return callback(err) | 199 | if (err) return callback(err) |
207 | 200 | ||
208 | callback(null, JSON.parse(body)) | 201 | try { |
202 | const json = JSON.parse(body) | ||
203 | return callback(null, json) | ||
204 | } catch (err) { | ||
205 | return callback(err) | ||
206 | } | ||
209 | }) | 207 | }) |
210 | } | 208 | } |
211 | 209 | ||
@@ -220,7 +218,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { | |||
220 | url: pod.url + '/api/' + constants.API_VERSION + '/pods/', | 218 | url: pod.url + '/api/' + constants.API_VERSION + '/pods/', |
221 | method: 'POST', | 219 | method: 'POST', |
222 | json: { | 220 | json: { |
223 | url: http + '://' + host + ':' + port, | 221 | url: constants.CONFIG.WEBSERVER.URL, |
224 | publicKey: cert | 222 | publicKey: cert |
225 | } | 223 | } |
226 | } | 224 | } |
diff --git a/server/lib/oauth-model.js b/server/lib/oauth-model.js index d9f8b175a..45f796796 100644 --- a/server/lib/oauth-model.js +++ b/server/lib/oauth-model.js | |||
@@ -8,12 +8,12 @@ const User = mongoose.model('User') | |||
8 | 8 | ||
9 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications | 9 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications |
10 | const OAuthModel = { | 10 | const OAuthModel = { |
11 | getAccessToken: getAccessToken, | 11 | getAccessToken, |
12 | getClient: getClient, | 12 | getClient, |
13 | getRefreshToken: getRefreshToken, | 13 | getRefreshToken, |
14 | getUser: getUser, | 14 | getUser, |
15 | revokeToken: revokeToken, | 15 | revokeToken, |
16 | saveToken: saveToken | 16 | saveToken |
17 | } | 17 | } |
18 | 18 | ||
19 | // --------------------------------------------------------------------------- | 19 | // --------------------------------------------------------------------------- |
@@ -41,7 +41,22 @@ function getRefreshToken (refreshToken, callback) { | |||
41 | function getUser (username, password) { | 41 | function getUser (username, password) { |
42 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') | 42 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') |
43 | 43 | ||
44 | return User.getByUsernameAndPassword(username, password) | 44 | return User.getByUsername(username).then(function (user) { |
45 | if (!user) return null | ||
46 | |||
47 | // We need to return a promise | ||
48 | return new Promise(function (resolve, reject) { | ||
49 | return user.isPasswordMatch(password, function (err, isPasswordMatch) { | ||
50 | if (err) return reject(err) | ||
51 | |||
52 | if (isPasswordMatch === true) { | ||
53 | return resolve(user) | ||
54 | } | ||
55 | |||
56 | return resolve(null) | ||
57 | }) | ||
58 | }) | ||
59 | }) | ||
45 | } | 60 | } |
46 | 61 | ||
47 | function revokeToken (token) { | 62 | function revokeToken (token) { |