diff --git a/src/middleware/nasc.ts b/src/middleware/nasc.ts index 7a79e85..4cc657f 100644 --- a/src/middleware/nasc.ts +++ b/src/middleware/nasc.ts @@ -95,6 +95,17 @@ async function NASCMiddleware(request: express.Request, response: express.Respon return; } + let nexAccount: HydratedNEXAccountDocument | null = null; + if (pid) { + nexAccount = await NEXAccount.findOne({ pid }); + + if (!nexAccount || nexAccount.access_level < 0) { + response.status(200).send(nascError('102').toString()); + return; + } + } + + let device: HydratedDeviceDocument | null = await Device.findOne({ fcdcert_hash: fcdcertHash, }); @@ -108,11 +119,50 @@ async function NASCMiddleware(request: express.Request, response: express.Respon if (pid) { const linkedPIDs: number[] = device.linked_pids; + // * If a user performs a system transfer from + // * a console to another using a Nintendo account + // * during the transfer and both consoles have + // * a Pretendo account, the new device won't have + // * the user's PID. + // * + // * So, the linked PIDs won't have the user's PID + // * anymore. if (!linkedPIDs.includes(pid)) { - response.status(200).send(nascError('102').toString()); - return; + device.linked_pids.push(pid); + + await device.save(); } } + + if (device.serial !== serialNumber) { + response.status(200).send(nascError('102').toString()); + return; + } + + if (device.mac_hash !== macAddressHash) { + response.status(200).send(nascError('102').toString()); + return; + } + } + + // * Workaround for edge case on system transfers + // * if a console that has a Pretendo account performs + // * a system transfer using the Nintendo account to + // * another that doesn't have a Pretendo account. + // * + // * This would make the Pretendo account to not have + // * a device on the database. + if (!device && pid) { + device = new Device({ + model, + serial: serialNumber, + environment, + mac_hash: macAddressHash, + fcdcert_hash: fcdcertHash, + linked_pids: [pid] + }); + + await device.save(); } if (titleID === '0004013000003202') { @@ -124,7 +174,7 @@ async function NASCMiddleware(request: express.Request, response: express.Respon try { // Create new NEX account - const nexAccount: HydratedNEXAccountDocument = new NEXAccount({ + nexAccount = new NEXAccount({ device_type: '3ds', password }); @@ -183,13 +233,6 @@ async function NASCMiddleware(request: express.Request, response: express.Respon } } - const nexAccount: HydratedNEXAccountDocument | null = await NEXAccount.findOne({ pid }); - - if (!nexAccount || nexAccount.access_level < 0) { - response.status(200).send(nascError('102').toString()); - return; - } - request.nexAccount = nexAccount; return next(); @@ -234,4 +277,4 @@ function validNintendoMACAddress(macAddress: string): boolean { return MAC_REGEX.test(macAddress); } -export default NASCMiddleware; \ No newline at end of file +export default NASCMiddleware;