1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 22:52:57 +08:00
osu-lazer/osu.Game/Online/API/OAuthToken.cs

65 lines
1.8 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-08-31 18:49:34 +08:00
using System;
using System.Globalization;
using Newtonsoft.Json;
namespace osu.Game.Online.API
{
[Serializable]
public class OAuthToken
2016-08-31 18:49:34 +08:00
{
/// <summary>
/// OAuth 2.0 access token.
/// </summary>
[JsonProperty(@"access_token")]
public string AccessToken;
[JsonProperty(@"expires_in")]
public long ExpiresIn
{
get
{
2017-09-24 00:42:46 +08:00
return AccessTokenExpiry - DateTimeOffset.UtcNow.ToUnixTimeSeconds();
2016-08-31 18:49:34 +08:00
}
set
{
2017-09-24 00:42:46 +08:00
AccessTokenExpiry = DateTimeOffset.Now.AddSeconds(value).ToUnixTimeSeconds();
2016-08-31 18:49:34 +08:00
}
}
public bool IsValid => !string.IsNullOrEmpty(AccessToken) && ExpiresIn > 30;
public long AccessTokenExpiry;
/// <summary>
/// OAuth 2.0 refresh token.
/// </summary>
[JsonProperty(@"refresh_token")]
public string RefreshToken;
public override string ToString() => $@"{AccessToken}|{AccessTokenExpiry.ToString(NumberFormatInfo.InvariantInfo)}|{RefreshToken}";
2016-08-31 18:49:34 +08:00
public static OAuthToken Parse(string value)
{
try
{
string[] parts = value.Split('|');
2017-03-07 09:59:19 +08:00
return new OAuthToken
2016-08-31 18:49:34 +08:00
{
AccessToken = parts[0],
AccessTokenExpiry = long.Parse(parts[1], NumberFormatInfo.InvariantInfo),
RefreshToken = parts[2]
};
}
catch
{
}
return null;
}
}
}