improve: authenticate addItem requests (#242)

This commit is contained in:
Sainan 2024-05-30 13:32:28 +02:00 committed by GitHub
parent 02e4562daa
commit bc21a4d282
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 12 deletions

View File

@ -1,3 +1,4 @@
import { getAccountIdForRequest } from "@/src/services/loginService";
import { ItemType, toAddItemRequest } from "@/src/helpers/customHelpers/addItemHelpers"; import { ItemType, toAddItemRequest } from "@/src/helpers/customHelpers/addItemHelpers";
import { getWeaponType } from "@/src/services/itemDataService"; import { getWeaponType } from "@/src/services/itemDataService";
import { addPowerSuit, addWeapon } from "@/src/services/inventoryService"; import { addPowerSuit, addWeapon } from "@/src/services/inventoryService";
@ -5,16 +6,17 @@ import { RequestHandler } from "express";
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
const addItemController: RequestHandler = async (req, res) => { const addItemController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const request = toAddItemRequest(req.body); const request = toAddItemRequest(req.body);
switch (request.type) { switch (request.type) {
case ItemType.Powersuit: case ItemType.Powersuit:
const powersuit = await addPowerSuit(request.InternalName, request.accountId); const powersuit = await addPowerSuit(request.InternalName, accountId);
res.json(powersuit); res.json(powersuit);
return; return;
case ItemType.Weapon: case ItemType.Weapon:
const weaponType = getWeaponType(request.InternalName); const weaponType = getWeaponType(request.InternalName);
const weapon = await addWeapon(weaponType, request.InternalName, request.accountId); const weapon = await addWeapon(weaponType, request.InternalName, accountId);
res.json(weapon); res.json(weapon);
break; break;
default: default:

View File

@ -21,7 +21,6 @@ const parseItemType = (itemType: unknown): ItemType => {
interface IAddItemRequest { interface IAddItemRequest {
type: ItemType; type: ItemType;
InternalName: string; InternalName: string;
accountId: string;
} }
export const isInternalItemName = (internalName: string): boolean => { export const isInternalItemName = (internalName: string): boolean => {
const item = items.find(i => i.uniqueName === internalName); const item = items.find(i => i.uniqueName === internalName);
@ -41,11 +40,10 @@ export const toAddItemRequest = (body: unknown): IAddItemRequest => {
throw new Error("incorrect or missing add item request data"); throw new Error("incorrect or missing add item request data");
} }
if ("type" in body && "internalName" in body && "accountId" in body) { if ("type" in body && "internalName" in body) {
return { return {
type: parseItemType(body.type), type: parseItemType(body.type),
InternalName: parseInternalItemName(body.internalName), InternalName: parseInternalItemName(body.internalName)
accountId: parseString(body.accountId)
}; };
} }

View File

@ -266,12 +266,11 @@ function doAcquireWarframe() {
} }
revalidateAuthz(() => { revalidateAuthz(() => {
const req = $.post({ const req = $.post({
url: "/custom/addItem", url: "/custom/addItem?" + window.authz,
contentType: "application/json", contentType: "application/json",
data: JSON.stringify({ data: JSON.stringify({
type: "Powersuit", type: "Powersuit",
internalName: uniqueName, internalName: uniqueName
accountId: window.accountId
}) })
}); });
req.done(() => { req.done(() => {
@ -293,12 +292,11 @@ function doAcquireWeapon() {
} }
revalidateAuthz(() => { revalidateAuthz(() => {
const req = $.post({ const req = $.post({
url: "/custom/addItem", url: "/custom/addItem?" + window.authz,
contentType: "application/json", contentType: "application/json",
data: JSON.stringify({ data: JSON.stringify({
type: "Weapon", type: "Weapon",
internalName: uniqueName, internalName: uniqueName
accountId: window.accountId
}) })
}); });
req.done(() => { req.done(() => {