diff --git a/src/database.js b/src/database.js index 8d8e317..77cef66 100644 --- a/src/database.js +++ b/src/database.js @@ -79,9 +79,9 @@ async function getUserBearer(token) { const user = await getUserByPID(unpackedToken.pid); if (user) { - const expireTime = Math.floor((Number(unpackedToken.date) / 1000) + 3600); + const expireTime = Math.floor((Number(unpackedToken.expire_time) / 1000)); - if (Math.floor(Date.now()/1000) > expireTime) { + if (Math.floor(Date.now() / 1000) > expireTime) { return null; } } diff --git a/src/services/account/routes/oauth.js b/src/services/account/routes/oauth.js index 55f6506..6c0bca9 100644 --- a/src/services/account/routes/oauth.js +++ b/src/services/account/routes/oauth.js @@ -88,7 +88,7 @@ router.post('/access_token/generate', clientHeaderCheck, async (request, respons token_type: 0x1, // OAuth Access, pid: pnid.get('pid'), title_id: BigInt(parseInt(titleId, 16)), - date: BigInt(Date.now()) + expire_time: BigInt(Date.now() + (3600 * 1000)) }; const refreshTokenOptions = { @@ -96,7 +96,7 @@ router.post('/access_token/generate', clientHeaderCheck, async (request, respons token_type: 0x2, // OAuth Refresh, pid: pnid.get('pid'), title_id: BigInt(parseInt(titleId, 16)), - date: BigInt(Date.now()) + expire_time: BigInt(Date.now() + (3600 * 1000)) }; const accessToken = util.generateToken(cryptoOptions, accessTokenOptions); diff --git a/src/services/account/routes/provider.js b/src/services/account/routes/provider.js index 56c52a0..f65163e 100644 --- a/src/services/account/routes/provider.js +++ b/src/services/account/routes/provider.js @@ -57,7 +57,7 @@ router.get('/service_token/@me', async (request, response) => { token_type: 0x4, // service token, pid: pnid.get('pid'), title_id: BigInt(parseInt(titleId, 16)), - date: BigInt(Date.now()) + expire_time: BigInt(Date.now() + (3600 * 1000)) }; const serviceToken = util.generateToken(cryptoOptions, tokenOptions); @@ -133,7 +133,7 @@ router.get('/nex_token/@me', async (request, response) => { token_type: 0x3, // nex token, pid: pnid.get('pid'), title_id: BigInt(parseInt(titleId, 16)), - date: BigInt(Date.now()) + expire_time: BigInt(Date.now() + (3600 * 1000)) }; const nexUser = await NEXAccount.findOne({ diff --git a/src/util.js b/src/util.js index f3b6f31..8510491 100644 --- a/src/util.js +++ b/src/util.js @@ -41,10 +41,12 @@ function generateToken(cryptoOptions, tokenOptions) { if ([0x1, 0x2].includes(tokenOptions.token_type)) { const cryptoPath = `${__dirname}/../certs/access`; const aesKey = Buffer.from(fs.readFileSync(`${cryptoPath}/aes.key`, { encoding: 'utf8' }), 'hex'); - const dataBuffer = Buffer.alloc(4 + 8); + const dataBuffer = Buffer.alloc(1 + 1 + 4 + 8); - dataBuffer.writeUInt32LE(tokenOptions.pid, 0x0); - dataBuffer.writeBigUInt64LE(tokenOptions.date, 0x4); + dataBuffer.writeUInt8(tokenOptions.system_type, 0x0); + dataBuffer.writeUInt8(tokenOptions.token_type, 0x1); + dataBuffer.writeUInt32LE(tokenOptions.pid, 0x2); + dataBuffer.writeBigUInt64LE(tokenOptions.expire_time, 0x4); const iv = Buffer.alloc(16); const cipher = crypto.createCipheriv('aes-128-cbc', aesKey, iv); @@ -69,7 +71,7 @@ function generateToken(cryptoOptions, tokenOptions) { dataBuffer.writeUInt8(tokenOptions.token_type, 0x1); dataBuffer.writeUInt32LE(tokenOptions.pid, 0x2); dataBuffer.writeBigUInt64LE(tokenOptions.title_id, 0x6); - dataBuffer.writeBigUInt64LE(tokenOptions.date, 0xE); + dataBuffer.writeBigUInt64LE(tokenOptions.expire_time, 0xE); // Calculate the signature of the token body const hmac = crypto.createHmac('sha1', cryptoOptions.hmac_secret).update(dataBuffer); @@ -184,8 +186,10 @@ function decryptToken(token) { function unpackToken(token) { if (token.length <= 32) { return { - pid: token.readUInt32LE(0x0), - date: token.readBigUInt64LE(0x2) + system_type: token.readUInt8(0x0), + token_type: token.readUInt8(0x1), + pid: token.readUInt32LE(0x2), + expire_time: token.readBigUInt64LE(0x6) }; } @@ -194,7 +198,7 @@ function unpackToken(token) { token_type: token.readUInt8(0x1), pid: token.readUInt32LE(0x2), title_id: token.readBigUInt64LE(0x6), - date: token.readBigUInt64LE(0xE) + expire_time: token.readBigUInt64LE(0xE) }; }