diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-25 17:57:37 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-25 17:57:37 +0200 |
commit | 26d7d31ba3b1d26ea9a51e8626e4a4537867db94 (patch) | |
tree | 988da4baa1397aaaf46a3c131918257fee4bc34f /server/models | |
parent | f84a89f0e7e9595d2b6f6dd59181c01f562a4239 (diff) | |
download | PeerTube-26d7d31ba3b1d26ea9a51e8626e4a4537867db94.tar.gz PeerTube-26d7d31ba3b1d26ea9a51e8626e4a4537867db94.tar.zst PeerTube-26d7d31ba3b1d26ea9a51e8626e4a4537867db94.zip |
Server: encrypt password in database
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/user.js | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/server/models/user.js b/server/models/user.js index c9c35b3e2..e76aab2ce 100644 --- a/server/models/user.js +++ b/server/models/user.js | |||
@@ -2,6 +2,7 @@ const mongoose = require('mongoose') | |||
2 | 2 | ||
3 | const customUsersValidators = require('../helpers/custom-validators').users | 3 | const customUsersValidators = require('../helpers/custom-validators').users |
4 | const modelUtils = require('./utils') | 4 | const modelUtils = require('./utils') |
5 | const peertubeCrypto = require('../helpers/peertube-crypto') | ||
5 | 6 | ||
6 | // --------------------------------------------------------------------------- | 7 | // --------------------------------------------------------------------------- |
7 | 8 | ||
@@ -20,27 +21,53 @@ UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | |||
20 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | 21 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) |
21 | 22 | ||
22 | UserSchema.methods = { | 23 | UserSchema.methods = { |
24 | isPasswordMatch: isPasswordMatch, | ||
23 | toFormatedJSON: toFormatedJSON | 25 | toFormatedJSON: toFormatedJSON |
24 | } | 26 | } |
25 | 27 | ||
26 | UserSchema.statics = { | 28 | UserSchema.statics = { |
27 | countTotal: countTotal, | 29 | countTotal: countTotal, |
28 | getByUsernameAndPassword: getByUsernameAndPassword, | 30 | getByUsername: getByUsername, |
29 | listForApi: listForApi, | 31 | listForApi: listForApi, |
30 | loadById: loadById, | 32 | loadById: loadById, |
31 | loadByUsername: loadByUsername | 33 | loadByUsername: loadByUsername |
32 | } | 34 | } |
33 | 35 | ||
36 | UserSchema.pre('save', function (next) { | ||
37 | const user = this | ||
38 | |||
39 | peertubeCrypto.cryptPassword(this.password, function (err, hash) { | ||
40 | if (err) return next(err) | ||
41 | |||
42 | user.password = hash | ||
43 | |||
44 | return next() | ||
45 | }) | ||
46 | }) | ||
47 | |||
34 | mongoose.model('User', UserSchema) | 48 | mongoose.model('User', UserSchema) |
35 | 49 | ||
36 | // --------------------------------------------------------------------------- | 50 | // ------------------------------ METHODS ------------------------------ |
51 | |||
52 | function isPasswordMatch (password, callback) { | ||
53 | return peertubeCrypto.comparePassword(password, this.password, callback) | ||
54 | } | ||
55 | |||
56 | function toFormatedJSON () { | ||
57 | return { | ||
58 | id: this._id, | ||
59 | username: this.username, | ||
60 | role: this.role | ||
61 | } | ||
62 | } | ||
63 | // ------------------------------ STATICS ------------------------------ | ||
37 | 64 | ||
38 | function countTotal (callback) { | 65 | function countTotal (callback) { |
39 | return this.count(callback) | 66 | return this.count(callback) |
40 | } | 67 | } |
41 | 68 | ||
42 | function getByUsernameAndPassword (username, password) { | 69 | function getByUsername (username) { |
43 | return this.findOne({ username: username, password: password }) | 70 | return this.findOne({ username: username }) |
44 | } | 71 | } |
45 | 72 | ||
46 | function listForApi (start, count, sort, callback) { | 73 | function listForApi (start, count, sort, callback) { |
@@ -55,11 +82,3 @@ function loadById (id, callback) { | |||
55 | function loadByUsername (username, callback) { | 82 | function loadByUsername (username, callback) { |
56 | return this.findOne({ username: username }, callback) | 83 | return this.findOne({ username: username }, callback) |
57 | } | 84 | } |
58 | |||
59 | function toFormatedJSON () { | ||
60 | return { | ||
61 | id: this._id, | ||
62 | username: this.username, | ||
63 | role: this.role | ||
64 | } | ||
65 | } | ||