2023-05-26 21:37:12 +08:00
|
|
|
import { RequestHandler } from "express";
|
|
|
|
|
2024-05-15 21:55:59 +02:00
|
|
|
import { config } from "@/src/services/configService";
|
2024-12-30 01:51:38 +01:00
|
|
|
import { buildConfig } from "@/src/services/buildConfigService";
|
2023-05-26 21:37:12 +08:00
|
|
|
|
2023-05-19 15:22:48 -03:00
|
|
|
import { Account } from "@/src/models/loginModel";
|
2024-12-23 22:44:01 +01:00
|
|
|
import { createAccount, isCorrectPassword, isNameTaken } from "@/src/services/loginService";
|
2024-12-29 21:41:39 +01:00
|
|
|
import { IDatabaseAccountJson, ILoginRequest, ILoginResponse } from "@/src/types/loginTypes";
|
2024-01-06 16:26:58 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2024-12-23 00:40:35 +01:00
|
|
|
export const loginController: RequestHandler = async (request, response) => {
|
2024-12-29 21:41:39 +01:00
|
|
|
const loginRequest = JSON.parse(String(request.body)) as ILoginRequest; // parse octet stream of json data to json object
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2025-01-31 14:15:36 +01:00
|
|
|
const account = await Account.findOne({ email: loginRequest.email });
|
2024-05-28 13:45:06 +02:00
|
|
|
const nonce = Math.round(Math.random() * Number.MAX_SAFE_INTEGER);
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2024-12-20 03:11:09 +01:00
|
|
|
const buildLabel: string =
|
|
|
|
typeof request.query.buildLabel == "string"
|
|
|
|
? request.query.buildLabel.split(" ").join("+")
|
|
|
|
: buildConfig.buildLabel;
|
|
|
|
|
2025-03-09 05:45:11 -07:00
|
|
|
const myAddress = request.host.indexOf("warframe.com") == -1 ? request.host : config.myAddress;
|
|
|
|
|
2024-05-06 15:14:09 +02:00
|
|
|
if (!account && config.autoCreateAccount && loginRequest.ClientType != "webui") {
|
2023-05-23 20:42:06 -04:00
|
|
|
try {
|
2024-12-23 22:44:01 +01:00
|
|
|
const nameFromEmail = loginRequest.email.substring(0, loginRequest.email.indexOf("@"));
|
2025-03-26 00:28:35 +01:00
|
|
|
let name = nameFromEmail || "SpaceNinja";
|
2024-12-23 22:44:01 +01:00
|
|
|
if (await isNameTaken(name)) {
|
|
|
|
let suffix = 0;
|
|
|
|
do {
|
|
|
|
++suffix;
|
|
|
|
name = nameFromEmail + suffix;
|
|
|
|
} while (await isNameTaken(name));
|
|
|
|
}
|
2023-05-23 20:42:06 -04:00
|
|
|
const newAccount = await createAccount({
|
|
|
|
email: loginRequest.email,
|
|
|
|
password: loginRequest.password,
|
2024-12-23 22:44:01 +01:00
|
|
|
DisplayName: name,
|
2023-05-23 20:42:06 -04:00
|
|
|
CountryCode: loginRequest.lang.toUpperCase(),
|
|
|
|
ClientType: loginRequest.ClientType,
|
|
|
|
CrossPlatformAllowed: true,
|
|
|
|
ForceLogoutVersion: 0,
|
|
|
|
ConsentNeeded: false,
|
2024-05-28 13:45:06 +02:00
|
|
|
TrackedSettings: [],
|
2025-03-21 05:19:42 -07:00
|
|
|
Nonce: nonce
|
2023-05-23 20:42:06 -04:00
|
|
|
});
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug("created new account");
|
2025-03-09 05:45:11 -07:00
|
|
|
response.json(createLoginResponse(myAddress, newAccount, buildLabel));
|
2023-05-23 20:42:06 -04:00
|
|
|
return;
|
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
2024-02-18 13:58:43 +01:00
|
|
|
throw new Error(`error creating account ${error.message}`);
|
2023-05-23 20:42:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//email not found or incorrect password
|
|
|
|
if (!account || !isCorrectPassword(loginRequest.password, account.password)) {
|
|
|
|
response.status(400).json({ error: "incorrect login data" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-03-09 07:40:37 -07:00
|
|
|
if (loginRequest.ClientType == "webui") {
|
|
|
|
if (!account.Nonce) {
|
|
|
|
account.ClientType = "webui";
|
|
|
|
account.Nonce = nonce;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (account.Nonce && account.ClientType != "webui" && !account.Dropped && !loginRequest.kick) {
|
|
|
|
response.status(400).json({ error: "nonce still set" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
account.ClientType = loginRequest.ClientType;
|
2024-05-28 13:45:06 +02:00
|
|
|
account.Nonce = nonce;
|
2024-06-06 14:34:27 +02:00
|
|
|
account.CountryCode = loginRequest.lang.toUpperCase();
|
|
|
|
}
|
|
|
|
await account.save();
|
2024-05-28 13:45:06 +02:00
|
|
|
|
2025-03-09 05:45:11 -07:00
|
|
|
response.json(createLoginResponse(myAddress, account.toJSON(), buildLabel));
|
2024-12-23 00:40:35 +01:00
|
|
|
};
|
|
|
|
|
2025-03-09 05:45:11 -07:00
|
|
|
const createLoginResponse = (myAddress: string, account: IDatabaseAccountJson, buildLabel: string): ILoginResponse => {
|
2024-12-23 00:40:35 +01:00
|
|
|
return {
|
|
|
|
id: account.id,
|
|
|
|
DisplayName: account.DisplayName,
|
|
|
|
CountryCode: account.CountryCode,
|
|
|
|
ClientType: account.ClientType,
|
|
|
|
CrossPlatformAllowed: account.CrossPlatformAllowed,
|
|
|
|
ForceLogoutVersion: account.ForceLogoutVersion,
|
|
|
|
AmazonAuthToken: account.AmazonAuthToken,
|
|
|
|
AmazonRefreshToken: account.AmazonRefreshToken,
|
|
|
|
ConsentNeeded: account.ConsentNeeded,
|
|
|
|
TrackedSettings: account.TrackedSettings,
|
|
|
|
Nonce: account.Nonce,
|
2025-02-20 06:16:40 -08:00
|
|
|
Groups: [],
|
2025-03-09 05:45:11 -07:00
|
|
|
IRC: config.myIrcAddresses ?? [myAddress],
|
|
|
|
platformCDNs: [`https://${myAddress}/`],
|
|
|
|
HUB: `https://${myAddress}/api/`,
|
2025-02-20 06:16:40 -08:00
|
|
|
NRS: config.NRS,
|
|
|
|
DTLS: 99,
|
2024-12-20 03:11:09 +01:00
|
|
|
BuildLabel: buildLabel,
|
2024-05-15 21:55:59 +02:00
|
|
|
MatchmakingBuildId: buildConfig.matchmakingBuildId
|
2023-05-23 20:42:06 -04:00
|
|
|
};
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|