From eda3666bc7e4ea81501c9cc406f485a61384db65 Mon Sep 17 00:00:00 2001 From: Valentin Date: Sat, 26 Jun 2021 13:39:05 +0200 Subject: Add support for mega.nz two fa authentication --- .../Backend/Mega/Duplicati.Library.Backend.Mega.csproj | 7 +++++-- Duplicati/Library/Backend/Mega/MegaBackend.cs | 13 ++++++++++++- Duplicati/Library/Backend/Mega/Strings.cs | 4 +++- Duplicati/Library/Backend/Mega/packages.config | 5 +++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Duplicati/Library/Backend/Mega/Duplicati.Library.Backend.Mega.csproj b/Duplicati/Library/Backend/Mega/Duplicati.Library.Backend.Mega.csproj index c8871b0e0..27a9e9cd4 100644 --- a/Duplicati/Library/Backend/Mega/Duplicati.Library.Backend.Mega.csproj +++ b/Duplicati/Library/Backend/Mega/Duplicati.Library.Backend.Mega.csproj @@ -31,12 +31,15 @@ false - - ..\..\..\..\packages\MegaApiClient.1.7.1\lib\net46\MegaApiClient.dll + + ..\..\..\..\packages\MegaApiClient.1.9.0\lib\net46\MegaApiClient.dll ..\..\..\..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll + + ..\..\..\..\packages\Otp.NET.1.2.2\lib\net45\Otp.NET.dll + diff --git a/Duplicati/Library/Backend/Mega/MegaBackend.cs b/Duplicati/Library/Backend/Mega/MegaBackend.cs index 184a63769..c3878b1e1 100644 --- a/Duplicati/Library/Backend/Mega/MegaBackend.cs +++ b/Duplicati/Library/Backend/Mega/MegaBackend.cs @@ -22,6 +22,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using OtpNet; namespace Duplicati.Library.Backend.Mega { @@ -31,6 +32,7 @@ namespace Duplicati.Library.Backend.Mega { private readonly string m_username = null; private readonly string m_password = null; + private readonly string m_twoFactorKey = null; private Dictionary> m_filecache; private INode m_currentFolder = null; private readonly string m_prefix = null; @@ -48,7 +50,13 @@ namespace Duplicati.Library.Backend.Mega if (m_client == null) { var cl = new MegaApiClient(); - cl.Login(m_username, m_password); + if (m_twoFactorKey == null) + cl.Login(m_username, m_password); + else + { + var totp = new Totp(Base32Encoding.ToBytes(m_twoFactorKey)).ComputeTotp(); + cl.Login(m_username, m_password, totp); + } m_client = cl; } @@ -64,6 +72,8 @@ namespace Duplicati.Library.Backend.Mega m_username = options["auth-username"]; if (options.ContainsKey("auth-password")) m_password = options["auth-password"]; + if (options.ContainsKey("auth-two-factor-key")) + m_twoFactorKey = options["auth-two-factor-key"]; if (!string.IsNullOrEmpty(uri.Username)) m_username = uri.Username; @@ -253,6 +263,7 @@ namespace Duplicati.Library.Backend.Mega return new List(new ICommandLineArgument[] { new CommandLineArgument("auth-password", CommandLineArgument.ArgumentType.Password, Strings.MegaBackend.AuthPasswordDescriptionShort, Strings.MegaBackend.AuthPasswordDescriptionLong), new CommandLineArgument("auth-username", CommandLineArgument.ArgumentType.String, Strings.MegaBackend.AuthUsernameDescriptionShort, Strings.MegaBackend.AuthUsernameDescriptionLong), + new CommandLineArgument("auth-two-factor-key", CommandLineArgument.ArgumentType.Password, Strings.MegaBackend.AuthTwoFactorKeyDescriptionShort, Strings.MegaBackend.AuthTwoFactorKeyDescriptionLong), }); } } diff --git a/Duplicati/Library/Backend/Mega/Strings.cs b/Duplicati/Library/Backend/Mega/Strings.cs index d52089191..e3572f0e7 100644 --- a/Duplicati/Library/Backend/Mega/Strings.cs +++ b/Duplicati/Library/Backend/Mega/Strings.cs @@ -6,8 +6,10 @@ namespace Duplicati.Library.Backend.Strings { public static string AuthPasswordDescriptionShort { get { return LC.L(@"Supplies the password used to connect to the server"); } } public static string AuthUsernameDescriptionLong { get { return LC.L(@"The username used to connect to the server. This may also be supplied as the environment variable ""AUTH_USERNAME""."); } } public static string AuthUsernameDescriptionShort { get { return LC.L(@"Supplies the username used to connect to the server"); } } + public static string AuthTwoFactorKeyDescriptionShort { get { return LC.L(@"Supplies the two-factor key used to connect to the server"); } } + public static string AuthTwoFactorKeyDescriptionLong { get { return LC.L(@"The two-factor authentication key used to connect to the server."); } } public static string NoPasswordError { get { return LC.L(@"No password given"); } } public static string NoUsernameError { get { return LC.L(@"No username given"); } } public static string Description { get { return LC.L(@"This backend can read and write data to Mega.co.nz. Allowed formats are: ""mega://folder/subfolder"""); } } } -} +} \ No newline at end of file diff --git a/Duplicati/Library/Backend/Mega/packages.config b/Duplicati/Library/Backend/Mega/packages.config index 06ff32662..c90fbddd1 100644 --- a/Duplicati/Library/Backend/Mega/packages.config +++ b/Duplicati/Library/Backend/Mega/packages.config @@ -1,5 +1,6 @@  - + - + + \ No newline at end of file -- cgit v1.2.3