namespace KMCCC.Authentication
{
#region
using System;
using System.Threading;
using System.Threading.Tasks;
using Modules.Yggdrasil;
#endregion
#region Login
///
/// 正版验证器(直接登陆)
///
public class YggdrasilLogin : IAuthenticator
{
///
/// 新建正版验证器
///
/// 电子邮件地址
/// 密码
/// 是否启用Twitch
/// clientToken
public YggdrasilLogin(string email, string password, bool twitchEnabled, Guid clientToken)
{
Email = email;
Password = password;
TwitchEnabled = twitchEnabled;
ClientToken = clientToken;
}
///
/// 新建正版验证器(随机的新ClientToken)
///
/// 电子邮件地址
/// 密码
/// 是否启用Twitch
public YggdrasilLogin(string email, string password, bool twitchEnabled) : this(email, password, twitchEnabled, Guid.NewGuid())
{
}
///
/// 电子邮件地址
///
public string Email { get; private set; }
///
/// 密码
///
public string Password { get; private set; }
///
/// 是否启用Twitch
///
public bool TwitchEnabled { get; private set; }
///
///
public Guid ClientToken { get; private set; }
///
/// 返回Yggdrasil验证器类型
///
public string Type
{
get { return "KMCCC.Yggdrasil"; }
}
public AuthenticationInfo Do()
{
var client = new YggdrasilClient(ClientToken);
if (client.Authenticate(Email, Password, TwitchEnabled))
{
return new AuthenticationInfo
{
AccessToken = client.AccessToken,
UserType = client.AccountType,
DisplayName = client.DisplayName,
Properties = client.Properties,
UUID = client.UUID
};
}
return new AuthenticationInfo
{
Error = "验证错误"
};
}
public Task DoAsync(CancellationToken token)
{
var client = new YggdrasilClient(ClientToken);
return client.AuthenticateAsync(Email, Password, TwitchEnabled, token).ContinueWith(task =>
{
if ((task.Exception == null) && (task.Result))
{
return new AuthenticationInfo
{
AccessToken = client.AccessToken,
UserType = client.AccountType,
DisplayName = client.DisplayName,
Properties = client.Properties,
UUID = client.UUID
};
}
return new AuthenticationInfo
{
Error = "验证错误"
};
}, token);
}
}
#endregion
#region Refresh
///
/// 正版验证器(直接登陆)
///
public class YggdrasilRefresh : IAuthenticator
{
///
/// 新建正版验证器
///
/// 合法的Token
/// 是否启用Twitch
/// clientToken
public YggdrasilRefresh(Guid accessToken, bool twitchEnabled, Guid clientToken)
{
AccessToken = accessToken;
TwitchEnabled = twitchEnabled;
ClientToken = clientToken;
}
///
/// 新建正版验证器(随机的新ClientToken)
///
/// 合法的Token
/// 是否启用Twitch
public YggdrasilRefresh(Guid accessToken, bool twitchEnabled)
: this(accessToken, twitchEnabled, Guid.NewGuid())
{
}
public Guid AccessToken { get; private set; }
///
/// 是否启用Twitch
///
public bool TwitchEnabled { get; private set; }
///
///
public Guid ClientToken { get; private set; }
///
/// 返回Yggdrasil验证器类型
///
public string Type
{
get { return "KMCCC.Yggdrasil"; }
}
public AuthenticationInfo Do()
{
var client = new YggdrasilClient(ClientToken);
if (client.Refresh(AccessToken, TwitchEnabled))
{
return new AuthenticationInfo
{
AccessToken = client.AccessToken,
UserType = client.AccountType,
DisplayName = client.DisplayName,
Properties = client.Properties,
UUID = client.UUID
};
}
return new AuthenticationInfo
{
Error = "验证错误"
};
}
public Task DoAsync(CancellationToken token)
{
return Task.Factory.StartNew(Do, token);
}
}
#endregion
}