Renamed user functions to PNID for clarity

This commit is contained in:
Jonathan Barrow
2023-03-19 13:46:29 -04:00
parent b89d3a15b0
commit f977d77c5c
11 changed files with 46 additions and 46 deletions

View File

@@ -43,7 +43,7 @@ export function verifyConnected(): void {
}
}
export async function getUserByUsername(username: string): Promise<HydratedPNIDDocument | null> {
export async function getPNIDByUsername(username: string): Promise<HydratedPNIDDocument | null> {
verifyConnected();
return await PNID.findOne<HydratedPNIDDocument>({
@@ -51,7 +51,7 @@ export async function getUserByUsername(username: string): Promise<HydratedPNIDD
});
}
export async function getUserByPID(pid: number): Promise<HydratedPNIDDocument | null> {
export async function getPNIDByPID(pid: number): Promise<HydratedPNIDDocument | null> {
verifyConnected();
return await PNID.findOne<HydratedPNIDDocument>({
@@ -59,7 +59,7 @@ export async function getUserByPID(pid: number): Promise<HydratedPNIDDocument |
});
}
export async function getUserByEmailAddress(email: string): Promise<HydratedPNIDDocument | null> {
export async function getPNIDByEmailAddress(email: string): Promise<HydratedPNIDDocument | null> {
verifyConnected();
// TODO - Update documents to store email normalized
@@ -68,13 +68,13 @@ export async function getUserByEmailAddress(email: string): Promise<HydratedPNID
});
}
export async function doesUserExist(username: string): Promise<boolean> {
export async function doesPNIDExist(username: string): Promise<boolean> {
verifyConnected();
return !!await getUserByUsername(username);
return !!await getPNIDByUsername(username);
}
export async function getUserBasic(token: string): Promise<HydratedPNIDDocument | null> {
export async function getPNIDByBasicAuth(token: string): Promise<HydratedPNIDDocument | null> {
verifyConnected();
// * Wii U sends Basic auth as `username password`, where the password may not have spaces
@@ -85,7 +85,7 @@ export async function getUserBasic(token: string): Promise<HydratedPNIDDocument
const username: string = parts[0];
const password: string = parts[1];
const pnid: HydratedPNIDDocument | null = await getUserByUsername(username);
const pnid: HydratedPNIDDocument | null = await getPNIDByUsername(username);
if (!pnid) {
return null;
@@ -100,14 +100,14 @@ export async function getUserBasic(token: string): Promise<HydratedPNIDDocument
return pnid;
}
export async function getUserBearer(token: string): Promise<HydratedPNIDDocument | null> {
export async function getPNIDByBearerAuth(token: string): Promise<HydratedPNIDDocument | null> {
verifyConnected();
try {
const decryptedToken: Buffer = await decryptToken(Buffer.from(token, 'base64'));
const unpackedToken: Token = unpackToken(decryptedToken);
const pnid: HydratedPNIDDocument | null = await getUserByPID(unpackedToken.pid);
const pnid: HydratedPNIDDocument | null = await getPNIDByPID(unpackedToken.pid);
if (pnid) {
const expireTime: number = Math.floor((Number(unpackedToken.expire_time) / 1000));
@@ -125,10 +125,10 @@ export async function getUserBearer(token: string): Promise<HydratedPNIDDocument
}
}
export async function getUserProfileJSONByPID(pid: number): Promise<PNIDProfile | null> {
export async function getPNIDProfileJSONByPID(pid: number): Promise<PNIDProfile | null> {
verifyConnected();
const pnid: HydratedPNIDDocument | null = await getUserByPID(pid);
const pnid: HydratedPNIDDocument | null = await getPNIDByPID(pid);
if (!pnid) {
return null;
@@ -208,7 +208,7 @@ export async function getUserProfileJSONByPID(pid: number): Promise<PNIDProfile
};
}
export async function getServer(gameServerId: string, accessMode: string): Promise<HydratedServerDocument | null> {
export async function getServerByGameServerId(gameServerId: string, accessMode: string): Promise<HydratedServerDocument | null> {
return await Server.findOne({
game_server_id: gameServerId,
access_mode: accessMode
@@ -222,13 +222,13 @@ export async function getServerByTitleId(titleId: string, accessMode: string): P
});
}
export async function addUserConnection(pnid: HydratedPNIDDocument, data: ConnectionData, type: string): Promise<ConnectionResponse | undefined> {
export async function addPNIDConnection(pnid: HydratedPNIDDocument, data: ConnectionData, type: string): Promise<ConnectionResponse | undefined> {
if (type === 'discord') {
return await addUserConnectionDiscord(pnid, data);
return await addPNIDConnectionDiscord(pnid, data);
}
}
export async function addUserConnectionDiscord(pnid: HydratedPNIDDocument, data: DiscordConnectionData): Promise<ConnectionResponse> {
export async function addPNIDConnectionDiscord(pnid: HydratedPNIDDocument, data: DiscordConnectionData): Promise<ConnectionResponse> {
const valid: joi.ValidationResult = discordConnectionSchema.validate(data);
if (valid.error) {
@@ -251,7 +251,7 @@ export async function addUserConnectionDiscord(pnid: HydratedPNIDDocument, data:
};
}
export async function removeUserConnection(pnid: HydratedPNIDDocument, type: string): Promise<ConnectionResponse | undefined> {
export async function removePNIDConnection(pnid: HydratedPNIDDocument, type: string): Promise<ConnectionResponse | undefined> {
// * Add more connections later?
if (type === 'discord') {
return await removeUserConnectionDiscord(pnid);

View File

@@ -1,6 +1,6 @@
import express from 'express';
import { getValueFromHeaders } from '@/util';
import { getUserBearer } from '@/database';
import { getPNIDByBearerAuth } from '@/database';
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
async function APIMiddleware(request: express.Request, _response: express.Response, next: express.NextFunction): Promise<void> {
@@ -11,7 +11,7 @@ async function APIMiddleware(request: express.Request, _response: express.Respon
}
const token: string = authHeader.split(' ')[1];
const pnid: HydratedPNIDDocument | null = await getUserBearer(token);
const pnid: HydratedPNIDDocument | null = await getPNIDByBearerAuth(token);
request.pnid = pnid;

View File

@@ -1,7 +1,7 @@
import express from 'express';
import xmlbuilder from 'xmlbuilder';
import { getValueFromHeaders } from '@/util';
import { getUserBasic, getUserBearer } from '@/database';
import { getPNIDByBasicAuth, getPNIDByBearerAuth } from '@/database';
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
async function PNIDMiddleware(request: express.Request, response: express.Response, next: express.NextFunction): Promise<void> {
@@ -21,9 +21,9 @@ async function PNIDMiddleware(request: express.Request, response: express.Respon
}
if (type === 'Basic') {
pnid = await getUserBasic(token);
pnid = await getPNIDByBasicAuth(token);
} else {
pnid = await getUserBearer(token);
pnid = await getPNIDByBearerAuth(token);
}
if (!pnid) {

View File

@@ -1,5 +1,5 @@
import express from 'express';
import { addUserConnection, removeUserConnection } from '@/database';
import { addPNIDConnection, removePNIDConnection } from '@/database';
import { ConnectionData } from '@/types/services/api/connection-data';
import { ConnectionResponse } from '@/types/services/api/connection-response';
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
@@ -44,7 +44,7 @@ router.post('/add/:type', async (request: express.Request, response: express.Res
});
}
let result: ConnectionResponse | undefined = await addUserConnection(pnid, data, type);
let result: ConnectionResponse | undefined = await addPNIDConnection(pnid, data, type);
if (!result) {
result = {
@@ -82,7 +82,7 @@ router.delete('/remove/:type', async (request: express.Request, response: expres
});
}
let result: ConnectionResponse | undefined = await removeUserConnection(pnid, type);
let result: ConnectionResponse | undefined = await removePNIDConnection(pnid, type);
if (!result) {
result = {

View File

@@ -1,6 +1,6 @@
import express from 'express';
import validator from 'validator';
import { getUserByEmailAddress, getUserByUsername } from '@/database';
import { getPNIDByEmailAddress, getPNIDByUsername } from '@/database';
import { sendForgotPasswordEmail } from '@/util';
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
@@ -20,9 +20,9 @@ router.post('/', async (request: express.Request, response: express.Response) =>
let pnid: HydratedPNIDDocument | null;
if (validator.isEmail(input)) {
pnid = await getUserByEmailAddress(input);
pnid = await getPNIDByEmailAddress(input);
} else {
pnid = await getUserByUsername(input);
pnid = await getPNIDByUsername(input);
}
if (pnid) {

View File

@@ -1,7 +1,7 @@
import express from 'express';
import bcrypt from 'bcrypt';
import fs from 'fs-extra';
import { getUserByUsername, getUserBearer } from '@/database';
import { getPNIDByUsername, getPNIDByBearerAuth } from '@/database';
import { getServicePublicKey, getServiceSecretKey } from '@/cache';
import { nintendoPasswordHash, generateToken} from '@/util';
import { CryptoOptions } from '@/types/common/crypto-options';
@@ -57,7 +57,7 @@ router.post('/', async (request: express.Request, response: express.Response) =>
let pnid: HydratedPNIDDocument | null;
if (grantType === 'password') {
pnid = await getUserByUsername(username);
pnid = await getPNIDByUsername(username);
if (!pnid) {
return response.status(400).json({
@@ -77,7 +77,7 @@ router.post('/', async (request: express.Request, response: express.Response) =>
});
}
} else {
pnid = await getUserBearer(refreshToken);
pnid = await getPNIDByBearerAuth(refreshToken);
if (!pnid) {
return response.status(400).json({

View File

@@ -8,7 +8,7 @@ import moment from 'moment';
import hcaptcha from 'hcaptcha';
import Mii from 'mii-js';
import mongoose from 'mongoose';
import { doesUserExist, connection as databaseConnection } from '@/database';
import { doesPNIDExist, connection as databaseConnection } from '@/database';
import { getServicePublicKey, getServiceSecretKey } from '@/cache';
import { nintendoPasswordHash, sendConfirmationEmail, generateToken } from '@/util';
import { LOG_ERROR } from '@/logger';
@@ -141,7 +141,7 @@ router.post('/', async (request: express.Request, response: express.Response) =>
});
}
const userExists: boolean = await doesUserExist(username);
const userExists: boolean = await doesPNIDExist(username);
if (userExists) {
return response.status(400).json({

View File

@@ -2,7 +2,7 @@ import express from 'express';
import xmlbuilder from 'xmlbuilder';
import bcrypt from 'bcrypt';
import fs from 'fs-extra';
import { getUserByUsername } from '@/database';
import { getPNIDByUsername } from '@/database';
import { generateToken } from '@/util';
import { TokenOptions } from '@/types/common/token-options';
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
@@ -52,7 +52,7 @@ router.post('/access_token/generate', async (request: express.Request, response:
}).end());
}
const pnid: HydratedPNIDDocument | null = await getUserByUsername(username);
const pnid: HydratedPNIDDocument | null = await getPNIDByUsername(username);
if (!pnid || !await bcrypt.compare(password, pnid.password)) {
response.status(400);

View File

@@ -6,7 +6,7 @@ import moment from 'moment';
import mongoose from 'mongoose';
import deviceCertificateMiddleware from '@/middleware/device-certificate';
import ratelimit from '@/middleware/ratelimit';
import { connection as databaseConnection, doesUserExist, getUserProfileJSONByPID } from '@/database';
import { connection as databaseConnection, doesPNIDExist, getPNIDProfileJSONByPID } from '@/database';
import { getValueFromHeaders, nintendoPasswordHash, sendConfirmationEmail } from '@/util';
import { PNID } from '@/models/pnid';
import { NEXAccount } from '@/models/nex-account';
@@ -31,7 +31,7 @@ const router: express.Router = express.Router();
router.get('/:username', async (request: express.Request, response: express.Response) => {
const username: string = request.params.username;
const userExists: boolean = await doesUserExist(username);
const userExists: boolean = await doesPNIDExist(username);
if (userExists) {
response.status(400);
@@ -72,7 +72,7 @@ router.post('/', ratelimit, deviceCertificateMiddleware, async (request: express
// * request.body is a Map, as is request.body.person
const person: Person = request.body.get('person');
const userExists: boolean = await doesUserExist(person.get('user_id'));
const userExists: boolean = await doesPNIDExist(person.get('user_id'));
if (userExists) {
response.status(400);
@@ -238,7 +238,7 @@ router.get('/@me/profile', async (request: express.Request, response: express.Re
}).end());
}
const person: PNIDProfile | null = await getUserProfileJSONByPID(pnid.pid);
const person: PNIDProfile | null = await getPNIDProfileJSONByPID(pnid.pid);
if (!person) {
// TODO - Research this error more
@@ -289,7 +289,7 @@ router.post('/@me/devices', async (request: express.Request, response: express.R
}).end());
}
const person: PNIDProfile | null = await getUserProfileJSONByPID(pnid.pid);
const person: PNIDProfile | null = await getPNIDProfileJSONByPID(pnid.pid);
if (!person) {
// TODO - Research this error more
@@ -399,7 +399,7 @@ router.get('/@me/devices/owner', async (request: express.Request, response: expr
}).end());
}
const person: PNIDProfile | null = await getUserProfileJSONByPID(pnid.pid);
const person: PNIDProfile | null = await getPNIDProfileJSONByPID(pnid.pid);
if (!person) {
// TODO - Research this error more

View File

@@ -1,7 +1,7 @@
import express from 'express';
import xmlbuilder from 'xmlbuilder';
import fs from 'fs-extra';
import { getServerByTitleId, getServer } from '@/database';
import { getServerByTitleId, getServerByGameServerId } from '@/database';
import { generateToken, getValueFromHeaders, getValueFromQueryString } from '@/util';
import { getServicePublicKey, getServiceSecretKey, getNEXPublicKey, getNEXSecretKey } from '@/cache';
import { NEXAccount } from '@/models/nex-account';
@@ -148,7 +148,7 @@ router.get('/nex_token/@me', async (request: express.Request, response: express.
}
const serverAccessLevel: string = pnid.server_access_level;
const server: HydratedServerDocument | null = await getServer(gameServerID, serverAccessLevel);
const server: HydratedServerDocument | null = await getServerByGameServerId(gameServerID, serverAccessLevel);
if (!server) {
return response.send(xmlbuilder.create({

View File

@@ -2,7 +2,7 @@ import dns from 'node:dns';
import express from 'express';
import xmlbuilder from 'xmlbuilder';
import moment from 'moment';
import { getUserByPID } from '@/database';
import { getPNIDByPID } from '@/database';
import { sendEmailConfirmedEmail, sendConfirmationEmail, sendForgotPasswordEmail } from '@/util';
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
@@ -56,7 +56,7 @@ router.put('/email_confirmation/:pid/:code', async (request: express.Request, re
const code: string = request.params.code;
const pid: number = Number(request.params.pid);
const pnid: HydratedPNIDDocument | null = await getUserByPID(pid);
const pnid: HydratedPNIDDocument | null = await getPNIDByPID(pid);
if (!pnid) {
return response.status(400).send(xmlbuilder.create({
@@ -101,7 +101,7 @@ router.put('/email_confirmation/:pid/:code', async (request: express.Request, re
router.get('/resend_confirmation', async (request: express.Request, response: express.Response) => {
const pid: number = Number(request.headers['x-nintendo-pid']);
const pnid: HydratedPNIDDocument | null = await getUserByPID(pid);
const pnid: HydratedPNIDDocument | null = await getPNIDByPID(pid);
if (!pnid) {
// TODO - Unsure if this is the right error
@@ -129,7 +129,7 @@ router.get('/resend_confirmation', async (request: express.Request, response: ex
router.get('/forgotten_password/:pid', async (request: express.Request, response: express.Response) => {
const pid: number = Number(request.params.pid);
const pnid: HydratedPNIDDocument | null = await getUserByPID(pid);
const pnid: HydratedPNIDDocument | null = await getPNIDByPID(pid);
if (!pnid) {
// TODO - Better errors