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