From 5f698b82c7055df763f3830882ac5bad1397db23 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 1 Jul 2016 16:22:36 +0200 Subject: Use dashes for filenames --- server/helpers/custom-validators.js | 114 ++++++++++++++++++++++++++++ server/helpers/customValidators.js | 114 ---------------------------- server/helpers/peertube-crypto.js | 147 ++++++++++++++++++++++++++++++++++++ server/helpers/peertubeCrypto.js | 147 ------------------------------------ server/helpers/requests.js | 2 +- 5 files changed, 262 insertions(+), 262 deletions(-) create mode 100644 server/helpers/custom-validators.js delete mode 100644 server/helpers/customValidators.js create mode 100644 server/helpers/peertube-crypto.js delete mode 100644 server/helpers/peertubeCrypto.js (limited to 'server/helpers') diff --git a/server/helpers/custom-validators.js b/server/helpers/custom-validators.js new file mode 100644 index 000000000..b666644c0 --- /dev/null +++ b/server/helpers/custom-validators.js @@ -0,0 +1,114 @@ +'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/customValidators.js b/server/helpers/customValidators.js deleted file mode 100644 index b666644c0..000000000 --- a/server/helpers/customValidators.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/peertube-crypto.js b/server/helpers/peertube-crypto.js new file mode 100644 index 000000000..46dff8d03 --- /dev/null +++ b/server/helpers/peertube-crypto.js @@ -0,0 +1,147 @@ +'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 logger = require('./logger') + +const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) +const algorithm = 'aes-256-ctr' + +const peertubeCrypto = { + checkSignature: checkSignature, + createCertsIfNotExist: createCertsIfNotExist, + decrypt: decrypt, + encrypt: encrypt, + getCertDir: getCertDir, + sign: sign +} + +function checkSignature (publicKey, rawData, hexSignature) { + const crt = ursa.createPublicKey(publicKey) + const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex') + return isValid +} + +function createCertsIfNotExist (callback) { + certsExist(function (exist) { + if (exist === true) { + return callback(null) + } + + createCerts(function (err) { + return callback(err) + }) + }) +} + +function decrypt (key, data, callback) { + fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { + if (err) return callback(err) + + const myPrivateKey = ursa.createPrivateKey(file) + const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8') + const decryptedData = symetricDecrypt(data, decryptedKey) + + return callback(null, decryptedData) + }) +} + +function encrypt (publicKey, data, callback) { + const crt = ursa.createPublicKey(publicKey) + + symetricEncrypt(data, function (err, dataEncrypted) { + if (err) return callback(err) + + const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') + const encrypted = { + data: dataEncrypted.crypted, + key: key + } + + callback(null, encrypted) + }) +} + +function getCertDir () { + return certDir +} + +function sign (data) { + const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) + const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') + + return signature +} + +// --------------------------------------------------------------------------- + +module.exports = peertubeCrypto + +// --------------------------------------------------------------------------- + +function certsExist (callback) { + fs.exists(certDir + 'peertube.key.pem', function (exists) { + return callback(exists) + }) +} + +function createCerts (callback) { + certsExist(function (exist) { + if (exist === true) { + const string = 'Certs already exist.' + logger.warning(string) + return callback(new Error(string)) + } + + logger.info('Generating a RSA key...') + openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { + if (err) { + logger.error('Cannot create private key on this pod.') + return callback(err) + } + logger.info('RSA key generated.') + + logger.info('Manage public key...') + openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { + if (err) { + logger.error('Cannot create public key on this pod.') + return callback(err) + } + + logger.info('Public key managed.') + return callback(null) + }) + }) + }) +} + +function generatePassword (callback) { + crypto.randomBytes(32, function (err, buf) { + if (err) return callback(err) + + callback(null, buf.toString('utf8')) + }) +} + +function symetricDecrypt (text, password) { + const decipher = crypto.createDecipher(algorithm, password) + let dec = decipher.update(text, 'hex', 'utf8') + dec += decipher.final('utf8') + return dec +} + +function symetricEncrypt (text, callback) { + generatePassword(function (err, password) { + if (err) return callback(err) + + const cipher = crypto.createCipher(algorithm, password) + let crypted = cipher.update(text, 'utf8', 'hex') + crypted += cipher.final('hex') + callback(null, { crypted: crypted, password: password }) + }) +} diff --git a/server/helpers/peertubeCrypto.js b/server/helpers/peertubeCrypto.js deleted file mode 100644 index 46dff8d03..000000000 --- a/server/helpers/peertubeCrypto.js +++ /dev/null @@ -1,147 +0,0 @@ -'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 logger = require('./logger') - -const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) -const algorithm = 'aes-256-ctr' - -const peertubeCrypto = { - checkSignature: checkSignature, - createCertsIfNotExist: createCertsIfNotExist, - decrypt: decrypt, - encrypt: encrypt, - getCertDir: getCertDir, - sign: sign -} - -function checkSignature (publicKey, rawData, hexSignature) { - const crt = ursa.createPublicKey(publicKey) - const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex') - return isValid -} - -function createCertsIfNotExist (callback) { - certsExist(function (exist) { - if (exist === true) { - return callback(null) - } - - createCerts(function (err) { - return callback(err) - }) - }) -} - -function decrypt (key, data, callback) { - fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { - if (err) return callback(err) - - const myPrivateKey = ursa.createPrivateKey(file) - const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8') - const decryptedData = symetricDecrypt(data, decryptedKey) - - return callback(null, decryptedData) - }) -} - -function encrypt (publicKey, data, callback) { - const crt = ursa.createPublicKey(publicKey) - - symetricEncrypt(data, function (err, dataEncrypted) { - if (err) return callback(err) - - const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') - const encrypted = { - data: dataEncrypted.crypted, - key: key - } - - callback(null, encrypted) - }) -} - -function getCertDir () { - return certDir -} - -function sign (data) { - const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) - const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') - - return signature -} - -// --------------------------------------------------------------------------- - -module.exports = peertubeCrypto - -// --------------------------------------------------------------------------- - -function certsExist (callback) { - fs.exists(certDir + 'peertube.key.pem', function (exists) { - return callback(exists) - }) -} - -function createCerts (callback) { - certsExist(function (exist) { - if (exist === true) { - const string = 'Certs already exist.' - logger.warning(string) - return callback(new Error(string)) - } - - logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { - if (err) { - logger.error('Cannot create private key on this pod.') - return callback(err) - } - logger.info('RSA key generated.') - - logger.info('Manage public key...') - openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { - if (err) { - logger.error('Cannot create public key on this pod.') - return callback(err) - } - - logger.info('Public key managed.') - return callback(null) - }) - }) - }) -} - -function generatePassword (callback) { - crypto.randomBytes(32, function (err, buf) { - if (err) return callback(err) - - callback(null, buf.toString('utf8')) - }) -} - -function symetricDecrypt (text, password) { - const decipher = crypto.createDecipher(algorithm, password) - let dec = decipher.update(text, 'hex', 'utf8') - dec += decipher.final('utf8') - return dec -} - -function symetricEncrypt (text, callback) { - generatePassword(function (err, password) { - if (err) return callback(err) - - const cipher = crypto.createCipher(algorithm, password) - let crypted = cipher.update(text, 'utf8', 'hex') - crypted += cipher.final('hex') - callback(null, { crypted: crypted, password: password }) - }) -} diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 871342d60..547230adc 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js @@ -5,7 +5,7 @@ const replay = require('request-replay') const request = require('request') const constants = require('../initializers/constants') -const peertubeCrypto = require('./peertubeCrypto') +const peertubeCrypto = require('./peertube-crypto') const http = config.get('webserver.https') ? 'https' : 'http' const host = config.get('webserver.host') -- cgit v1.2.3