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

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Net.Mail;
using Newtonsoft.Json.Linq;

namespace XFEWarframe.Core.Services;

public sealed partial class PlayerPortal
{
    private ApiResponse EditIdentity(JObject state, JObject account, JObject body)
    {
        var action = body.RequiredString("action");
        if (!Accounts.VerifyPassword(Accounts.GamePassword(body.RequiredString("currentPassword", 256)), account.RequiredString("credential"))) throw new ApiException(403, "当前密码不正确。");
        switch (action)
        {
            case "name":
                var name = body.RequiredString("value", 24).Trim();
                if (name.Length == 0 || name == "all" || name.Any(char.IsControl)) throw new ApiException(400, "游戏昵称无效。");
                if (Accounts.All(state).Any(x => x.Value<string>("id") != account.Value<string>("id") && x.Value<string>("DisplayName") == name)) throw new ApiException(409, "此昵称已被使用。");
                account["DisplayName"] = name; break;
            case "email":
                var email = body.RequiredString("value", 254).Trim().ToLowerInvariant();
                if (!MailAddress.TryCreate(email, out var address) || address.Address != email) throw new ApiException(400, "邮箱地址无效。");
                if (Accounts.All(state).Any(x => x.Value<string>("id") != account.Value<string>("id") && x.Value<string>("email") == email)) throw new ApiException(409, "此邮箱已被使用。");
                account["email"] = email; break;
            case "password":
                var password = body.RequiredString("value", 256);
                if (password.Length < 8) throw new ApiException(400, "新密码至少需要 8 位。");
                account["credential"] = Accounts.HashPassword(Accounts.GamePassword(password)); account["Nonce"] = 0; break;
            case "delete":
                if (body.Value<string>("value") != account.Value<string>("DisplayName")) throw new ApiException(400, "请输入当前游戏昵称以确认删除。");
                CustomApi.Delete(state, account); break;
            default: throw new ApiException(400, "未知账号操作。");
        }
        if (action != "name") foreach (var session in sessions.Where(x => x.Value.AccountId == account.Value<string>("id")).ToArray()) sessions.TryRemove(session.Key, out _);
        return ApiResponse.Json(new { deleted = action == "delete", reauthenticate = action is "email" or "password" });
    }
}