Merge pull request #14949 from peppy/login-error-display

Show login failure messages on login form
This commit is contained in:
Dan Balasescu
2021-10-04 17:23:11 +09:00
committed by GitHub
7 changed files with 107 additions and 19 deletions

View File

@ -35,9 +35,8 @@ namespace osu.Game.Online.API
public string WebsiteRootUrl { get; }
/// <summary>
/// The username/email provided by the user when initiating a login.
/// </summary>
public Exception LastLoginError { get; private set; }
public string ProvidedUsername { get; private set; }
private string password;
@ -136,14 +135,23 @@ namespace osu.Game.Online.API
// 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 && !authentication.AuthenticateWithLogin(ProvidedUsername, password))
if (!authentication.HasValidAccessToken)
{
//todo: this fails even on network-related issues. we should probably handle those differently.
//NotificationOverlay.ShowMessage("Login failed!");
log.Add(@"Login failed!");
password = null;
authentication.Clear();
continue;
LastLoginError = null;
try
{
authentication.AuthenticateWithLogin(ProvidedUsername, password);
}
catch (Exception e)
{
//todo: this fails even on network-related issues. we should probably handle those differently.
LastLoginError = e;
log.Add(@"Login failed!");
password = null;
authentication.Clear();
continue;
}
}
var userReq = new GetUserRequest();

View File

@ -32,6 +32,8 @@ namespace osu.Game.Online.API
public string WebsiteRootUrl => "http://localhost";
public Exception LastLoginError { get; private set; }
/// <summary>
/// Provide handling logic for an arbitrary API request.
/// Should return true is a request was handled. If null or false return, the request will be failed with a <see cref="NotSupportedException"/>.
@ -40,6 +42,8 @@ namespace osu.Game.Online.API
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
private bool shouldFailNextLogin;
/// <summary>
/// The current connectivity state of the API.
/// </summary>
@ -74,6 +78,18 @@ namespace osu.Game.Online.API
public void Login(string username, string password)
{
state.Value = APIState.Connecting;
if (shouldFailNextLogin)
{
LastLoginError = new APIException("Not powerful enough to login.", new ArgumentException(nameof(shouldFailNextLogin)));
state.Value = APIState.Offline;
shouldFailNextLogin = false;
return;
}
LastLoginError = null;
LocalUser.Value = new User
{
Username = username,
@ -102,5 +118,7 @@ namespace osu.Game.Online.API
IBindable<User> IAPIProvider.LocalUser => LocalUser;
IBindableList<User> IAPIProvider.Friends => Friends;
IBindable<UserActivity> IAPIProvider.Activity => Activity;
public void FailNextLogin() => shouldFailNextLogin = true;
}
}

View File

@ -3,6 +3,7 @@
#nullable enable
using System;
using System.Threading.Tasks;
using osu.Framework.Bindables;
using osu.Game.Users;
@ -55,6 +56,11 @@ namespace osu.Game.Online.API
/// </summary>
string WebsiteRootUrl { get; }
/// <summary>
/// The last login error that occurred, if any.
/// </summary>
Exception? LastLoginError { get; }
/// <summary>
/// The current connection state of the API.
/// This is not thread-safe and should be scheduled locally if consumed from a drawable component.

View File

@ -1,8 +1,10 @@
// 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.
using System;
using System.Diagnostics;
using System.Net.Http;
using Newtonsoft.Json;
using osu.Framework.Bindables;
namespace osu.Game.Online.API
@ -32,10 +34,10 @@ namespace osu.Game.Online.API
this.endpoint = endpoint;
}
internal bool AuthenticateWithLogin(string username, string password)
internal void AuthenticateWithLogin(string username, string password)
{
if (string.IsNullOrEmpty(username)) return false;
if (string.IsNullOrEmpty(password)) return false;
if (string.IsNullOrEmpty(username)) throw new ArgumentException("Missing username.");
if (string.IsNullOrEmpty(password)) throw new ArgumentException("Missing password.");
using (var req = new AccessTokenRequestPassword(username, password)
{
@ -49,13 +51,27 @@ namespace osu.Game.Online.API
{
req.Perform();
}
catch
catch (Exception ex)
{
return false;
Token.Value = null;
var throwableException = ex;
try
{
// attempt to decode a displayable error string.
var error = JsonConvert.DeserializeObject<OAuthError>(req.GetResponseString() ?? string.Empty);
if (error != null)
throwableException = new APIException(error.UserDisplayableError, ex);
}
catch
{
}
throw throwableException;
}
Token.Value = req.ResponseObject;
return true;
}
}
@ -182,5 +198,19 @@ namespace osu.Game.Online.API
base.PrePerform();
}
}
private class OAuthError
{
public string UserDisplayableError => !string.IsNullOrEmpty(Hint) ? Hint : ErrorIdentifier;
[JsonProperty("error")]
public string ErrorIdentifier { get; set; }
[JsonProperty("hint")]
public string Hint { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
}
}
}