mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using RadicadeServer.RecNet.Enums.Players;
|
||||
|
||||
namespace RadicadeServer.Extensions
|
||||
{
|
||||
public static class EnumExtension
|
||||
{
|
||||
public static DeviceClass GetDeviceClass(this Platforms platform)
|
||||
{
|
||||
switch (platform)
|
||||
{
|
||||
case Platforms.All:
|
||||
return DeviceClass.Unknown;
|
||||
case Platforms.Steam:
|
||||
return DeviceClass.Screen;
|
||||
case Platforms.PlayStation:
|
||||
return DeviceClass.Screen;
|
||||
case Platforms.HeadlessBot:
|
||||
return DeviceClass.Unknown;
|
||||
case Platforms.GooglePlay:
|
||||
return DeviceClass.Screen;
|
||||
case Platforms.IOS:
|
||||
return DeviceClass.Screen;
|
||||
case Platforms.Xbox:
|
||||
return DeviceClass.Screen;
|
||||
case Platforms.Oculus:
|
||||
return DeviceClass.VRLow;
|
||||
}
|
||||
return DeviceClass.Unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace RadicadeServer.Extensions
|
||||
{
|
||||
public static class JsonExtension
|
||||
{
|
||||
public static T? FromJson<T>(this string json)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(json);
|
||||
}
|
||||
public static T? FromJson<T>(this string json, JsonSerializerOptions options)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(json, options);
|
||||
}
|
||||
public static string ToJson(this object val)
|
||||
{
|
||||
return JsonSerializer.Serialize(val);
|
||||
}
|
||||
|
||||
public static T Get<T>(this JsonNode node, string key, T fallback = default!)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = node?[key];
|
||||
|
||||
if (value is null)
|
||||
return fallback;
|
||||
|
||||
if (value is JsonArray or JsonObject)
|
||||
return value.Deserialize<T>() ?? fallback;
|
||||
|
||||
var kind = value.GetValueKind();
|
||||
|
||||
if (typeof(T) == typeof(bool))
|
||||
{
|
||||
if (kind == JsonValueKind.String)
|
||||
{
|
||||
var text = value.GetValue<string>();
|
||||
return (T)(object)(text == "1" || text.Equals("true", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
if (kind == JsonValueKind.Number)
|
||||
return (T)(object)(value.GetValue<decimal>() != 0);
|
||||
}
|
||||
|
||||
if (typeof(T).IsEnum)
|
||||
{
|
||||
if (kind == JsonValueKind.String)
|
||||
return (T)Enum.Parse(typeof(T), value.GetValue<string>(), ignoreCase: true);
|
||||
|
||||
if (kind == JsonValueKind.Number)
|
||||
return (T)Enum.ToObject(typeof(T), Convert.ChangeType(value.GetValue<decimal>(), Enum.GetUnderlyingType(typeof(T))));
|
||||
}
|
||||
|
||||
return value.GetValue<T>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
using RadicadeServer.DB;
|
||||
|
||||
namespace RadicadeServer.Extensions
|
||||
{
|
||||
public static class UserExtension
|
||||
{
|
||||
public static long? GetUserId(this ClaimsPrincipal req)
|
||||
{
|
||||
string? userIdStr = req.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (userIdStr == null) return null;
|
||||
return long.Parse(userIdStr);
|
||||
}
|
||||
|
||||
public static string? GetPlatId(this ClaimsPrincipal req)
|
||||
{
|
||||
string? userIdStr = req.FindFirstValue("rn.platid");
|
||||
return userIdStr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static long? GetUserId(this string req)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(req)) return null;
|
||||
string token = req.Trim();
|
||||
|
||||
|
||||
if (token.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||||
token = token.Substring(7).Trim();
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(DBConfig.JwtKey);
|
||||
|
||||
var principal = tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
}, out _);
|
||||
|
||||
var tokenClaim = principal.Claims.FirstOrDefault(claim => claim.Type == "userId")?.Value;
|
||||
if (tokenClaim == null) return null;
|
||||
return long.Parse(tokenClaim);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user