using System.Text.Json; using System.Text.Json.Serialization; using RecNet.Application.Common.Interfaces; using RecNet.Application.Common.PlatformAuth; using RecNet.Domain.Common; namespace RecNet.Infrastructure.Services.Steam; public class SteamAuthValidator(ISteamAuthService steamAuthService) : IPlatformAuthValidator { public PlatformType PlatformType => PlatformType.Steamworks; public async Task ValidateAsync( string platformAuthentication, string platformId, CancellationToken ct = default) { SteamPlatformAuth? steamParams; try { steamParams = JsonSerializer.Deserialize(platformAuthentication); } catch (JsonException) { return PlatformAuthResult.Failure("Invalid authentication"); } if (steamParams?.AppId is null || string.IsNullOrWhiteSpace(steamParams.Ticket)) return PlatformAuthResult.Failure("Invalid authentication"); var result = await steamAuthService.AuthorizeAsync(steamParams.Ticket, steamParams.AppId.AppId, ct); if (!result.Succeeded || result.SteamId is null) return PlatformAuthResult.Failure("Invalid authentication"); if (!string.IsNullOrWhiteSpace(platformId) && platformId != result.SteamId) return PlatformAuthResult.Failure("Nice try"); return PlatformAuthResult.Success(result.DisplayName); } private sealed class SteamPlatformAuth { public required string Ticket { get; set; } public required SteamAppIdAuth AppId { get; set; } } // Splooty, why did you set it up like this ;-; private sealed class SteamAppIdAuth { [JsonPropertyName("m_AppId")] public required uint AppId { get; set; } } }