2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
2021-10-04 14:40:00 +08:00
|
|
|
|
using System;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Diagnostics;
|
2018-09-25 19:53:24 +08:00
|
|
|
|
using System.Net.Http;
|
2021-10-04 14:40:00 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
|
|
|
|
public class OAuth
|
|
|
|
|
{
|
|
|
|
|
private readonly string clientId;
|
|
|
|
|
private readonly string clientSecret;
|
|
|
|
|
private readonly string endpoint;
|
|
|
|
|
|
|
|
|
|
public readonly Bindable<OAuthToken> Token = new Bindable<OAuthToken>();
|
|
|
|
|
|
|
|
|
|
public string TokenString
|
|
|
|
|
{
|
|
|
|
|
get => Token.Value?.ToString();
|
|
|
|
|
set => Token.Value = string.IsNullOrEmpty(value) ? null : OAuthToken.Parse(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal OAuth(string clientId, string clientSecret, string endpoint)
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(clientId != null);
|
|
|
|
|
Debug.Assert(clientSecret != null);
|
|
|
|
|
Debug.Assert(endpoint != null);
|
|
|
|
|
|
|
|
|
|
this.clientId = clientId;
|
|
|
|
|
this.clientSecret = clientSecret;
|
|
|
|
|
this.endpoint = endpoint;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 14:40:00 +08:00
|
|
|
|
internal void AuthenticateWithLogin(string username, string password)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-10-04 14:40:00 +08:00
|
|
|
|
if (string.IsNullOrEmpty(username)) throw new ArgumentException("Missing username.");
|
|
|
|
|
if (string.IsNullOrEmpty(password)) throw new ArgumentException("Missing password.");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-18 13:28:29 +08:00
|
|
|
|
var accessTokenRequest = new AccessTokenRequestPassword(username, password)
|
|
|
|
|
{
|
|
|
|
|
Url = $@"{endpoint}/oauth/token",
|
|
|
|
|
Method = HttpMethod.Post,
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
ClientSecret = clientSecret
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (accessTokenRequest)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-10-18 13:28:29 +08:00
|
|
|
|
accessTokenRequest.Perform();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
2021-10-04 14:40:00 +08:00
|
|
|
|
catch (Exception ex)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-10-04 14:40:00 +08:00
|
|
|
|
Token.Value = null;
|
|
|
|
|
|
|
|
|
|
var throwableException = ex;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// attempt to decode a displayable error string.
|
2021-10-18 13:28:29 +08:00
|
|
|
|
var error = JsonConvert.DeserializeObject<OAuthError>(accessTokenRequest.GetResponseString() ?? string.Empty);
|
2021-10-04 14:40:00 +08:00
|
|
|
|
if (error != null)
|
2021-10-04 15:18:55 +08:00
|
|
|
|
throwableException = new APIException(error.UserDisplayableError, ex);
|
2021-10-04 14:40:00 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw throwableException;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 13:28:29 +08:00
|
|
|
|
Token.Value = accessTokenRequest.ResponseObject;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool AuthenticateWithRefresh(string refresh)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-10-18 13:28:29 +08:00
|
|
|
|
var refreshRequest = new AccessTokenRequestRefresh(refresh)
|
|
|
|
|
{
|
|
|
|
|
Url = $@"{endpoint}/oauth/token",
|
|
|
|
|
Method = HttpMethod.Post,
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
ClientSecret = clientSecret
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (refreshRequest)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-10-18 13:28:29 +08:00
|
|
|
|
refreshRequest.Perform();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-18 13:28:29 +08:00
|
|
|
|
Token.Value = refreshRequest.ResponseObject;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
//todo: potentially only kill the refresh token on certain exception types.
|
|
|
|
|
Token.Value = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly object access_token_retrieval_lock = new object();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Should be run before any API request to make sure we have a valid key.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool ensureAccessToken()
|
|
|
|
|
{
|
|
|
|
|
// if we already have a valid access token, let's use it.
|
|
|
|
|
if (accessTokenValid) return true;
|
|
|
|
|
|
|
|
|
|
// we want to ensure only a single authentication update is happening at once.
|
|
|
|
|
lock (access_token_retrieval_lock)
|
|
|
|
|
{
|
|
|
|
|
// re-check if valid, in case another request completed and revalidated our access.
|
|
|
|
|
if (accessTokenValid) return true;
|
|
|
|
|
|
|
|
|
|
// if not, let's try using our refresh token to request a new access token.
|
|
|
|
|
if (!string.IsNullOrEmpty(Token.Value?.RefreshToken))
|
|
|
|
|
// ReSharper disable once PossibleNullReferenceException
|
|
|
|
|
AuthenticateWithRefresh(Token.Value.RefreshToken);
|
|
|
|
|
|
|
|
|
|
return accessTokenValid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool accessTokenValid => Token.Value?.IsValid ?? false;
|
|
|
|
|
|
|
|
|
|
internal bool HasValidAccessToken => RequestAccessToken() != null;
|
|
|
|
|
|
|
|
|
|
internal string RequestAccessToken()
|
|
|
|
|
{
|
|
|
|
|
if (!ensureAccessToken()) return null;
|
|
|
|
|
|
|
|
|
|
return Token.Value.AccessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Clear()
|
|
|
|
|
{
|
|
|
|
|
Token.Value = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class AccessTokenRequestRefresh : AccessTokenRequest
|
|
|
|
|
{
|
|
|
|
|
internal readonly string RefreshToken;
|
|
|
|
|
|
|
|
|
|
internal AccessTokenRequestRefresh(string refreshToken)
|
|
|
|
|
{
|
|
|
|
|
RefreshToken = refreshToken;
|
|
|
|
|
GrantType = @"refresh_token";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PrePerform()
|
|
|
|
|
{
|
|
|
|
|
AddParameter("refresh_token", RefreshToken);
|
|
|
|
|
|
|
|
|
|
base.PrePerform();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class AccessTokenRequestPassword : AccessTokenRequest
|
|
|
|
|
{
|
|
|
|
|
internal readonly string Username;
|
|
|
|
|
internal readonly string Password;
|
|
|
|
|
|
|
|
|
|
internal AccessTokenRequestPassword(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
Username = username;
|
|
|
|
|
Password = password;
|
|
|
|
|
GrantType = @"password";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PrePerform()
|
|
|
|
|
{
|
|
|
|
|
AddParameter("username", Username);
|
|
|
|
|
AddParameter("password", Password);
|
|
|
|
|
|
|
|
|
|
base.PrePerform();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 18:21:27 +08:00
|
|
|
|
private class AccessTokenRequest : OsuJsonWebRequest<OAuthToken>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
protected string GrantType;
|
|
|
|
|
|
|
|
|
|
internal string ClientId;
|
|
|
|
|
internal string ClientSecret;
|
|
|
|
|
|
|
|
|
|
protected override void PrePerform()
|
|
|
|
|
{
|
|
|
|
|
AddParameter("grant_type", GrantType);
|
|
|
|
|
AddParameter("client_id", ClientId);
|
|
|
|
|
AddParameter("client_secret", ClientSecret);
|
2018-11-28 18:02:23 +08:00
|
|
|
|
AddParameter("scope", "*");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
base.PrePerform();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-04 14:40:00 +08:00
|
|
|
|
|
|
|
|
|
private class OAuthError
|
|
|
|
|
{
|
2021-10-04 15:18:55 +08:00
|
|
|
|
public string UserDisplayableError => !string.IsNullOrEmpty(Hint) ? Hint : ErrorIdentifier;
|
|
|
|
|
|
2021-10-04 14:40:00 +08:00
|
|
|
|
[JsonProperty("error")]
|
2021-10-04 15:18:55 +08:00
|
|
|
|
public string ErrorIdentifier { get; set; }
|
2021-10-04 14:40:00 +08:00
|
|
|
|
|
|
|
|
|
[JsonProperty("hint")]
|
2021-10-04 15:18:55 +08:00
|
|
|
|
public string Hint { get; set; }
|
|
|
|
|
|
|
|
|
|
[JsonProperty("message")]
|
2021-10-04 14:40:00 +08:00
|
|
|
|
public string Message { get; set; }
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|