From 27a78270cc15bf703ba2bf45a235e458fd80ecb4 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Wed, 28 Sep 2022 13:01:57 -0400 Subject: [PATCH] Removed pre-save from PNID --- src/models/pnid.js | 18 ------------------ src/services/api/routes/v1/register.js | 11 ++++++++++- src/services/nnid/routes/people.js | 11 ++++++++++- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/models/pnid.js b/src/models/pnid.js index a1e743e..6719352 100644 --- a/src/models/pnid.js +++ b/src/models/pnid.js @@ -224,24 +224,6 @@ PNIDSchema.methods.getServerMode = function () { return serverMode; }; -PNIDSchema.pre('save', async function(next) { - if (!this.isModified('password')) { - return next(); - } - - this.set('usernameLower', this.get('username').toLowerCase()); - //await this.generatePID(); - await this.generateEmailValidationCode(); - await this.generateEmailValidationToken(); - await this.generateMiiImages(); - - const primaryHash = util.nintendoPasswordHash(this.get('password'), this.get('pid')); - const hash = bcrypt.hashSync(primaryHash, 10); - - this.set('password', hash); - next(); -}); - const PNID = model('PNID', PNIDSchema); module.exports = { diff --git a/src/services/api/routes/v1/register.js b/src/services/api/routes/v1/register.js index b935db7..e5e8fb5 100644 --- a/src/services/api/routes/v1/register.js +++ b/src/services/api/routes/v1/register.js @@ -4,6 +4,7 @@ const fs = require('fs-extra'); const moment = require('moment'); const crypto = require('crypto'); const hcaptcha = require('hcaptcha'); +const bcrypt = require('bcrypt'); const { PNID } = require('../../../../models/pnid'); const { NEXAccount } = require('../../../../models/nex-account'); const database = require('../../../../database'); @@ -253,12 +254,16 @@ router.post('/', async (request, response) => { nexAccount = nexAccountResult[0]; + const primaryPasswordHash = util.nintendoPasswordHash(password, nexAccount.get('pid')); + const passwordHash = await bcrypt.hash(primaryPasswordHash, 10); + const pnidResult = await PNID.create([{ pid: nexAccount.get('pid'), creation_date: creationDate, updated: creationDate, username: username, - password: password, // will be hashed before saving + usernameLower: username.toLowerCase(), + password: passwordHash, birthdate: '1990-01-01', // TODO: Change this gender: 'M', // TODO: Change this country: 'US', // TODO: Change this @@ -298,6 +303,10 @@ router.post('/', async (request, response) => { pnid = pnidResult[0]; + await pnid.generateEmailValidationCode(); + await pnid.generateEmailValidationToken(); + await pnid.generateMiiImages(); + // Quick hack to get the PIDs to match // TODO: Change this // NN with a NNID will always use the NNID PID diff --git a/src/services/nnid/routes/people.js b/src/services/nnid/routes/people.js index b090ffa..6ccebc2 100644 --- a/src/services/nnid/routes/people.js +++ b/src/services/nnid/routes/people.js @@ -2,6 +2,7 @@ const router = require('express').Router(); const xmlbuilder = require('xmlbuilder'); const moment = require('moment'); const crypto = require('crypto'); +const bcrypt = require('bcrypt'); const { PNID } = require('../../../models/pnid'); const { NEXAccount } = require('../../../models/nex-account'); const deviceCertificateMiddleware = require('../../../middleware/device-certificate'); @@ -90,12 +91,16 @@ router.post('/', ratelimit, deviceCertificateMiddleware, async (request, respons nexAccount = nexAccountResult[0]; + const primaryPasswordHash = util.nintendoPasswordHash(person.get('password'), nexAccount.get('pid')); + const passwordHash = await bcrypt.hash(primaryPasswordHash, 10); + const pnidResult = await PNID.create([{ pid: nexAccount.get('pid'), creation_date: creationDate, updated: creationDate, username: person.get('user_id'), - password: person.get('password'), // will be hashed before saving + usernameLower: person.get('user_id').toLowerCase(), + password: passwordHash, birthdate: person.get('birth_date'), gender: person.get('gender'), country: person.get('country'), @@ -135,6 +140,10 @@ router.post('/', ratelimit, deviceCertificateMiddleware, async (request, respons pnid = pnidResult[0]; + await pnid.generateEmailValidationCode(); + await pnid.generateEmailValidationToken(); + await pnid.generateMiiImages(); + // Quick hack to get the PIDs to match // TODO: Change this // NN with a NNID will always use the NNID PID