Created INeedsLoginCredentials and implemented in MEGADownloader

This commit is contained in:
erri120 2020-04-04 15:54:09 +02:00
parent 9eac8d7641
commit 21b78931a8
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
2 changed files with 72 additions and 2 deletions

View File

@ -14,4 +14,21 @@ namespace Wabbajack.Lib.Downloaders
Uri SiteURL { get; }
Uri IconUri { get; }
}
public struct LoginReturnMessage
{
public string Message;
public bool Failure;
public LoginReturnMessage(string message, bool failure)
{
Message = message;
Failure = failure;
}
}
public interface INeedsLoginCredentials : INeedsLogin
{
LoginReturnMessage LoginWithCredentials(string username, string password);
}
}

View File

@ -8,11 +8,59 @@ using Wabbajack.Common;
namespace Wabbajack.Lib.Downloaders
{
public class MegaDownloader : IUrlDownloader, INeedsLogin
public class MegaDownloader : IUrlDownloader, INeedsLoginCredentials
{
public MegaApiClient MegaApiClient;
private const string DataName = "mega-cred";
public LoginReturnMessage LoginWithCredentials(string username, string password)
{
MegaApiClient.AuthInfos authInfos;
try
{
authInfos = MegaApiClient.GenerateAuthInfos(username, password);
// purge credentials
username = null;
password = null;
}
catch (ApiException e)
{
return e.ApiResultCode switch
{
ApiResultCode.BadArguments => new LoginReturnMessage($"Email or password was wrong! {e.Message}",
true),
ApiResultCode.InternalError => new LoginReturnMessage(
$"Internal error occured! Please report this to the Wabbajack Team! {e.Message}", true),
_ => new LoginReturnMessage($"Error generating authentication information! {e.Message}", true)
};
}
try
{
MegaApiClient.Login(authInfos);
}
catch (ApiException e)
{
if ((int)e.ApiResultCode == -26)
{
return new LoginReturnMessage("Two-Factor Authentication needs to be disabled before login!", true);
}
return e.ApiResultCode switch
{
ApiResultCode.InternalError => new LoginReturnMessage(
$"Internal error occured! Please report this to the Wabbajack Team! {e.Message}", true),
_ => new LoginReturnMessage($"Error during login: {e.Message}", true)
};
}
if(MegaApiClient.IsLoggedIn)
authInfos.ToEcryptedJson(DataName);
return new LoginReturnMessage("Logged in successfully, you can now close this window.",
!MegaApiClient.IsLoggedIn || !Utils.HaveEncryptedJson(DataName));
}
public MegaDownloader()
{
MegaApiClient = new MegaApiClient();
@ -47,10 +95,15 @@ namespace Wabbajack.Lib.Downloaders
public override async Task<bool> Download(Archive a, string destination)
{
if (!MegaApiClient.IsLoggedIn)
if (!MegaApiClient.IsLoggedIn && !Utils.HaveEncryptedJson(DataName))
{
Utils.Status("Logging into MEGA (as anonymous)");
MegaApiClient.LoginAnonymous();
} else if (Utils.HaveEncryptedJson(DataName))
{
Utils.Status("Logging into MEGA with saved credentials.");
var authInfo = Utils.FromEncryptedJson<MegaApiClient.AuthInfos>(DataName);
MegaApiClient.Login(authInfo);
}
var fileLink = new Uri(Url);