From 2e5e692cefc9f2470fd5d54681b9cbc3fb5c1f90 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Sat, 21 Jun 2025 17:27:49 -0400 Subject: [PATCH] fix: update remaining magic numbers for system/token types --- src/database.ts | 3 ++- src/middleware/pnid.ts | 3 ++- src/util.ts | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/database.ts b/src/database.ts index 42ed024..77d93eb 100644 --- a/src/database.ts +++ b/src/database.ts @@ -13,6 +13,7 @@ import type { PNIDProfile } from '@/types/services/nnas/pnid-profile'; import type { ConnectionData } from '@/types/services/api/connection-data'; import type { ConnectionResponse } from '@/types/services/api/connection-response'; import type { DiscordConnectionData } from '@/types/services/api/discord-connection-data'; +import type { SystemType } from '@/types/common/system-types'; const connection_string = config.mongoose.connection_string; const options = config.mongoose.options; @@ -104,7 +105,7 @@ export async function getPNIDByBasicAuth(token: string): Promise { +export async function getPNIDByTokenAuth(token: string, allowedTypes?: SystemType[]): Promise { verifyConnected(); try { diff --git a/src/middleware/pnid.ts b/src/middleware/pnid.ts index e0c05f0..de09c8b 100644 --- a/src/middleware/pnid.ts +++ b/src/middleware/pnid.ts @@ -1,4 +1,5 @@ import xmlbuilder from 'xmlbuilder'; +import { SystemType } from '@/types/common/system-types'; import { getValueFromHeaders } from '@/util'; import { getPNIDByBasicAuth, getPNIDByTokenAuth } from '@/database'; import type express from 'express'; @@ -24,7 +25,7 @@ async function PNIDMiddleware(request: express.Request, response: express.Respon pnid = await getPNIDByBasicAuth(token); } else if (type === 'Bearer') { // TODO - This "accepted types list" is mostly a hack. Change this - pnid = await getPNIDByTokenAuth(token, [1, 2]); + pnid = await getPNIDByTokenAuth(token, [SystemType.WUP, SystemType.CTR]); } if (!pnid) { diff --git a/src/util.ts b/src/util.ts index 6cdce5c..19a80c2 100644 --- a/src/util.ts +++ b/src/util.ts @@ -64,7 +64,7 @@ export function generateToken(key: string, options: TokenOptions): Buffer | null dataBuffer.writeUInt32LE(options.pid, 0x2); dataBuffer.writeBigUInt64LE(options.expire_time, 0x6); - if ((options.token_type !== 0x1 && options.token_type !== 0x2) || options.system_type === 0x3) { + if ((options.token_type !== TokenType.OAuthAccess && options.token_type !== TokenType.OAuthRefresh) || options.system_type === SystemType.API) { // * Access and refresh tokens have smaller bodies due to size constraints // * The API does not have this restraint, however if (options.title_id === undefined || options.access_level === undefined) { @@ -90,7 +90,7 @@ export function generateToken(key: string, options: TokenOptions): Buffer | null let final = encrypted; - if ((options.token_type !== 0x1 && options.token_type !== 0x2) || options.system_type === 0x3) { + if ((options.token_type !== TokenType.OAuthAccess && options.token_type !== TokenType.OAuthRefresh) || options.system_type === SystemType.API) { // * Access and refresh tokens don't get a checksum due to size constraints const checksum = bufferCrc32(dataBuffer); @@ -142,7 +142,7 @@ export function unpackToken(token: Buffer): Token { expire_time: token.readBigUInt64LE(0x6) }; - if (unpacked.token_type !== 0x1 && unpacked.token_type !== 0x2) { + if (unpacked.token_type !== TokenType.OAuthAccess && unpacked.token_type !== TokenType.OAuthRefresh) { unpacked.title_id = token.readBigUInt64LE(0xE); unpacked.access_level = token.readInt8(0x16); }