XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

SpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 1 Star 0
返回提交历史

XFEstudio/SpaceNinjaServer

fix: avoid spilling new database account fields into login response (#610)

d5c829e4
Sainan <sainan@calamity.inc>
提交于

代码差异

4 个文件 +41 -42
Modified src/controllers/api/loginController.ts +19 -24
@@ -7,11 +7,11 @@ import buildConfig from "@/static/data/buildConfig.json";
7 7 import { toLoginRequest } from "@/src/helpers/loginHelpers";
8 8 import { Account } from "@/src/models/loginModel";
9 9 import { createAccount, isCorrectPassword } from "@/src/services/loginService";
10 import { ILoginResponse } from "@/src/types/loginTypes";
10 import { IDatabaseAccountJson, ILoginResponse } from "@/src/types/loginTypes";
11 11 import { DTLS, groups, HUB, platformCDNs } from "@/static/fixed_responses/login_static";
12 12 import { logger } from "@/src/utils/logger";
13 13
14 const loginController: RequestHandler = async (request, response) => {
14 export const loginController: RequestHandler = async (request, response) => {
15 15 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
16 16 const body = JSON.parse(request.body); // parse octet stream of json data to json object
17 17 const loginRequest = toLoginRequest(body);
@@ -39,21 +39,7 @@ const loginController: RequestHandler = async (request, response) => {
39 39 Nonce: nonce
40 40 });
41 41 logger.debug("created new account");
42 // eslint-disable-next-line @typescript-eslint/no-unused-vars
43 const { email, password, LastLoginDay, ...databaseAccount } = newAccount;
44 const newLoginResponse: ILoginResponse = {
45 ...databaseAccount,
46 Groups: groups,
47 platformCDNs: platformCDNs,
48 NRS: [config.myAddress],
49 DTLS: DTLS,
50 IRC: config.myIrcAddresses ?? [config.myAddress],
51 HUB: HUB,
52 BuildLabel: buildLabel,
53 MatchmakingBuildId: buildConfig.matchmakingBuildId
54 };
55
56 response.json(newLoginResponse);
42 response.json(createLoginResponse(newAccount, buildLabel));
57 43 return;
58 44 } catch (error: unknown) {
59 45 if (error instanceof Error) {
@@ -76,9 +62,22 @@ const loginController: RequestHandler = async (request, response) => {
76 62 }
77 63 await account.save();
78 64
79 const { email, password, LastLoginDay, ...databaseAccount } = account.toJSON();
80 const newLoginResponse: ILoginResponse = {
81 ...databaseAccount,
65 response.json(createLoginResponse(account.toJSON(), buildLabel));
66 };
67
68 const createLoginResponse = (account: IDatabaseAccountJson, buildLabel: string): ILoginResponse => {
69 return {
70 id: account.id,
71 DisplayName: account.DisplayName,
72 CountryCode: account.CountryCode,
73 ClientType: account.ClientType,
74 CrossPlatformAllowed: account.CrossPlatformAllowed,
75 ForceLogoutVersion: account.ForceLogoutVersion,
76 AmazonAuthToken: account.AmazonAuthToken,
77 AmazonRefreshToken: account.AmazonRefreshToken,
78 ConsentNeeded: account.ConsentNeeded,
79 TrackedSettings: account.TrackedSettings,
80 Nonce: account.Nonce,
82 81 Groups: groups,
83 82 platformCDNs: platformCDNs,
84 83 NRS: [config.myAddress],
@@ -88,8 +87,4 @@ const loginController: RequestHandler = async (request, response) => {
88 87 BuildLabel: buildLabel,
89 88 MatchmakingBuildId: buildConfig.matchmakingBuildId
90 89 };
91
92 response.json(newLoginResponse);
93 90 };
94
95 export { loginController };
Modified src/models/loginModel.ts +3 -3
@@ -1,4 +1,4 @@
1 import { IDatabaseAccountDocument } from "@/src/types/loginTypes";
1 import { IDatabaseAccountJson } from "@/src/types/loginTypes";
2 2 import { model, Schema, SchemaOptions } from "mongoose";
3 3
4 4 const opts = {
@@ -20,7 +20,7 @@ const opts = {
20 20 // }
21 21 // }
22 22
23 const databaseAccountSchema = new Schema<IDatabaseAccountDocument>(
23 const databaseAccountSchema = new Schema<IDatabaseAccountJson>(
24 24 {
25 25 email: { type: String, required: true, unique: true },
26 26 password: { type: String, required: true },
@@ -48,4 +48,4 @@ databaseAccountSchema.set("toJSON", {
48 48 virtuals: true
49 49 });
50 50
51 export const Account = model<IDatabaseAccountDocument>("Account", databaseAccountSchema);
51 export const Account = model<IDatabaseAccountJson>("Account", databaseAccountSchema);
Modified src/services/loginService.ts +2 -2
@@ -1,6 +1,6 @@
1 1 import { Account } from "@/src/models/loginModel";
2 2 import { createInventory } from "@/src/services/inventoryService";
3 import { IDatabaseAccount } from "@/src/types/loginTypes";
3 import { IDatabaseAccount, IDatabaseAccountJson } from "@/src/types/loginTypes";
4 4 import { createShip } from "./shipService";
5 5 import { Types } from "mongoose";
6 6 import { Loadout } from "@/src/models/inventoryModels/loadoutModel";
@@ -12,7 +12,7 @@ export const isCorrectPassword = (requestPassword: string, databasePassword: str
12 12 return requestPassword === databasePassword;
13 13 };
14 14
15 export const createAccount = async (accountData: IDatabaseAccount) => {
15 export const createAccount = async (accountData: IDatabaseAccount): Promise<IDatabaseAccountJson> => {
16 16 const account = new Account(accountData);
17 17 try {
18 18 await account.save();
Modified src/types/loginTypes.ts +17 -13
@@ -1,4 +1,18 @@
1 export interface ILoginResponse extends Omit<IDatabaseAccountDocument, "email" | "password"> {
1 export interface IAccountAndLoginResponseCommons {
2 DisplayName: string;
3 CountryCode: string;
4 ClientType: string;
5 CrossPlatformAllowed: boolean;
6 ForceLogoutVersion: number;
7 AmazonAuthToken?: string;
8 AmazonRefreshToken?: string;
9 ConsentNeeded: boolean;
10 TrackedSettings: string[];
11 Nonce: number;
12 }
13
14 export interface ILoginResponse extends IAccountAndLoginResponseCommons {
15 id: string;
2 16 Groups: IGroup[];
3 17 BuildLabel: string;
4 18 MatchmakingBuildId: string;
@@ -10,7 +24,7 @@ export interface ILoginResponse extends Omit<IDatabaseAccountDocument, "email" |
10 24 }
11 25
12 26 // Includes virtual ID
13 export interface IDatabaseAccountDocument extends IDatabaseAccount {
27 export interface IDatabaseAccountJson extends IDatabaseAccount {
14 28 id: string;
15 29 }
16 30
@@ -19,19 +33,9 @@ export interface IGroup {
19 33 experimentGroup: string;
20 34 }
21 35
22 export interface IDatabaseAccount {
36 export interface IDatabaseAccount extends IAccountAndLoginResponseCommons {
23 37 email: string;
24 38 password: string;
25 DisplayName: string;
26 CountryCode: string;
27 ClientType: string;
28 CrossPlatformAllowed: boolean;
29 ForceLogoutVersion: number;
30 AmazonAuthToken?: string;
31 AmazonRefreshToken?: string;
32 ConsentNeeded: boolean;
33 TrackedSettings: string[];
34 Nonce: number;
35 39 LastLoginDay?: number;
36 40 }
37 41