1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 15:03:13 +08:00

Move API configuration hooks out of OsuGameBase

Also makes username more private, and password completely private.
This commit is contained in:
Dean Herbert 2018-03-14 09:48:03 +09:00
parent 586ac9c3f1
commit afc3646450
3 changed files with 26 additions and 31 deletions

View File

@ -10,6 +10,7 @@ using System.Threading;
using osu.Framework.Configuration;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Game.Configuration;
using osu.Game.Online.API.Requests;
using osu.Game.Users;
@ -17,6 +18,7 @@ namespace osu.Game.Online.API
{
public class APIAccess : IAPIProvider
{
private readonly OsuConfigManager config;
private readonly OAuth authentication;
public string Endpoint = @"https://osu.ppy.sh";
@ -27,11 +29,12 @@ namespace osu.Game.Online.API
public Scheduler Scheduler = new Scheduler();
public string Username;
/// <summary>
/// The username/email provided by the user when initiating a login.
/// </summary>
public string ProvidedUsername { get; private set; }
//private SecurePassword password;
public string Password;
private string password;
public Bindable<User> LocalUser { get; } = new Bindable<User>(createGuestUser());
@ -41,18 +44,23 @@ namespace osu.Game.Online.API
set { authentication.Token = string.IsNullOrEmpty(value) ? null : OAuthToken.Parse(value); }
}
protected bool HasLogin => Token != null || !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
protected bool HasLogin => Token != null || !string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password);
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable (should dispose of this or at very least keep a reference).
private readonly Thread thread;
private readonly Logger log;
public APIAccess()
public APIAccess(OsuConfigManager config)
{
this.config = config;
authentication = new OAuth(client_id, client_secret, Endpoint);
log = Logger.GetLogger(LoggingTarget.Network);
ProvidedUsername = config.Get<string>(OsuSetting.Username);
Token = config.Get<string>(OsuSetting.Token);
thread = new Thread(run) { IsBackground = true };
thread.Start();
}
@ -111,12 +119,15 @@ namespace osu.Game.Online.API
State = APIState.Connecting;
if (!authentication.HasValidAccessToken && !authentication.AuthenticateWithLogin(Username, Password))
// save the username at this point, if the user requested for it to be.
config.Set(OsuSetting.Username, config.Get<bool>(OsuSetting.SaveUsername) ? ProvidedUsername : string.Empty);
if (!authentication.HasValidAccessToken && !authentication.AuthenticateWithLogin(ProvidedUsername, password))
{
//todo: this fails even on network-related issues. we should probably handle those differently.
//NotificationOverlay.ShowMessage("Login failed!");
log.Add(@"Login failed!");
Password = null;
password = null;
authentication.Clear();
continue;
}
@ -173,8 +184,8 @@ namespace osu.Game.Online.API
{
Debug.Assert(State == APIState.Offline);
Username = username;
Password = password;
ProvidedUsername = username;
this.password = password;
}
/// <summary>
@ -283,8 +294,8 @@ namespace osu.Game.Online.API
public void Logout(bool clearUsername = true)
{
flushQueue();
if (clearUsername) Username = null;
Password = null;
if (clearUsername) ProvidedUsername = null;
password = null;
authentication.Clear();
LocalUser.Value = createGuestUser();
}

View File

@ -34,7 +34,7 @@ using osu.Game.Skinning;
namespace osu.Game
{
public class OsuGameBase : Framework.Game, IOnlineComponent, ICanAcceptFiles
public class OsuGameBase : Framework.Game, ICanAcceptFiles
{
protected OsuConfigManager LocalConfig;
@ -108,11 +108,7 @@ namespace osu.Game
dependencies.Cache(SkinManager = new SkinManager(Host.Storage, contextFactory, Host, Audio));
dependencies.Cache(API = new APIAccess
{
Username = LocalConfig.Get<string>(OsuSetting.Username),
Token = LocalConfig.Get<string>(OsuSetting.Token)
});
dependencies.Cache(API = new APIAccess(LocalConfig));
dependencies.CacheAs<IAPIProvider>(API);
dependencies.Cache(RulesetStore = new RulesetStore(contextFactory));
@ -183,8 +179,6 @@ namespace osu.Game
lastBeatmap = b;
};
API.Register(this);
FileStore.Cleanup();
}
@ -211,16 +205,6 @@ namespace osu.Game
private WorkingBeatmap lastBeatmap;
public void APIStateChanged(APIAccess api, APIState state)
{
switch (state)
{
case APIState.Online:
LocalConfig.Set(OsuSetting.Username, LocalConfig.Get<bool>(OsuSetting.SaveUsername) ? API.Username : string.Empty);
break;
}
}
protected override void LoadComplete()
{
base.LoadComplete();

View File

@ -210,7 +210,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
{
PlaceholderText = "Email address",
RelativeSizeAxes = Axes.X,
Text = api?.Username ?? string.Empty,
Text = api?.ProvidedUsername ?? string.Empty,
TabbableContentContainer = this
},
password = new OsuPasswordTextBox