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
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using Newtonsoft.Json;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2017-11-21 02:49:42 +00:00
|
|
|
|
public class OAuthToken
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// OAuth 2.0 access token.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonProperty(@"access_token")]
|
|
|
|
|
public string AccessToken;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
[JsonProperty(@"expires_in")]
|
|
|
|
|
public long ExpiresIn
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => AccessTokenExpiry - DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
|
|
|
|
set => AccessTokenExpiry = DateTimeOffset.Now.AddSeconds(value).ToUnixTimeSeconds();
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
public bool IsValid => !string.IsNullOrEmpty(AccessToken) && ExpiresIn > 30;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
public long AccessTokenExpiry;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// OAuth 2.0 refresh token.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonProperty(@"refresh_token")]
|
|
|
|
|
public string RefreshToken;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-05-11 10:27:02 +00:00
|
|
|
|
public override string ToString() => $@"{AccessToken}|{AccessTokenExpiry.ToString(NumberFormatInfo.InvariantInfo)}|{RefreshToken}";
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
public static OAuthToken Parse(string value)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-11 10:27:02 +00:00
|
|
|
|
string[] parts = value.Split('|');
|
2017-03-07 01:59:19 +00:00
|
|
|
|
return new OAuthToken
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
AccessToken = parts[0],
|
|
|
|
|
AccessTokenExpiry = long.Parse(parts[1], NumberFormatInfo.InvariantInfo),
|
|
|
|
|
RefreshToken = parts[2]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|