Skip to content

Commit

Permalink
chore: resolve review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryoverload committed Feb 1, 2025
1 parent 8f2e6fc commit fdc122b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
8 changes: 1 addition & 7 deletions src/models/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Schema, model } from 'mongoose';
import uniqueValidator from 'mongoose-unique-validator';
import { IServer, IServerMethods, ServerModel } from '@/types/mongoose/server';
import type { SystemType } from '@/types/common/token';

const ServerSchema = new Schema<IServer, ServerModel, IServerMethods>({
client_id: String,
Expand All @@ -19,9 +18,4 @@ const ServerSchema = new Schema<IServer, ServerModel, IServerMethods>({

ServerSchema.plugin(uniqueValidator, { message: '{PATH} already in use.' });

export const Server = model<IServer, ServerModel>('Server', ServerSchema);

export const serverDeviceToSystemType: Record<number, SystemType> = {
1: 'WIIU',
2: '3DS'
};
export const Server = model<IServer, ServerModel>('Server', ServerSchema);
6 changes: 3 additions & 3 deletions src/services/grpc/api/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ export async function login(request: LoginRequest): Promise<DeepPartial<LoginRes
if (!username) throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid or missing username');
if (!password) throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid or missing password');

pnid = await getPNIDByUsername(username); // * We know username will never be null here
pnid = await getPNIDByUsername(username);

if (!pnid) throw new ServerError(Status.INVALID_ARGUMENT, 'User not found');

const hashedPassword = nintendoPasswordHash(password, pnid.pid); // * We know password will never be null here
const hashedPassword = nintendoPasswordHash(password, pnid.pid);

if (!bcrypt.compareSync(hashedPassword, pnid.password)) {
throw new ServerError(Status.INVALID_ARGUMENT, 'Password is incorrect');
}
} else {
if (!refreshToken) throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid or missing refresh token');

pnid = await getPNIDByAPIRefreshToken(refreshToken); // * We know refreshToken will never be null here
pnid = await getPNIDByAPIRefreshToken(refreshToken);

if (!pnid) throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid or missing refresh token');
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/nnas/routes/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getServerByClientID, getServerByGameServerID } from '@/database';
import { generateToken, getValueFromHeaders, getValueFromQueryString } from '@/util';
import { NEXAccount } from '@/models/nex-account';
import { TokenOptions } from '@/types/common/token';
import { serverDeviceToSystemType } from '@/models/server';
import { serverDeviceToSystemType } from '@/types/common/token';

const router = express.Router();

Expand Down
5 changes: 5 additions & 0 deletions src/types/common/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export const SystemTypes = {
} as const;
export type SystemType = keyof typeof SystemTypes;

export const serverDeviceToSystemType: Record<number, SystemType> = {
1: 'WIIU',
2: '3DS'
};

export function getSystemTypeFromValue(type: number): SystemType | undefined {
const keys = Object.keys(SystemTypes) as SystemType[];
return keys.find((key) => SystemTypes[key] === type);
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ export function generateOAuthTokens(systemType: SystemType, pnid: HydratedPNIDDo
token_type: 'OAUTH_ACCESS',
pid: pnid.pid,
access_level: pnid.access_level,
expire_time: BigInt(Date.now() + (accessTokenExpiresInSecs * 1000)) // * 1 hour
expire_time: BigInt(Date.now() + (accessTokenExpiresInSecs * 1000))
};

const refreshTokenOptions: TokenOptions = {
system_type: systemType,
token_type: 'OAUTH_REFRESH',
pid: pnid.pid,
access_level: pnid.access_level,
expire_time: BigInt(Date.now() + (refreshTokenExpiresInSecs * 1000)) // * 30 days
expire_time: BigInt(Date.now() + (refreshTokenExpiresInSecs * 1000))
};

const accessToken = generateToken(config.aes_key, accessTokenOptions)?.toString('hex');
Expand Down

0 comments on commit fdc122b

Please sign in to comment.