2022-08-15 23:55:44 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2022-12-20 22:41:14 +08:00
|
|
|
|
using System.Text.Json.Serialization;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
namespace MinecraftClient.Protocol.ProfileKey
|
2022-08-15 23:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
public class PlayerKeyPair
|
|
|
|
|
|
{
|
2022-12-20 22:41:14 +08:00
|
|
|
|
[JsonInclude]
|
|
|
|
|
|
[JsonPropertyName("PublicKey")]
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public PublicKey PublicKey;
|
|
|
|
|
|
|
2022-12-20 22:41:14 +08:00
|
|
|
|
[JsonInclude]
|
|
|
|
|
|
[JsonPropertyName("PrivateKey")]
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public PrivateKey PrivateKey;
|
|
|
|
|
|
|
2022-12-20 22:41:14 +08:00
|
|
|
|
[JsonInclude]
|
|
|
|
|
|
[JsonPropertyName("ExpiresAt")]
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public DateTime ExpiresAt;
|
|
|
|
|
|
|
2022-12-20 22:41:14 +08:00
|
|
|
|
[JsonInclude]
|
|
|
|
|
|
[JsonPropertyName("RefreshedAfter")]
|
|
|
|
|
|
public DateTime RefreshedAfter;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
|
2022-12-20 22:41:14 +08:00
|
|
|
|
[JsonIgnore]
|
2022-08-31 18:00:00 +08:00
|
|
|
|
private const string DataTimeFormat = "yyyy-MM-ddTHH:mm:ss.ffffffZ";
|
2022-08-15 23:55:44 +08:00
|
|
|
|
|
2022-12-20 22:41:14 +08:00
|
|
|
|
[JsonConstructor]
|
|
|
|
|
|
public PlayerKeyPair(PublicKey PublicKey, PrivateKey PrivateKey, DateTime ExpiresAt, DateTime RefreshedAfter)
|
2022-08-15 23:55:44 +08:00
|
|
|
|
{
|
2022-12-20 22:41:14 +08:00
|
|
|
|
this.PublicKey = PublicKey;
|
|
|
|
|
|
this.PrivateKey = PrivateKey;
|
|
|
|
|
|
this.ExpiresAt = ExpiresAt;
|
|
|
|
|
|
this.RefreshedAfter = RefreshedAfter;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool NeedRefresh()
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
return DateTime.Now.ToUniversalTime() > RefreshedAfter;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsExpired()
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
return DateTime.Now.ToUniversalTime() > ExpiresAt;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long GetExpirationMilliseconds()
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTimeOffset timeOffset = new(ExpiresAt);
|
|
|
|
|
|
return timeOffset.ToUnixTimeMilliseconds();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long GetExpirationSeconds()
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTimeOffset timeOffset = new(ExpiresAt);
|
|
|
|
|
|
return timeOffset.ToUnixTimeSeconds();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
List<string> datas = new();
|
2022-08-15 23:55:44 +08:00
|
|
|
|
datas.Add(Convert.ToBase64String(PublicKey.Key));
|
2022-08-24 12:37:22 +08:00
|
|
|
|
if (PublicKey.Signature == null)
|
2022-12-06 15:50:17 +08:00
|
|
|
|
datas.Add(string.Empty);
|
2022-08-24 12:37:22 +08:00
|
|
|
|
else
|
|
|
|
|
|
datas.Add(Convert.ToBase64String(PublicKey.Signature));
|
2022-08-15 23:55:44 +08:00
|
|
|
|
if (PublicKey.SignatureV2 == null)
|
2022-12-06 15:50:17 +08:00
|
|
|
|
datas.Add(string.Empty);
|
2022-08-15 23:55:44 +08:00
|
|
|
|
else
|
|
|
|
|
|
datas.Add(Convert.ToBase64String(PublicKey.SignatureV2));
|
|
|
|
|
|
datas.Add(Convert.ToBase64String(PrivateKey.Key));
|
|
|
|
|
|
datas.Add(ExpiresAt.ToString(DataTimeFormat));
|
|
|
|
|
|
datas.Add(RefreshedAfter.ToString(DataTimeFormat));
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return string.Join(",", datas.ToArray());
|
2022-08-15 23:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|