Files
TenWholeYears.Server/RecNet.Infrastructure/Services/Steam/SteamAuthValidator.cs

48 lines
1.6 KiB
C#

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<PlatformAuthResult> ValidateAsync(
string platformAuthentication,
string platformId,
CancellationToken ct = default)
{
var steamParams = JsonSerializer.Deserialize<SteamPlatformAuth>(platformAuthentication);
if (steamParams == null)
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; }
}
}