From e4c556196d7b31111f17596840d2e1d60caa7dcb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 31 Jul 2016 20:58:43 +0200 Subject: Server: reorganize express validators --- server/helpers/custom-validators.js | 114 ----------------------------- server/helpers/custom-validators/index.js | 15 ++++ server/helpers/custom-validators/misc.js | 18 +++++ server/helpers/custom-validators/users.js | 18 +++++ server/helpers/custom-validators/videos.js | 106 +++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 114 deletions(-) delete mode 100644 server/helpers/custom-validators.js create mode 100644 server/helpers/custom-validators/index.js create mode 100644 server/helpers/custom-validators/misc.js create mode 100644 server/helpers/custom-validators/users.js create mode 100644 server/helpers/custom-validators/videos.js (limited to 'server/helpers') diff --git a/server/helpers/custom-validators.js b/server/helpers/custom-validators.js deleted file mode 100644 index b666644c0..000000000 --- a/server/helpers/custom-validators.js +++ /dev/null @@ -1,114 +0,0 @@ -'use strict' - -const validator = require('express-validator').validator - -const constants = require('../initializers/constants') -const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS - -const customValidators = { - exists: exists, - isEachRemoteVideosValid: isEachRemoteVideosValid, - isArray: isArray, - isVideoAuthorValid: isVideoAuthorValid, - isVideoDateValid: isVideoDateValid, - isVideoDescriptionValid: isVideoDescriptionValid, - isVideoDurationValid: isVideoDurationValid, - isVideoMagnetUriValid: isVideoMagnetUriValid, - isVideoNameValid: isVideoNameValid, - isVideoPodUrlValid: isVideoPodUrlValid, - isVideoTagsValid: isVideoTagsValid, - isVideoThumbnailValid: isVideoThumbnailValid, - isVideoThumbnail64Valid: isVideoThumbnail64Valid -} - -function exists (value) { - return value !== undefined && value !== null -} - -function isEachRemoteVideosValid (requests) { - return requests.every(function (request) { - const video = request.data - return ( - isRequestTypeAddValid(request.type) && - isVideoAuthorValid(video.author) && - isVideoDateValid(video.createdDate) && - isVideoDescriptionValid(video.description) && - isVideoDurationValid(video.duration) && - isVideoMagnetUriValid(video.magnetUri) && - isVideoNameValid(video.name) && - isVideoPodUrlValid(video.podUrl) && - isVideoTagsValid(video.tags) && - isVideoThumbnail64Valid(video.thumbnailBase64) - ) || - ( - isRequestTypeRemoveValid(request.type) && - isVideoNameValid(video.name) && - isVideoMagnetUriValid(video.magnetUri) - ) - }) -} - -function isArray (value) { - return Array.isArray(value) -} - -function isRequestTypeAddValid (value) { - return value === 'add' -} - -function isRequestTypeRemoveValid (value) { - return value === 'remove' -} - -function isVideoAuthorValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) -} - -function isVideoDateValid (value) { - return validator.isDate(value) -} - -function isVideoDescriptionValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) -} - -function isVideoDurationValid (value) { - return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) -} - -function isVideoMagnetUriValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) -} - -function isVideoNameValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) -} - -function isVideoPodUrlValid (value) { - // TODO: set options (TLD...) - return validator.isURL(value) -} - -function isVideoTagsValid (tags) { - return isArray(tags) && - validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && - tags.every(function (tag) { - return validator.isAlphanumeric(tag) && - validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) - }) -} - -function isVideoThumbnailValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) -} - -function isVideoThumbnail64Valid (value) { - return validator.isBase64(value) && - validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) -} - -// --------------------------------------------------------------------------- - -module.exports = customValidators - -// --------------------------------------------------------------------------- diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js new file mode 100644 index 000000000..ab3066822 --- /dev/null +++ b/server/helpers/custom-validators/index.js @@ -0,0 +1,15 @@ +'use strict' + +const miscValidators = require('./misc') +const usersValidators = require('./users') +const videosValidators = require('./videos') + +const validators = { + misc: miscValidators, + users: usersValidators, + videos: videosValidators +} + +// --------------------------------------------------------------------------- + +module.exports = validators diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js new file mode 100644 index 000000000..782ae3dee --- /dev/null +++ b/server/helpers/custom-validators/misc.js @@ -0,0 +1,18 @@ +'use strict' + +const miscValidators = { + exists: exists, + isArray: isArray +} + +function exists (value) { + return value !== undefined && value !== null +} + +function isArray (value) { + return Array.isArray(value) +} + +// --------------------------------------------------------------------------- + +module.exports = miscValidators diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js new file mode 100644 index 000000000..41e00d046 --- /dev/null +++ b/server/helpers/custom-validators/users.js @@ -0,0 +1,18 @@ +'use strict' + +const validator = require('express-validator').validator + +const constants = require('../../initializers/constants') +const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS + +const usersValidators = { + isUserUsernameValid: isUserUsernameValid +} + +function isUserUsernameValid (value) { + return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME) +} + +// --------------------------------------------------------------------------- + +module.exports = usersValidators diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js new file mode 100644 index 000000000..39a19cbd7 --- /dev/null +++ b/server/helpers/custom-validators/videos.js @@ -0,0 +1,106 @@ +'use strict' + +const validator = require('express-validator').validator + +const constants = require('../../initializers/constants') +const usersValidators = require('./users') +const miscValidators = require('./misc') +const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS + +const videosValidators = { + isEachRemoteVideosValid: isEachRemoteVideosValid, + isVideoAuthorValid: isVideoAuthorValid, + isVideoDateValid: isVideoDateValid, + isVideoDescriptionValid: isVideoDescriptionValid, + isVideoDurationValid: isVideoDurationValid, + isVideoMagnetUriValid: isVideoMagnetUriValid, + isVideoNameValid: isVideoNameValid, + isVideoPodUrlValid: isVideoPodUrlValid, + isVideoTagsValid: isVideoTagsValid, + isVideoThumbnailValid: isVideoThumbnailValid, + isVideoThumbnail64Valid: isVideoThumbnail64Valid +} + +function isEachRemoteVideosValid (requests) { + return requests.every(function (request) { + const video = request.data + return ( + isRequestTypeAddValid(request.type) && + isVideoAuthorValid(video.author) && + isVideoDateValid(video.createdDate) && + isVideoDescriptionValid(video.description) && + isVideoDurationValid(video.duration) && + isVideoMagnetUriValid(video.magnetUri) && + isVideoNameValid(video.name) && + isVideoPodUrlValid(video.podUrl) && + isVideoTagsValid(video.tags) && + isVideoThumbnail64Valid(video.thumbnailBase64) + ) || + ( + isRequestTypeRemoveValid(request.type) && + isVideoNameValid(video.name) && + isVideoMagnetUriValid(video.magnetUri) + ) + }) +} + +function isVideoAuthorValid (value) { + return usersValidators.isUserUsernameValid(usersValidators) +} + +function isVideoDateValid (value) { + return validator.isDate(value) +} + +function isVideoDescriptionValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) +} + +function isVideoDurationValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) +} + +function isVideoMagnetUriValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) +} + +function isVideoNameValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) +} + +function isVideoPodUrlValid (value) { + // TODO: set options (TLD...) + return validator.isURL(value) +} + +function isVideoTagsValid (tags) { + return miscValidators.isArray(tags) && + validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && + tags.every(function (tag) { + return validator.isAlphanumeric(tag) && + validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) + }) +} + +function isVideoThumbnailValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) +} + +function isVideoThumbnail64Valid (value) { + return validator.isBase64(value) && + validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) +} + +// --------------------------------------------------------------------------- + +module.exports = videosValidators + +// --------------------------------------------------------------------------- + +function isRequestTypeAddValid (value) { + return value === 'add' +} + +function isRequestTypeRemoveValid (value) { + return value === 'remove' +} -- cgit v1.2.3 From 9bd2662976a75d3b03364cdbe6419e57c80f99a6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Aug 2016 22:32:36 +0200 Subject: Implement user API (create, update, remove, list) --- server/helpers/custom-validators/users.js | 15 ++++++++++++++- server/helpers/custom-validators/videos.js | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js index 41e00d046..0e92989e5 100644 --- a/server/helpers/custom-validators/users.js +++ b/server/helpers/custom-validators/users.js @@ -1,16 +1,29 @@ 'use strict' const validator = require('express-validator').validator +const values = require('lodash/values') const constants = require('../../initializers/constants') const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS const usersValidators = { + isUserPasswordValid: isUserPasswordValid, + isUserRoleValid: isUserRoleValid, isUserUsernameValid: isUserUsernameValid } +function isUserPasswordValid (value) { + return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) +} + +function isUserRoleValid (value) { + return values(constants.USER_ROLES).indexOf(value) !== -1 +} + function isUserUsernameValid (value) { - return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME) + const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max + const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min + return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) } // --------------------------------------------------------------------------- diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index 39a19cbd7..cffa973f8 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js @@ -45,7 +45,7 @@ function isEachRemoteVideosValid (requests) { } function isVideoAuthorValid (value) { - return usersValidators.isUserUsernameValid(usersValidators) + return usersValidators.isUserUsernameValid(value) } function isVideoDateValid (value) { -- cgit v1.2.3 From e861452fb26553177ad4e32bfb18b4fd8a5b1816 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Aug 2016 21:34:51 +0200 Subject: Server: put config in constants --- server/helpers/logger.js | 10 +++++----- server/helpers/peertube-crypto.js | 29 ++++++++++++++++------------- server/helpers/requests.js | 11 ++--------- 3 files changed, 23 insertions(+), 27 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/logger.js b/server/helpers/logger.js index 8ae90a4b2..590ceaeb6 100644 --- a/server/helpers/logger.js +++ b/server/helpers/logger.js @@ -1,23 +1,23 @@ // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ 'use strict' -const config = require('config') const mkdirp = require('mkdirp') const path = require('path') const winston = require('winston') winston.emitErrs = true -const logDir = path.join(__dirname, '..', '..', config.get('storage.logs')) -const label = config.get('webserver.host') + ':' + config.get('webserver.port') +const constants = require('../initializers/constants') + +const label = constants.CONFIG.WEBSERVER.HOST + ':' + constants.CONFIG.WEBSERVER.PORT // Create the directory if it does not exist -mkdirp.sync(logDir) +mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR) const logger = new winston.Logger({ transports: [ new winston.transports.File({ level: 'debug', - filename: path.join(logDir, 'all-logs.log'), + filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'), handleExceptions: true, json: true, maxsize: 5242880, diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 46dff8d03..ef130ea5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -1,15 +1,13 @@ 'use strict' -const config = require('config') const crypto = require('crypto') const fs = require('fs') const openssl = require('openssl-wrapper') -const path = require('path') const ursa = require('ursa') +const constants = require('../initializers/constants') const logger = require('./logger') -const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) const algorithm = 'aes-256-ctr' const peertubeCrypto = { @@ -17,7 +15,6 @@ const peertubeCrypto = { createCertsIfNotExist: createCertsIfNotExist, decrypt: decrypt, encrypt: encrypt, - getCertDir: getCertDir, sign: sign } @@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) { } function decrypt (key, data, callback) { - fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { + fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { if (err) return callback(err) const myPrivateKey = ursa.createPrivateKey(file) @@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) { }) } -function getCertDir () { - return certDir -} - function sign (data) { - const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) + const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') return signature @@ -85,7 +78,7 @@ module.exports = peertubeCrypto // --------------------------------------------------------------------------- function certsExist (callback) { - fs.exists(certDir + 'peertube.key.pem', function (exists) { + fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { return callback(exists) }) } @@ -99,15 +92,25 @@ function createCerts (callback) { } logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { + + let options = { + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + '2048': false + } + openssl.exec('genrsa', options, function (err) { if (err) { logger.error('Cannot create private key on this pod.') return callback(err) } logger.info('RSA key generated.') + options = { + 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + 'pubout': true, + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' + } logger.info('Manage public key...') - openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { + openssl.exec('rsa', options, function (err) { if (err) { logger.error('Cannot create public key on this pod.') return callback(err) diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 547230adc..f76ff3473 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js @@ -1,16 +1,11 @@ 'use strict' -const config = require('config') const replay = require('request-replay') const request = require('request') const constants = require('../initializers/constants') const peertubeCrypto = require('./peertube-crypto') -const http = config.get('webserver.https') ? 'https' : 'http' -const host = config.get('webserver.host') -const port = config.get('webserver.port') - const requests = { makeRetryRequest: makeRetryRequest, makeSecureRequest: makeSecureRequest @@ -29,8 +24,6 @@ function makeRetryRequest (params, callback) { } function makeSecureRequest (params, callback) { - const myUrl = http + '://' + host + ':' + port - const requestParams = { url: params.toPod.url + params.path } @@ -42,8 +35,8 @@ function makeSecureRequest (params, callback) { // Add signature if it is specified in the params if (params.sign === true) { requestParams.json.signature = { - url: myUrl, - signature: peertubeCrypto.sign(myUrl) + url: constants.CONFIG.WEBSERVER.URL, + signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL) } } -- cgit v1.2.3 From 1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 20 Aug 2016 16:59:25 +0200 Subject: Server: make friends urls come from the request instead of the configuration file --- server/helpers/custom-validators/misc.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js index 782ae3dee..13904ea1b 100644 --- a/server/helpers/custom-validators/misc.js +++ b/server/helpers/custom-validators/misc.js @@ -1,8 +1,11 @@ 'use strict' +const validator = require('express-validator').validator + const miscValidators = { exists: exists, - isArray: isArray + isArray: isArray, + isEachUrl: isEachUrl } function exists (value) { @@ -13,6 +16,12 @@ function isArray (value) { return Array.isArray(value) } +function isEachUrl (urls) { + return urls.every(function (url) { + return validator.isURL(url) + }) +} + // --------------------------------------------------------------------------- module.exports = miscValidators -- cgit v1.2.3 From 6c1a098b4107cc923631d8cd94ed54c184fcec7d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 21 Aug 2016 09:54:46 +0200 Subject: Server: fix remote videos requests validator --- server/helpers/custom-validators/videos.js | 41 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index cffa973f8..ebe927208 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js @@ -22,26 +22,27 @@ const videosValidators = { } function isEachRemoteVideosValid (requests) { - return requests.every(function (request) { - const video = request.data - return ( - isRequestTypeAddValid(request.type) && - isVideoAuthorValid(video.author) && - isVideoDateValid(video.createdDate) && - isVideoDescriptionValid(video.description) && - isVideoDurationValid(video.duration) && - isVideoMagnetUriValid(video.magnetUri) && - isVideoNameValid(video.name) && - isVideoPodUrlValid(video.podUrl) && - isVideoTagsValid(video.tags) && - isVideoThumbnail64Valid(video.thumbnailBase64) - ) || - ( - isRequestTypeRemoveValid(request.type) && - isVideoNameValid(video.name) && - isVideoMagnetUriValid(video.magnetUri) - ) - }) + return miscValidators.isArray(requests) && + requests.every(function (request) { + const video = request.data + return ( + isRequestTypeAddValid(request.type) && + isVideoAuthorValid(video.author) && + isVideoDateValid(video.createdDate) && + isVideoDescriptionValid(video.description) && + isVideoDurationValid(video.duration) && + isVideoMagnetUriValid(video.magnetUri) && + isVideoNameValid(video.name) && + isVideoPodUrlValid(video.podUrl) && + isVideoTagsValid(video.tags) && + isVideoThumbnail64Valid(video.thumbnailBase64) + ) || + ( + isRequestTypeRemoveValid(request.type) && + isVideoNameValid(video.name) && + isVideoMagnetUriValid(video.magnetUri) + ) + }) } function isVideoAuthorValid (value) { -- cgit v1.2.3 From d57d6f2605f4ac4a81f9a8594433bb7b65f108b9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 21 Aug 2016 10:08:40 +0200 Subject: Server: fix makefriends validation and tests --- server/helpers/custom-validators/index.js | 2 ++ server/helpers/custom-validators/misc.js | 11 +---------- server/helpers/custom-validators/pods.js | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 server/helpers/custom-validators/pods.js (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js index ab3066822..96b5b20b9 100644 --- a/server/helpers/custom-validators/index.js +++ b/server/helpers/custom-validators/index.js @@ -1,11 +1,13 @@ 'use strict' const miscValidators = require('./misc') +const podsValidators = require('./pods') const usersValidators = require('./users') const videosValidators = require('./videos') const validators = { misc: miscValidators, + pods: podsValidators, users: usersValidators, videos: videosValidators } diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js index 13904ea1b..782ae3dee 100644 --- a/server/helpers/custom-validators/misc.js +++ b/server/helpers/custom-validators/misc.js @@ -1,11 +1,8 @@ 'use strict' -const validator = require('express-validator').validator - const miscValidators = { exists: exists, - isArray: isArray, - isEachUrl: isEachUrl + isArray: isArray } function exists (value) { @@ -16,12 +13,6 @@ function isArray (value) { return Array.isArray(value) } -function isEachUrl (urls) { - return urls.every(function (url) { - return validator.isURL(url) - }) -} - // --------------------------------------------------------------------------- module.exports = miscValidators diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.js new file mode 100644 index 000000000..28d04a05d --- /dev/null +++ b/server/helpers/custom-validators/pods.js @@ -0,0 +1,21 @@ +'use strict' + +const validator = require('express-validator').validator + +const miscValidators = require('./misc') + +const podsValidators = { + isEachUniqueUrlValid: isEachUniqueUrlValid +} + +function isEachUniqueUrlValid (urls) { + return miscValidators.isArray(urls) && + urls.length !== 0 && + urls.every(function (url) { + return validator.isURL(url) && urls.indexOf(url) === urls.lastIndexOf(url) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = podsValidators -- cgit v1.2.3 From 26d7d31ba3b1d26ea9a51e8626e4a4537867db94 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 25 Aug 2016 17:57:37 +0200 Subject: Server: encrypt password in database --- server/helpers/peertube-crypto.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'server/helpers') diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index ef130ea5c..4783e9965 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -1,5 +1,6 @@ 'use strict' +const bcrypt = require('bcrypt') const crypto = require('crypto') const fs = require('fs') const openssl = require('openssl-wrapper') @@ -12,7 +13,9 @@ const algorithm = 'aes-256-ctr' const peertubeCrypto = { checkSignature: checkSignature, + comparePassword: comparePassword, createCertsIfNotExist: createCertsIfNotExist, + cryptPassword: cryptPassword, decrypt: decrypt, encrypt: encrypt, sign: sign @@ -24,6 +27,14 @@ function checkSignature (publicKey, rawData, hexSignature) { return isValid } +function comparePassword (plainPassword, hashPassword, callback) { + bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { + if (err) return callback(err) + + return callback(null, isPasswordMatch) + }) +} + function createCertsIfNotExist (callback) { certsExist(function (exist) { if (exist === true) { @@ -36,6 +47,16 @@ function createCertsIfNotExist (callback) { }) } +function cryptPassword (password, callback) { + bcrypt.genSalt(constants.BCRYPT_SALT_SIZE, function (err, salt) { + if (err) return callback(err) + + bcrypt.hash(password, salt, function (err, hash) { + return callback(err, hash) + }) + }) +} + function decrypt (key, data, callback) { fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { if (err) return callback(err) -- cgit v1.2.3 From c4403b29ad4db097af528a7f04eea07e0ed320d0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 2 Oct 2016 12:19:02 +0200 Subject: Server: remove useless hash affectations --- server/helpers/custom-validators/misc.js | 4 ++-- server/helpers/custom-validators/pods.js | 2 +- server/helpers/custom-validators/users.js | 6 +++--- server/helpers/custom-validators/videos.js | 22 +++++++++++----------- server/helpers/peertube-crypto.js | 14 +++++++------- server/helpers/requests.js | 4 ++-- server/helpers/utils.js | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js index 782ae3dee..052726241 100644 --- a/server/helpers/custom-validators/misc.js +++ b/server/helpers/custom-validators/misc.js @@ -1,8 +1,8 @@ 'use strict' const miscValidators = { - exists: exists, - isArray: isArray + exists, + isArray } function exists (value) { diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.js index 28d04a05d..40f8b5d0b 100644 --- a/server/helpers/custom-validators/pods.js +++ b/server/helpers/custom-validators/pods.js @@ -5,7 +5,7 @@ const validator = require('express-validator').validator const miscValidators = require('./misc') const podsValidators = { - isEachUniqueUrlValid: isEachUniqueUrlValid + isEachUniqueUrlValid } function isEachUniqueUrlValid (urls) { diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js index 0e92989e5..88fa1592e 100644 --- a/server/helpers/custom-validators/users.js +++ b/server/helpers/custom-validators/users.js @@ -7,9 +7,9 @@ const constants = require('../../initializers/constants') const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS const usersValidators = { - isUserPasswordValid: isUserPasswordValid, - isUserRoleValid: isUserRoleValid, - isUserUsernameValid: isUserUsernameValid + isUserPasswordValid, + isUserRoleValid, + isUserUsernameValid } function isUserPasswordValid (value) { diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index ebe927208..a507ff686 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js @@ -8,17 +8,17 @@ const miscValidators = require('./misc') const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS const videosValidators = { - isEachRemoteVideosValid: isEachRemoteVideosValid, - isVideoAuthorValid: isVideoAuthorValid, - isVideoDateValid: isVideoDateValid, - isVideoDescriptionValid: isVideoDescriptionValid, - isVideoDurationValid: isVideoDurationValid, - isVideoMagnetUriValid: isVideoMagnetUriValid, - isVideoNameValid: isVideoNameValid, - isVideoPodUrlValid: isVideoPodUrlValid, - isVideoTagsValid: isVideoTagsValid, - isVideoThumbnailValid: isVideoThumbnailValid, - isVideoThumbnail64Valid: isVideoThumbnail64Valid + isEachRemoteVideosValid, + isVideoAuthorValid, + isVideoDateValid, + isVideoDescriptionValid, + isVideoDurationValid, + isVideoMagnetUriValid, + isVideoNameValid, + isVideoPodUrlValid, + isVideoTagsValid, + isVideoThumbnailValid, + isVideoThumbnail64Valid } function isEachRemoteVideosValid (requests) { diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 4783e9965..1ff638b04 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -12,13 +12,13 @@ const logger = require('./logger') const algorithm = 'aes-256-ctr' const peertubeCrypto = { - checkSignature: checkSignature, - comparePassword: comparePassword, - createCertsIfNotExist: createCertsIfNotExist, - cryptPassword: cryptPassword, - decrypt: decrypt, - encrypt: encrypt, - sign: sign + checkSignature, + comparePassword, + createCertsIfNotExist, + cryptPassword, + decrypt, + encrypt, + sign } function checkSignature (publicKey, rawData, hexSignature) { diff --git a/server/helpers/requests.js b/server/helpers/requests.js index f76ff3473..95775c981 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js @@ -7,8 +7,8 @@ const constants = require('../initializers/constants') const peertubeCrypto = require('./peertube-crypto') const requests = { - makeRetryRequest: makeRetryRequest, - makeSecureRequest: makeSecureRequest + makeRetryRequest, + makeSecureRequest } function makeRetryRequest (params, callback) { diff --git a/server/helpers/utils.js b/server/helpers/utils.js index a77116e08..9c2d402e3 100644 --- a/server/helpers/utils.js +++ b/server/helpers/utils.js @@ -5,8 +5,8 @@ const crypto = require('crypto') const logger = require('./logger') const utils = { - cleanForExit: cleanForExit, - generateRandomString: generateRandomString + cleanForExit, + generateRandomString } function generateRandomString (size, callback) { -- cgit v1.2.3