Skip to content

Commit

Permalink
Add Pico CN auth endpoint, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-r-elp committed Aug 27, 2024
1 parent 3959f4c commit d26c168
Showing 1 changed file with 84 additions and 63 deletions.
147 changes: 84 additions & 63 deletions BeatTogether.MasterServer.Api/Implimentations/UserAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace BeatTogether.MasterServer.Api.Implementations
public class UserAuthenticator : IUserAuthenticator
{
public const string BeatSaverVerifyUserUrl = "https://api.beatsaver.com/users/verify";
public const string PicoVerifyUserUrl = "https://platform-us.picovr.com/s2s/v1/user/validate";
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 @@ -36,73 +38,92 @@ public async Task<bool> TryAuthenticateUserWithPlatform(MasterServerSession sess
var authLogReason = "unknown";

if (_apiServerConfiguration.AuthenticateClients &&
GetPlatformRequiresAuth(session.PlayerPlatform) && 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"));

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 if (_apiServerConfiguration.AuthenticateClients &&
GetPlatformRequiresAuth(session.PlayerPlatform) && session.PlayerPlatform == Platform.Pico)
GetPlatformRequiresAuth(session.PlayerPlatform))
{
var requestContent = new
{
access_token = Encoding.UTF8.GetString(session.SessionToken),
user_id = session.PlatformUserId
};
try
if (session.PlayerPlatform != Platform.Pico)
{
using var verifyResponse = await _httpClient.PostAsync(PicoVerifyUserUrl,
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();
verifyResponse.EnsureSuccessStatusCode();

var stringContent = await verifyResponse.Content.ReadAsStringAsync();
if (stringContent.Contains("\"is_validate\": true"))
{
authPasses = true;
authLogReason = "Authentication success";
}
else
{
authPasses = false;
authLogReason = "Authentication rejected";
_logger.Debug($"Pico API returned: {stringContent}");
}
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";
}
}
catch (Exception)
else
{
authPasses = true;
authLogReason = "Pico verify request failed, skipping authentication";
}
}
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;
Expand Down

0 comments on commit d26c168

Please sign in to comment.