1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 02:12:56 +08:00
osu-lazer/osu.Game/Online/API/APIAccess.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

675 lines
23 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2016-08-31 18:49:34 +08:00
using System;
using System.Collections.Generic;
2016-11-30 15:54:15 +08:00
using System.Diagnostics;
2016-08-31 18:49:34 +08:00
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
2016-08-31 18:49:34 +08:00
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.ExceptionExtensions;
using osu.Framework.Extensions.ObjectExtensions;
2018-03-14 09:42:58 +08:00
using osu.Framework.Graphics;
2016-08-31 18:49:34 +08:00
using osu.Framework.Logging;
using osu.Game.Configuration;
using osu.Game.Localisation;
2016-08-31 18:49:34 +08:00
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
using osu.Game.Online.Notifications.WebSocket;
2017-03-27 23:04:07 +08:00
using osu.Game.Users;
2018-04-13 17:19:50 +08:00
2016-08-31 18:49:34 +08:00
namespace osu.Game.Online.API
{
2018-03-14 09:42:58 +08:00
public partial class APIAccess : Component, IAPIProvider
2016-08-31 18:49:34 +08:00
{
private readonly OsuGameBase game;
private readonly OsuConfigManager config;
2020-02-14 21:27:21 +08:00
2021-02-14 22:31:57 +08:00
private readonly string versionHash;
private readonly OAuth authentication;
2018-04-13 17:19:50 +08:00
private readonly Queue<APIRequest> queue = new Queue<APIRequest>();
2018-04-13 17:19:50 +08:00
public string APIEndpointUrl { get; }
public string WebsiteRootUrl { get; }
2024-05-29 19:34:12 +08:00
/// <summary>
/// The API response version.
/// See: https://osu.ppy.sh/docs/index.html#api-versions
/// </summary>
public int APIVersion { get; }
2022-02-17 17:33:27 +08:00
public Exception LastLoginError { get; private set; }
public string ProvidedUsername { get; private set; }
2018-04-13 17:19:50 +08:00
2023-11-16 15:38:07 +08:00
public string SecondFactorCode { get; private set; }
private string password;
2018-04-13 17:19:50 +08:00
public IBindable<APIUser> LocalUser => localUser;
public IBindableList<APIUser> Friends => friends;
public IBindable<UserActivity> Activity => activity;
public IBindable<UserStatistics> Statistics => statistics;
2018-04-13 17:19:50 +08:00
public INotificationsClient NotificationsClient { get; }
public Language Language => game.CurrentLanguage.Value;
private Bindable<APIUser> localUser { get; } = new Bindable<APIUser>(createGuestUser());
2020-12-17 18:30:55 +08:00
private BindableList<APIUser> friends { get; } = new BindableList<APIUser>();
private Bindable<UserActivity> activity { get; } = new Bindable<UserActivity>();
private Bindable<UserStatus?> configStatus { get; } = new Bindable<UserStatus?>();
private Bindable<UserStatus?> localUserStatus { get; } = new Bindable<UserStatus?>();
private Bindable<UserStatistics> statistics { get; } = new Bindable<UserStatistics>();
protected bool HasLogin => authentication.Token.Value != null || (!string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password));
2018-04-13 17:19:50 +08:00
private readonly CancellationTokenSource cancellationToken = new CancellationTokenSource();
2018-04-13 17:19:50 +08:00
private readonly Logger log;
2018-04-13 17:19:50 +08:00
public APIAccess(OsuGameBase game, OsuConfigManager config, EndpointConfiguration endpointConfiguration, string versionHash)
2016-08-31 18:49:34 +08:00
{
this.game = game;
this.config = config;
2021-02-14 22:31:57 +08:00
this.versionHash = versionHash;
2018-04-13 17:19:50 +08:00
if (game.IsDeployedBuild)
APIVersion = game.AssemblyVersion.Major * 10000 + game.AssemblyVersion.Minor;
else
{
var now = DateTimeOffset.Now;
APIVersion = now.Year * 10000 + now.Month * 100 + now.Day;
}
APIEndpointUrl = endpointConfiguration.APIEndpointUrl;
WebsiteRootUrl = endpointConfiguration.WebsiteRootUrl;
NotificationsClient = setUpNotificationsClient();
authentication = new OAuth(endpointConfiguration.APIClientID, endpointConfiguration.APIClientSecret, APIEndpointUrl);
2016-08-31 18:49:34 +08:00
log = Logger.GetLogger(LoggingTarget.Network);
log.Add($@"API endpoint root: {APIEndpointUrl}");
log.Add($@"API request version: {APIVersion}");
2018-04-13 17:19:50 +08:00
ProvidedUsername = config.Get<string>(OsuSetting.Username);
2018-04-13 17:19:50 +08:00
2018-04-12 13:30:28 +08:00
authentication.TokenString = config.Get<string>(OsuSetting.Token);
authentication.Token.ValueChanged += onTokenChanged;
2018-04-13 17:19:50 +08:00
config.BindWith(OsuSetting.UserOnlineStatus, configStatus);
localUser.BindValueChanged(u =>
{
u.OldValue?.Activity.UnbindFrom(activity);
u.NewValue.Activity.BindTo(activity);
if (u.OldValue != null)
localUserStatus.UnbindFrom(u.OldValue.Status);
localUserStatus.BindTo(u.NewValue.Status);
}, true);
localUserStatus.BindValueChanged(val => configStatus.Value = val.NewValue);
var thread = new Thread(run)
{
Name = "APIAccess",
IsBackground = true
};
thread.Start();
2016-08-31 18:49:34 +08:00
}
2018-04-13 17:19:50 +08:00
private WebSocketNotificationsClientConnector setUpNotificationsClient()
{
var connector = new WebSocketNotificationsClientConnector(this);
connector.MessageReceived += msg =>
{
switch (msg.Event)
{
case @"verified":
if (state.Value == APIState.RequiresSecondFactorAuth)
state.Value = APIState.Online;
break;
case @"logout":
if (state.Value == APIState.Online)
Logout();
break;
}
};
return connector;
}
private void onTokenChanged(ValueChangedEvent<OAuthToken> e) => config.SetValue(OsuSetting.Token, config.Get<bool>(OsuSetting.SavePassword) ? authentication.TokenString : string.Empty);
2018-04-13 17:19:50 +08:00
2018-03-24 17:22:55 +08:00
internal new void Schedule(Action action) => base.Schedule(action);
2018-04-13 17:19:50 +08:00
public string AccessToken => authentication.RequestAccessToken();
2018-04-13 17:19:50 +08:00
2016-08-31 18:49:34 +08:00
/// <summary>
/// Number of consecutive requests which failed due to network issues.
/// </summary>
2017-03-07 09:59:19 +08:00
private int failureCount;
2018-04-13 17:19:50 +08:00
/// <summary>
/// The main API thread loop, which will continue to run until the game is shut down.
/// </summary>
2016-08-31 18:49:34 +08:00
private void run()
{
while (!cancellationToken.IsCancellationRequested)
2016-08-31 18:49:34 +08:00
{
if (state.Value == APIState.Failing)
{
// To recover from a failing state, falling through and running the full reconnection process seems safest for now.
// This could probably be replaced with a ping-style request if we want to avoid the reconnection overheads.
log.Add($@"{nameof(APIAccess)} is in a failing state, waiting a bit before we try again...");
Thread.Sleep(5000);
}
// Ensure that we have valid credentials.
// If not, setting the offline state will allow the game to prompt the user to provide new credentials.
if (!HasLogin)
2016-08-31 18:49:34 +08:00
{
state.Value = APIState.Offline;
Thread.Sleep(50);
continue;
}
Debug.Assert(HasLogin);
// Ensure that we are in an online state. If not, attempt a connect.
if (state.Value != APIState.Online)
{
attemptConnect();
if (state.Value != APIState.Online)
continue;
2016-08-31 18:49:34 +08:00
}
2018-04-13 17:19:50 +08:00
2020-05-05 09:31:11 +08:00
// hard bail if we can't get a valid access token.
2016-08-31 18:49:34 +08:00
if (authentication.RequestAccessToken() == null)
{
2018-12-22 16:54:19 +08:00
Logout();
2016-08-31 18:49:34 +08:00
continue;
}
2018-04-13 17:19:50 +08:00
processQueuedRequests();
Thread.Sleep(50);
}
}
/// <summary>
/// Dequeue from the queue and run each request synchronously until the queue is empty.
/// </summary>
private void processQueuedRequests()
{
while (true)
{
APIRequest req;
lock (queue)
{
if (queue.Count == 0) return;
req = queue.Dequeue();
}
2019-02-28 12:31:40 +08:00
handleRequest(req);
}
}
/// <summary>
/// From a non-connected state, perform a full connection flow, obtaining OAuth tokens and populating the local user and friends.
/// </summary>
/// <remarks>
/// This method takes control of <see cref="state"/> and transitions from <see cref="APIState.Connecting"/> to either
2023-11-16 15:38:07 +08:00
/// - <see cref="APIState.RequiresSecondFactorAuth"/> (pending 2fa)
/// - <see cref="APIState.Online"/> (successful connection)
/// - <see cref="APIState.Failing"/> (failed connection but retrying)
/// - <see cref="APIState.Offline"/> (failed and can't retry, clear credentials and require user interaction)
/// </remarks>
/// <returns>Whether the connection attempt was successful.</returns>
private void attemptConnect()
{
if (localUser.IsDefault)
{
// Show a placeholder user if saved credentials are available.
// This is useful for storing local scores and showing a placeholder username after starting the game,
// until a valid connection has been established.
setLocalUser(new APIUser
{
Username = ProvidedUsername,
Status = { Value = configStatus.Value ?? UserStatus.Online }
});
}
// save the username at this point, if the user requested for it to be.
config.SetValue(OsuSetting.Username, config.Get<bool>(OsuSetting.SaveUsername) ? ProvidedUsername : string.Empty);
if (!authentication.HasValidAccessToken)
{
2023-11-16 15:38:07 +08:00
state.Value = APIState.Connecting;
LastLoginError = null;
try
{
authentication.AuthenticateWithLogin(ProvidedUsername, password);
2016-08-31 18:49:34 +08:00
}
catch (Exception e)
{
//todo: this fails even on network-related issues. we should probably handle those differently.
LastLoginError = e;
log.Add($@"Login failed for username {ProvidedUsername} ({LastLoginError.Message})!");
2018-04-13 17:19:50 +08:00
Logout();
return;
}
2016-08-31 18:49:34 +08:00
}
2024-01-29 16:18:17 +08:00
switch (state.Value)
{
2024-01-29 16:18:17 +08:00
case APIState.RequiresSecondFactorAuth:
{
2024-01-29 16:18:17 +08:00
if (string.IsNullOrEmpty(SecondFactorCode))
return;
state.Value = APIState.Connecting;
LastLoginError = null;
var verificationRequest = new VerifySessionRequest(SecondFactorCode);
verificationRequest.Success += () => state.Value = APIState.Online;
verificationRequest.Failure += ex =>
{
2024-01-29 16:18:17 +08:00
state.Value = APIState.RequiresSecondFactorAuth;
LastLoginError = ex;
2024-01-29 16:18:17 +08:00
SecondFactorCode = null;
};
if (!handleRequest(verificationRequest))
{
state.Value = APIState.Failing;
2024-01-29 16:18:17 +08:00
return;
}
2024-01-29 16:18:17 +08:00
if (state.Value != APIState.Online)
return;
2024-01-29 16:18:17 +08:00
break;
}
2024-01-29 16:18:17 +08:00
default:
{
2024-01-29 16:18:17 +08:00
var userReq = new GetMeRequest();
2024-01-29 16:18:17 +08:00
userReq.Failure += ex =>
{
if (ex is APIException)
{
LastLoginError = ex;
log.Add($@"Login failed for username {ProvidedUsername} on user retrieval ({LastLoginError.Message})!");
Logout();
}
else if (ex is WebException webException && webException.Message == @"Unauthorized")
{
log.Add(@"Login no longer valid");
Logout();
}
else
{
state.Value = APIState.Failing;
}
};
userReq.Success += me =>
{
me.Status.Value = configStatus.Value ?? UserStatus.Online;
2024-01-29 16:18:17 +08:00
setLocalUser(me);
2024-01-29 16:18:17 +08:00
state.Value = me.SessionVerified ? APIState.Online : APIState.RequiresSecondFactorAuth;
failureCount = 0;
};
2024-01-29 16:18:17 +08:00
if (!handleRequest(userReq))
{
state.Value = APIState.Failing;
return;
}
2024-01-29 16:18:17 +08:00
break;
}
}
var friendsReq = new GetFriendsRequest();
friendsReq.Failure += _ => state.Value = APIState.Failing;
friendsReq.Success += res =>
{
friends.Clear();
friends.AddRange(res);
};
if (!handleRequest(friendsReq))
{
state.Value = APIState.Failing;
return;
}
// The Success callback event is fired on the main thread, so we should wait for that to run before proceeding.
// Without this, we will end up circulating this Connecting loop multiple times and queueing up many web requests
// before actually going online.
while (State.Value == APIState.Connecting && !cancellationToken.IsCancellationRequested)
Thread.Sleep(500);
2016-08-31 18:49:34 +08:00
}
2018-04-13 17:19:50 +08:00
public void Perform(APIRequest request)
{
try
{
request.Perform(this);
}
catch (Exception e)
{
// todo: fix exception handling
request.Fail(e);
}
}
public Task PerformAsync(APIRequest request) =>
Task.Factory.StartNew(() => Perform(request), TaskCreationOptions.LongRunning);
2016-11-30 15:54:15 +08:00
public void Login(string username, string password)
{
Debug.Assert(State.Value == APIState.Offline);
2018-04-13 17:19:50 +08:00
ProvidedUsername = username;
this.password = password;
2016-11-30 15:54:15 +08:00
}
2018-04-13 17:19:50 +08:00
2023-11-16 15:38:07 +08:00
public void AuthenticateSecondFactor(string code)
{
Debug.Assert(State.Value == APIState.RequiresSecondFactorAuth);
SecondFactorCode = code;
}
public IHubClientConnector GetHubConnector(string clientName, string endpoint, bool preferMessagePack) =>
new HubClientConnector(clientName, endpoint, this, versionHash, preferMessagePack);
public IChatClient GetChatClient() => new WebSocketChatClient(this);
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
{
Debug.Assert(State.Value == APIState.Offline);
2018-12-05 12:08:35 +08:00
var req = new RegistrationRequest
{
Url = $@"{APIEndpointUrl}/users",
Method = HttpMethod.Post,
Username = username,
Email = email,
Password = password
};
try
{
req.Perform();
}
catch (Exception e)
{
try
{
return JObject.Parse(req.GetResponseString().AsNonNull()).SelectToken(@"form_error", true).AsNonNull().ToObject<RegistrationRequest.RegistrationRequestErrors>();
}
catch
{
try
{
// attempt to parse a non-form error message
var response = JObject.Parse(req.GetResponseString().AsNonNull());
string redirect = (string)response.SelectToken(@"url", true);
string message = (string)response.SelectToken(@"error", false);
if (!string.IsNullOrEmpty(redirect))
{
return new RegistrationRequest.RegistrationRequestErrors
{
Redirect = redirect,
Message = message,
};
}
// if we couldn't deserialize the error message let's throw the original exception outwards.
e.Rethrow();
}
catch
{
// if we couldn't deserialize the error message let's throw the original exception outwards.
e.Rethrow();
}
}
}
2018-12-05 12:08:35 +08:00
return null;
}
2016-08-31 18:49:34 +08:00
/// <summary>
/// Handle a single API request.
/// Ensures all exceptions are caught and dealt with correctly.
2016-08-31 18:49:34 +08:00
/// </summary>
/// <param name="req">The request.</param>
/// <returns>true if the request succeeded.</returns>
2016-08-31 18:49:34 +08:00
private bool handleRequest(APIRequest req)
{
try
{
req.Perform(this);
2018-04-13 17:19:50 +08:00
if (req.CompletionState != APIRequestCompletionState.Completed)
return false;
// Reset failure count if this request succeeded.
2016-08-31 18:49:34 +08:00
failureCount = 0;
return true;
2016-08-31 18:49:34 +08:00
}
catch (HttpRequestException re)
{
log.Add($"{nameof(HttpRequestException)} while performing request {req}: {re.Message}");
handleFailure();
return false;
}
catch (SocketException se)
{
log.Add($"{nameof(SocketException)} while performing request {req}: {se.Message}");
handleFailure();
return false;
}
2016-08-31 18:49:34 +08:00
catch (WebException we)
{
log.Add($"{nameof(WebException)} while performing request {req}: {we.Message}");
handleWebException(we);
return false;
2016-08-31 18:49:34 +08:00
}
catch (Exception ex)
2016-08-31 18:49:34 +08:00
{
Logger.Error(ex, "Error occurred while handling an API request.");
return false;
2016-08-31 18:49:34 +08:00
}
}
2018-04-13 17:19:50 +08:00
private readonly Bindable<APIState> state = new Bindable<APIState>();
2018-04-13 17:19:50 +08:00
/// <summary>
/// The current connectivity state of the API.
/// </summary>
public IBindable<APIState> State => state;
2018-04-13 17:19:50 +08:00
private void handleWebException(WebException we)
{
HttpStatusCode statusCode = (we.Response as HttpWebResponse)?.StatusCode
?? (we.Status == WebExceptionStatus.UnknownError ? HttpStatusCode.NotAcceptable : HttpStatusCode.RequestTimeout);
// special cases for un-typed but useful message responses.
switch (we.Message)
{
case "Unauthorized":
case "Forbidden":
statusCode = HttpStatusCode.Unauthorized;
break;
}
switch (statusCode)
{
case HttpStatusCode.Unauthorized:
2018-12-22 16:54:19 +08:00
Logout();
break;
2019-04-01 11:16:05 +08:00
case HttpStatusCode.RequestTimeout:
handleFailure();
break;
}
}
private void handleFailure()
{
failureCount++;
log.Add($@"API failure count is now {failureCount}");
if (failureCount >= 3)
{
state.Value = APIState.Failing;
flushQueue();
}
}
public bool IsLoggedIn => State.Value > APIState.Offline;
2018-04-13 17:19:50 +08:00
public void Queue(APIRequest request)
{
lock (queue)
{
if (state.Value == APIState.Offline)
{
request.Fail(new WebException(@"User not logged in"));
return;
}
queue.Enqueue(request);
}
}
2018-04-13 17:19:50 +08:00
2016-08-31 18:49:34 +08:00
private void flushQueue(bool failOldRequests = true)
{
lock (queue)
{
var oldQueueRequests = queue.ToArray();
2018-04-13 17:19:50 +08:00
queue.Clear();
2018-04-13 17:19:50 +08:00
if (failOldRequests)
{
foreach (var req in oldQueueRequests)
req.Fail(new WebException($@"Request failed from flush operation (state {state.Value})"));
}
2016-08-31 18:49:34 +08:00
}
}
2018-04-13 17:19:50 +08:00
2018-12-22 16:54:19 +08:00
public void Logout()
2016-08-31 18:49:34 +08:00
{
password = null;
2023-11-16 15:38:07 +08:00
SecondFactorCode = null;
2016-08-31 18:49:34 +08:00
authentication.Clear();
2020-12-17 18:30:55 +08:00
// Scheduled prior to state change such that the state changed event is invoked with the correct user and their friends present
Schedule(() =>
{
setLocalUser(createGuestUser());
friends.Clear();
2020-12-17 18:30:55 +08:00
});
2019-05-09 12:42:04 +08:00
state.Value = APIState.Offline;
flushQueue();
2016-08-31 18:49:34 +08:00
}
2018-04-13 17:19:50 +08:00
public void UpdateStatistics(UserStatistics newStatistics)
{
statistics.Value = newStatistics;
if (IsLoggedIn)
localUser.Value.Statistics = newStatistics;
}
private static APIUser createGuestUser() => new GuestUser();
2018-04-13 17:19:50 +08:00
private void setLocalUser(APIUser user) => Scheduler.Add(() =>
{
localUser.Value = user;
statistics.Value = user.Statistics;
}, false);
2018-03-14 09:42:58 +08:00
protected override void Dispose(bool isDisposing)
2016-09-27 18:22:02 +08:00
{
2018-03-14 09:42:58 +08:00
base.Dispose(isDisposing);
2018-04-13 17:19:50 +08:00
2018-03-23 14:20:19 +08:00
flushQueue();
cancellationToken.Cancel();
2018-03-14 09:07:16 +08:00
}
2016-08-31 18:49:34 +08:00
}
2018-04-13 17:19:50 +08:00
internal class GuestUser : APIUser
{
public GuestUser()
{
Username = @"Guest";
Id = SYSTEM_USER_ID;
}
}
public enum APIState
{
/// <summary>
/// We cannot login (not enough credentials).
/// </summary>
Offline,
2018-04-13 17:19:50 +08:00
/// <summary>
/// We are having connectivity issues.
/// </summary>
Failing,
2018-04-13 17:19:50 +08:00
2023-11-15 19:00:09 +08:00
/// <summary>
/// Waiting on second factor authentication.
/// </summary>
2023-11-16 15:38:07 +08:00
RequiresSecondFactorAuth,
2023-11-15 19:00:09 +08:00
/// <summary>
/// We are in the process of (re-)connecting.
/// </summary>
Connecting,
2018-04-13 17:19:50 +08:00
/// <summary>
/// We are online.
/// </summary>
Online
}
2016-08-31 18:49:34 +08:00
}