},
"dependencies": {
"async": "^2.0.0",
+ "bcrypt": "^0.8.7",
"bittorrent-tracker": "^8.0.0",
"body-parser": "^1.12.4",
"concurrently": "^2.0.0",
'use strict'
+const bcrypt = require('bcrypt')
const crypto = require('crypto')
const fs = require('fs')
const openssl = require('openssl-wrapper')
const peertubeCrypto = {
checkSignature: checkSignature,
+ comparePassword: comparePassword,
createCertsIfNotExist: createCertsIfNotExist,
+ cryptPassword: cryptPassword,
decrypt: decrypt,
encrypt: encrypt,
sign: sign
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) {
})
}
+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)
// API version of our pod
const API_VERSION = 'v1'
+const BCRYPT_SALT_SIZE = 10
+
const CONFIG = {
DATABASE: {
DBNAME: 'peertube' + config.get('database.suffix'),
module.exports = {
API_VERSION: API_VERSION,
+ BCRYPT_SALT_SIZE: BCRYPT_SALT_SIZE,
CONFIG: CONFIG,
CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
FRIEND_SCORE: FRIEND_SCORE,
user.save(function (err, createdUser) {
if (err) return callback(err)
- logger.info('Username: ' + createdUser.username)
- logger.info('User password: ' + createdUser.password)
+ logger.info('Username: ' + username)
+ logger.info('User password: ' + password)
return callback(null)
})
function getUser (username, password) {
logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
- return User.getByUsernameAndPassword(username, password)
+ return User.getByUsername(username).then(function (user) {
+ if (!user) return null
+
+ // We need to return a promise
+ return new Promise(function (resolve, reject) {
+ return user.isPasswordMatch(password, function (err, isPasswordMatch) {
+ if (err) return reject(err)
+
+ if (isPasswordMatch === true) {
+ return resolve(user)
+ }
+
+ return resolve(null)
+ })
+ })
+ })
}
function revokeToken (token) {
const customUsersValidators = require('../helpers/custom-validators').users
const modelUtils = require('./utils')
+const peertubeCrypto = require('../helpers/peertube-crypto')
// ---------------------------------------------------------------------------
UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
UserSchema.methods = {
+ isPasswordMatch: isPasswordMatch,
toFormatedJSON: toFormatedJSON
}
UserSchema.statics = {
countTotal: countTotal,
- getByUsernameAndPassword: getByUsernameAndPassword,
+ getByUsername: getByUsername,
listForApi: listForApi,
loadById: loadById,
loadByUsername: loadByUsername
}
+UserSchema.pre('save', function (next) {
+ const user = this
+
+ peertubeCrypto.cryptPassword(this.password, function (err, hash) {
+ if (err) return next(err)
+
+ user.password = hash
+
+ return next()
+ })
+})
+
mongoose.model('User', UserSchema)
-// ---------------------------------------------------------------------------
+// ------------------------------ METHODS ------------------------------
+
+function isPasswordMatch (password, callback) {
+ return peertubeCrypto.comparePassword(password, this.password, callback)
+}
+
+function toFormatedJSON () {
+ return {
+ id: this._id,
+ username: this.username,
+ role: this.role
+ }
+}
+// ------------------------------ STATICS ------------------------------
function countTotal (callback) {
return this.count(callback)
}
-function getByUsernameAndPassword (username, password) {
- return this.findOne({ username: username, password: password })
+function getByUsername (username) {
+ return this.findOne({ username: username })
}
function listForApi (start, count, sort, callback) {
function loadByUsername (username, callback) {
return this.findOne({ username: username }, callback)
}
-
-function toFormatedJSON () {
- return {
- id: this._id,
- username: this.username,
- role: this.role
- }
-}