Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
….MasterServer into 1.37.0
  • Loading branch information
cubicgraphics committed Jan 22, 2025
2 parents 846eeb7 + b54af80 commit fc9954e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<IActionResult> GetMultiplayerInstance([FromBody] GetMultiplaye
// Failed to get player session, creating new player session
session = _sessionService.GetOrAddSession(SessionIdPrefix + Guid.NewGuid().ToString("N"));
session.PlayerClientVersion = TryParseGameVersion(request.Version);
session.PlayerPlatform = (Core.Enums.Platform)request.Platform;
session.PlayerPlatform = request.Platform;
session.PlatformUserId = request.AuthUserId;
session.HashedUserId = UserIdHash.Generate(session.PlayerPlatform, session.PlatformUserId);

Expand Down
121 changes: 88 additions & 33 deletions BeatTogether.MasterServer.Api/Implimentations/UserAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ namespace BeatTogether.MasterServer.Api.Implementations
public class UserAuthenticator : IUserAuthenticator
{
public const string BeatSaverVerifyUserUrl = "https://api.beatsaver.com/users/verify";
public const string PicoUSVerifyUserUrl = "https://platform-us.picovr.com/s2s/v1/user/validate";
public const string PicoCNVerifyUserUrl = "https://platform-cn.picovr.com/s2s/v1/user/validate";

private readonly ApiServerConfiguration _apiServerConfiguration;

private readonly ApiServerConfiguration _apiServerConfiguration;
private readonly HttpClient _httpClient;

private readonly ILogger _logger;
Expand All @@ -37,39 +40,91 @@ public async Task<bool> TryAuthenticateUserWithPlatform(MasterServerSession sess
if (_apiServerConfiguration.AuthenticateClients &&
GetPlatformRequiresAuth(session.PlayerPlatform))
{
if (session.PlayerPlatform != Platform.Pico)
{
var requestContent = new
{
proof = session.PlayerPlatform == Platform.Steam ? BitConverter.ToString(session.SessionToken).Replace("-", "") : Encoding.UTF8.GetString(session.SessionToken),
oculusId = session.PlayerPlatform == Platform.Oculus || session.PlayerPlatform == Platform.OculusQuest ? session.PlatformUserId : null,
steamId = session.PlayerPlatform == Platform.Steam ? session.PlatformUserId : null
};
try
{
using var verifyResponse = await _httpClient.PostAsync(BeatSaverVerifyUserUrl,
new StringContent(JsonSerializer.Serialize(requestContent), null, "application/json"));

var requestContent = new
{
proof = session.PlayerPlatform == Platform.Steam ? BitConverter.ToString(session.SessionToken).Replace("-", "") : Encoding.UTF8.GetString(session.SessionToken),
oculusId = session.PlayerPlatform == Platform.Oculus || session.PlayerPlatform == Platform.OculusQuest ? session.PlatformUserId : null,
steamId = session.PlayerPlatform == Platform.Steam ? session.PlatformUserId : null
};
try
{
using var verifyResponse = await _httpClient.PostAsync(BeatSaverVerifyUserUrl,
new StringContent(JsonSerializer.Serialize(requestContent), null, "application/json"));

verifyResponse.EnsureSuccessStatusCode();

var stringContent = await verifyResponse.Content.ReadAsStringAsync();
if (stringContent.Contains("\"success\": false"))
{
authPasses = false;
authLogReason = "Authentication rejected";
}
else
{
authPasses = true;
authLogReason = "Authentication success";
}
}
catch (Exception)
{
authPasses = true;
authLogReason = "BeatSaver verify request failed, skipping authentication";
}
}
else
verifyResponse.EnsureSuccessStatusCode();

var stringContent = await verifyResponse.Content.ReadAsStringAsync();
if (stringContent.Contains("\"success\": false"))
{
authPasses = false;
authLogReason = "Authentication rejected";
}
else
{
authPasses = true;
authLogReason = "Authentication success";
}
}
catch (Exception)
{
authPasses = true;
authLogReason = "BeatSaver verify request failed, skipping authentication";
}
}
else
{
var requestContent = new
{
access_token = Encoding.UTF8.GetString(session.SessionToken),
user_id = session.PlatformUserId
};
try
{
using var verifyResponse = await _httpClient.PostAsync(PicoUSVerifyUserUrl,
new StringContent(JsonSerializer.Serialize(requestContent), null, "application/json"));

verifyResponse.EnsureSuccessStatusCode();

var stringContent = await verifyResponse.Content.ReadAsStringAsync();
if (stringContent.Contains("\"is_validate\":true"))
{
authPasses = true;
authLogReason = "Authentication success";
}
else
{
_logger.Debug($"Pico US auth failed trying CN, API returned: {stringContent}");
// Trying CN auth
using var verifyResponseCN = await _httpClient.PostAsync(PicoCNVerifyUserUrl,
new StringContent(JsonSerializer.Serialize(requestContent), null, "application/json"));

verifyResponseCN.EnsureSuccessStatusCode();

stringContent = await verifyResponseCN.Content.ReadAsStringAsync();
if (stringContent.Contains("\"is_validate\":true"))
{
authPasses = true;
authLogReason = "Authentication success";
}
else
{
authPasses = false;
authLogReason = "Authentication rejected";
_logger.Debug($"Pico CN auth failed, API returned: {stringContent}");
}

}
}
catch (Exception)
{
authPasses = true;
authLogReason = "Pico verify request failed, skipping authentication";
}
}
}
else
{
authPasses = true;
authLogReason = $"Authentication not required for this platform";
Expand Down
1 change: 1 addition & 0 deletions BeatTogether.MasterServer.Api/Util/UserIdHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static string Generate(Platform platform, string platformUserId)
Platform.Test => "Test#",
Platform.Oculus => "Oculus#",
Platform.OculusQuest => "OculusQuest#",
Platform.Pico => "Pico#",
Platform.Steam => "Steam#",
Platform.Pico => "Pico#",
Platform.PS4 => "PSN#",
Expand Down

0 comments on commit fc9954e

Please sign in to comment.