diff --git a/models/pnid.js b/models/pnid.js index 19303a5..ba7d264 100644 --- a/models/pnid.js +++ b/models/pnid.js @@ -34,6 +34,10 @@ const PNIDSchema = new mongoose.Schema({ pnid: { key: { type: String // not sure what this should be + }, + pid: { + type: String, + unique: true } }, consoles: [] @@ -48,15 +52,14 @@ function validateEmail(email) { PNIDSchema.plugin(uniqueValidator, {message: '{PATH} already in use.'}); // hashing password -PNIDSchema.pre('save', function(next) { +PNIDSchema.pre('save', async function(next) { // only if modified if (!this.isModified('password')) { return next(); } - - // TODO make security all weird with double hash. // hashing - bcrypt.hash(this.get('password'), 10, (err, hash) => { + const primaryhash = PNIDModel.hashPasswordPrimary(this.get('password'), this.get('pid')); + bcrypt.hash(primaryhash, 10, (err, hash) => { if (err) { return next(err); } @@ -72,6 +75,41 @@ PNIDSchema.statics.findByEmail = function(username) { }); }; +PNIDSchema.statics.hashPasswordPrimary = function(password, pid) { + const buff1 = require('python-struct').pack(' { }); /* -* /admin/api/v1/login +* /api/v1/login * -* signs admin user in +* signs user in * * post { * email @@ -53,24 +53,22 @@ router.post('/api/v1/login', passport.authenticate('PNIDStrategy'), function (re }); /* -* /admin/api/v1/register -* - requires admin auth +* /api/v1/register * * registers a new admin user * * post { -* username - username of new admin account -* password - password of new admin account +* username +* password * } * return { * code: httpcode * success: boolean - true if register was successull -* username: undefined | string - username if register was successfull -* role: undefined | string - role of user if register was successfull +* username: undefined | string - username if register was successfullW * errors: Strings[messages] * } */ -router.post('/api/v1/register', recaptcha.middleware.verify, (req, res) => { +router.post('/api/v1/register', recaptcha.middleware.verify, async (req, res) => { if (!req.body) { // no post body apiHelper.sendApiGenericError(res); @@ -84,7 +82,11 @@ router.post('/api/v1/register', recaptcha.middleware.verify, (req, res) => { const { email, password } = req.body; const newUser = new PNID.PNIDModel({ email, - password + password, + pnid: { + key: 'abcd', + pid: PNID.PNIDModel.generatePID() + } }); // TODO verify password