1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00

Save OAuth token to config on every token change

This commit is contained in:
smoogipoo 2018-04-12 13:31:06 +09:00
parent 62968bb4c7
commit e007365916
2 changed files with 16 additions and 14 deletions

View File

@ -39,8 +39,8 @@ namespace osu.Game.Online.API
public string Token
{
get { return authentication.Token?.ToString(); }
set { authentication.Token = string.IsNullOrEmpty(value) ? null : OAuthToken.Parse(value); }
get { return authentication.Token.Value?.ToString(); }
set { authentication.Token.Value = string.IsNullOrEmpty(value) ? null : OAuthToken.Parse(value); }
}
protected bool HasLogin => Token != null || !string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password);
@ -59,9 +59,13 @@ namespace osu.Game.Online.API
ProvidedUsername = config.Get<string>(OsuSetting.Username);
Token = config.Get<string>(OsuSetting.Token);
authentication.Token.ValueChanged += onTokenChanged;
Task.Factory.StartNew(run, cancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
private void onTokenChanged(OAuthToken token) => config.Set(OsuSetting.Token, config.Get<bool>(OsuSetting.SavePassword) ? Token : string.Empty);
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
internal new void Schedule(Action action) => base.Schedule(action);
@ -306,9 +310,6 @@ namespace osu.Game.Online.API
{
base.Dispose(isDisposing);
config.Set(OsuSetting.Token, config.Get<bool>(OsuSetting.SavePassword) ? Token : string.Empty);
config.Save();
flushQueue();
cancellationToken.Cancel();
}

View File

@ -2,6 +2,7 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Diagnostics;
using osu.Framework.Configuration;
using osu.Framework.IO.Network;
namespace osu.Game.Online.API
@ -12,7 +13,7 @@ namespace osu.Game.Online.API
private readonly string clientSecret;
private readonly string endpoint;
public OAuthToken Token;
public readonly Bindable<OAuthToken> Token = new Bindable<OAuthToken>();
internal OAuth(string clientId, string clientSecret, string endpoint)
{
@ -47,7 +48,7 @@ namespace osu.Game.Online.API
return false;
}
Token = req.ResponseObject;
Token.Value = req.ResponseObject;
return true;
}
}
@ -66,14 +67,14 @@ namespace osu.Game.Online.API
{
req.Perform();
Token = req.ResponseObject;
Token.Value = req.ResponseObject;
return true;
}
}
catch
{
//todo: potentially only kill the refresh token on certain exception types.
Token = null;
Token.Value = null;
return false;
}
}
@ -95,15 +96,15 @@ namespace osu.Game.Online.API
if (accessTokenValid) return true;
// if not, let's try using our refresh token to request a new access token.
if (!string.IsNullOrEmpty(Token?.RefreshToken))
if (!string.IsNullOrEmpty(Token.Value?.RefreshToken))
// ReSharper disable once PossibleNullReferenceException
AuthenticateWithRefresh(Token.RefreshToken);
AuthenticateWithRefresh(Token.Value.RefreshToken);
return accessTokenValid;
}
}
private bool accessTokenValid => Token?.IsValid ?? false;
private bool accessTokenValid => Token.Value?.IsValid ?? false;
internal bool HasValidAccessToken => RequestAccessToken() != null;
@ -111,12 +112,12 @@ namespace osu.Game.Online.API
{
if (!ensureAccessToken()) return null;
return Token.AccessToken;
return Token.Value.AccessToken;
}
internal void Clear()
{
Token = null;
Token.Value = null;
}
private class AccessTokenRequestRefresh : AccessTokenRequest