Ejemplo n.º 1
0
Archivo: Mega.cs Proyecto: aeax/ShareX
 public Mega(MegaApiClient.AuthInfos authInfos, string parentNodeId)
 {
     AllowReportProgress = false;
     _megaClient = new MegaApiClient(this);
     _authInfos = authInfos;
     _parentNodeId = parentNodeId;
 }
        private static void SaveRefreshToken(string account, MegaApiClient.AuthInfos refreshToken)
        {
            var refreshTokens = Settings.Default.RefreshTokens;

            if (refreshTokens != null)
            {
                foreach (RefreshTokenSetting setting in refreshTokens)
                {
                    if (setting.Account == account)
                    {
                        refreshTokens.Remove(setting);
                        break;
                    }
                }
            }
            else
            {
                refreshTokens = Settings.Default.RefreshTokens = new System.Collections.ObjectModel.Collection <RefreshTokenSetting>();
            }

            refreshTokens.Insert(0, new RefreshTokenSetting()
            {
                Account = account, EMail = refreshToken.Email, Hash = refreshToken.Hash, PasswordAesKey = refreshToken.PasswordAesKey
            });

            Settings.Default.Save();
        }
Ejemplo n.º 3
0
        private void buttonAnon_Click(object sender, EventArgs e)
        {
            try
            {
                if (_client.IsLoggedIn)
                {
                    _client.Logout();
                }

                _authInfos = null;
                _token     = null;
                _client.LoginAnonymous();

                if (!_client.IsLoggedIn)
                {
                    throw new IOException("Client did not log in");
                }

                DialogResult = DialogResult.Ignore;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to log in as an anonymous user. Please make sure that you are connected to the internet, then try again.\n\nError message: " + ex.Message,
                                "Log in to mega.nz", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.None;
            }
        }
Ejemplo n.º 4
0
 public Mega(MegaApiClient.AuthInfos authInfos, string parentNodeId)
 {
     AllowReportProgress = false;
     _megaClient         = new MegaApiClient(this);
     _authInfos          = authInfos;
     _parentNodeId       = parentNodeId;
 }
Ejemplo n.º 5
0
        private void buttonAcc_Click(object sender, EventArgs e)
        {
            try
            {
                if (_client.IsLoggedIn)
                {
                    _client.Logout();
                }

                _authInfos = _client.GenerateAuthInfos(textBoxLogin.Text, textBoxPassw.Text);
                _token     = _client.Login(_authInfos);

                if (!_client.IsLoggedIn)
                {
                    throw new IOException("Client did not log in");
                }

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to log in with the specified login and password. Please make sure that they are correct and you are connected to the internet, then try again.\n\nError message: " + ex.Message,
                                "Log in to mega.nz", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.None;
            }
        }
Ejemplo n.º 6
0
 public void Login_NullAuthInfos_Throws(MegaApiClient.AuthInfos authInfos)
 {
     Assert.That(
         () => this.Client.Login(authInfos),
         Throws.TypeOf <ArgumentNullException>()
         .With.Property <ArgumentNullException>(x => x.ParamName).EqualTo("authInfos"));
 }
Ejemplo n.º 7
0
        public void Oauth(CloudType type)
        {
            CheckThread(true);
            Type type_oauthUI;

            switch (type)
            {
            case CloudType.Dropbox:
                DropboxOauthv2 oauth_dropbox = new DropboxOauthv2();
                oauth_dropbox.TokenCallBack += Oauth_dropbox_TokenCallBack;

                type_oauthUI       = LoadDllUI.GetTypeInterface(typeof(UIinterfaceDB));
                AppSetting.UIOauth = (OauthUI)Activator.CreateInstance(type_oauthUI);

                oauth_dropbox.GetCode(AppSetting.UIOauth, AppSetting.UIMain);
                break;


            case CloudType.GoogleDrive:
                GoogleAPIOauth2 oauth_gd = new GoogleAPIOauth2();
                oauth_gd.TokenCallBack += Oauth_gd_TokenCallBack;

                type_oauthUI       = LoadDllUI.GetTypeInterface(typeof(UIinterfaceGD));
                AppSetting.UIOauth = (OauthUI)Activator.CreateInstance(type_oauthUI);

                oauth_gd.GetCode(AppSetting.UIOauth, AppSetting.UIMain);
                break;


            case CloudType.Mega:
                type_oauthUI = LoadDllUI.GetTypeInterface(typeof(UIinterfaceMegaNz));
                UIinterfaceMegaNz mega = (UIinterfaceMegaNz)Activator.CreateInstance(type_oauthUI);
                bool error             = false;
reoauthMega:
                if (!error)
                {
                    mega.ShowDialog_();
                }
                else
                {
                    mega.ShowError("Wrong email or password.");
                }
                if (mega.Success)
                {
                    MegaApiClient.AuthInfos oauthinfo = MegaApiClient.GenerateAuthInfos(mega.Email, mega.Pass);
                    MegaApiClient           client    = new MegaApiClient();
                    try
                    {
                        client.Login(oauthinfo);
                    }
                    catch (Exception) { error = true; goto reoauthMega; }
                    SaveToken(mega.Email, JsonConvert.SerializeObject(oauthinfo), CloudType.Mega);
                }
                break;


            default: throw new Exception("Not support");
            }
        }
Ejemplo n.º 8
0
        public static MegaApiClient GetClient(string email)
        {
            MegaApiClient client = new MegaApiClient();

            MegaApiClient.AuthInfos authinfo = JsonConvert.DeserializeObject <MegaApiClient.AuthInfos>(AppSetting.settings.GetToken(email, CloudType.Mega));
            client.Login(authinfo);
            return(client);
        }
Ejemplo n.º 9
0
        public Mega(MegaApiClient.AuthInfos authInfos, string parentNodeId)
        {
            AllowReportProgress = false;
            Options options = new Options(chunksPackSize: UploadChunksPackSize);

            megaClient        = new MegaApiClient(options, this);
            this.authInfos    = authInfos;
            this.parentNodeId = parentNodeId;
        }
Ejemplo n.º 10
0
 public static MEGAAuthInfos ToMEGAAuthInfos(MegaApiClient.AuthInfos infos)
 {
     return(new MEGAAuthInfos
     {
         Email = infos.Email,
         Hash = infos.Hash,
         PasswordAesKey = infos.PasswordAesKey,
         MFAKey = infos.MFAKey
     });
 }
Ejemplo n.º 11
0
        public MegaUpdater(Uri serverUri, NetworkCredential credentials)
        {
            if (serverUri.Host.ToLower() != "mega.nz")
            {
                throw new NotSupportedException("The link doesn't point to mega.nz - " + serverUri);
            }

            var client = new MegaApiClient();

            _client = client;
            if (credentials != null)
            {
                _authInfos = _client.GenerateAuthInfos(credentials.UserName, credentials.Password);
            }
            CurrentFolderLink = serverUri;
        }
Ejemplo n.º 12
0
        public MegaUpdater(Uri serverUri, int discoveryPriority, int downloadPriority = 10, NetworkCredential credentials = null) : base(serverUri.OriginalString, discoveryPriority, downloadPriority)
        {
            if (serverUri == null)
            {
                throw new ArgumentNullException(nameof(serverUri));
            }
            if (serverUri.Host.ToLower() != "mega.nz")
            {
                throw new NotSupportedException("The link doesn't point to mega.nz - " + serverUri);
            }

            _client = new MegaApiClient();
            _client.ApiRequestFailed += (sender, args) => Console.WriteLine($@"MEGA API ERROR: {args.ApiResult}   {args.Exception}");

            _currentFolderLink = serverUri;

            try
            {
                if (credentials != null)
                {
                    _authInfos = _client.GenerateAuthInfos(credentials.UserName, credentials.Password);
                }
                else if (!string.IsNullOrWhiteSpace(Settings.Default.mega_authInfos))
                {
                    _authInfos = JsonConvert.DeserializeObject <MegaApiClient.AuthInfos>(Settings.Default.mega_authInfos);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to create MegaApiClient.AuthInfos - " + ex.ToStringDemystified());
            }

            try
            {
                if (!string.IsNullOrWhiteSpace(Settings.Default.mega_sessionToken))
                {
                    _loginToken = JsonConvert.DeserializeObject <MegaApiClient.LogonSessionToken>(Settings.Default.mega_sessionToken);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to read stored MegaApiClient.LogonSessionToken - " + ex.ToStringDemystified());
            }
        }
Ejemplo n.º 13
0
        private void Button2_Click(object sender, EventArgs e)
        {
            void Fail(string s) => MessageBox.Show(this, s, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (!_chrs.ContainsKey(comboBox1.SelectedIndex))
            {
                Fail("You must select a drive letter for the target drive.");
            }
            else
            {
                MegaApiClient.AuthInfos auth;

                if (_settings.Hash?.Any() ?? false)
                {
                    auth = new MegaApiClient.AuthInfos(_settings.Email, _settings.Hash, _settings.AES ?? new byte[0]);
                }
                else
                {
                    auth = MegaApiClient.GenerateAuthInfos(textBox1.Text, maskedTextBox1.Text);
                }

                try
                {
                    _settings.LastToken   = _mega.Login(auth);
                    _settings.Hash        = auth.Hash;
                    _settings.Email       = auth.Email;
                    _settings.AES         = auth.PasswordAesKey;
                    _settings.Save        = checkBox1.Checked;
                    _settings.Drive       = _chrs[comboBox1.SelectedIndex];
                    _settings.DeleteCache = checkBox2.Checked;
                    _settings.CacheSz     = (int)numericUpDown1.Value;

                    Close();
                }
                catch
                {
                    _settings.LastToken = null;

                    Fail("The combination of Email-address and password is invalid.");
                }
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Generates a JSON file with login encrypted data to use with Mega Api
 /// </summary>
 /// <returns>true if everything went ok</returns>
 public static bool GetAuthInfo()
 {
     try
     {
         MegaApiClient           client = new MegaApiClient();
         AuthInfos               AU     = MegaApiClient.GenerateAuthInfos("*****@*****.**", "password"); //Your email and pass used to create the mega account
         MegaApiClient.AuthInfos AF     = AU;                                                                // = new MegaApiClient.AuthInfos()
         MessageBox.Show(AF.PasswordAesKey.ToString());
         string  json = JsonConvert.SerializeObject(AF);
         JObject Auth = (JObject)JToken.FromObject(AF);
         // write JSON directly to a file
         using (StreamWriter file = File.CreateText("C:/Auth.json")) //location where the json file is going to be saved
             using (JsonTextWriter writer = new JsonTextWriter(file))
             {
                 Auth.WriteTo(writer);
             }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Ejemplo n.º 15
0
        private static void SaveRefreshToken(string account, MegaApiClient.AuthInfos refreshToken, string settingsPassPhrase)
        {
            var refreshTokens = Properties.Settings.Default.RefreshTokens;

            if (refreshTokens != null)
            {
                var setting = refreshTokens.SingleOrDefault(s => s.Account == account);
                if (setting != null)
                {
                    refreshTokens.Remove(setting);
                }
            }
            else
            {
                refreshTokens = Properties.Settings.Default.RefreshTokens = new System.Collections.ObjectModel.Collection <RefreshTokenSetting>();
            }

            refreshTokens.Insert(0, new RefreshTokenSetting()
            {
                Account = account, EMail = refreshToken.Email.EncryptUsing(settingsPassPhrase), Hash = refreshToken.Hash.EncryptUsing(settingsPassPhrase), PasswordAesKey = Encoding.Unicode.GetString(refreshToken.PasswordAesKey).EncryptUsing(settingsPassPhrase)
            });

            Properties.Settings.Default.Save();
        }
Ejemplo n.º 16
0
        private async Task ConnectImpl(bool askToLogin)
        {
            if (_client.IsLoggedIn)
            {
                if (askToLogin && !_anonymous && _loginToken == null)
                {
                    await _client.LogoutAsync();

                    goto retryLoginWithAuth;
                }
                return;
            }

            if (_loginToken != null)
            {
                try
                {
                    await _client.LoginAsync(_loginToken);

                    if (_client.IsLoggedIn)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    _loginToken = null;
                    Console.WriteLine($"Failed to log in to mega with token, retrying full login - {ex.ToStringDemystified()}");
                }
            }

retryLoginWithAuth:
            if (_authInfos != null)
            {
                _loginToken = await _client.LoginAsync(_authInfos);

                if (_client.IsLoggedIn)
                {
                    return;
                }
            }

            if (_anonymous || !askToLogin)
            {
                await _client.LoginAnonymousAsync();

                if (_client.IsLoggedIn)
                {
                    return;
                }
            }

            if (!askToLogin)
            {
                return;
            }

            var result = MegaLoginWindow.ShowDialog(_authInfos?.Email, _client);

            if (result == null)
            {
                throw new OperationCanceledException();
            }

            _authInfos  = result.Item1;
            _loginToken = result.Item2;
            _anonymous  = _loginToken == null;

            try
            {
                Settings.Default.mega_sessionToken = string.Empty;
                if (_loginToken != null)
                {
                    Settings.Default.mega_sessionToken = JsonConvert.SerializeObject(_loginToken);
                }
                Settings.Default.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to save MegaApiClient.LogonSessionToken - " + ex.ToStringDemystified());
            }
            try
            {
                Settings.Default.mega_authInfos = string.Empty;
                if (_authInfos != null)
                {
                    Settings.Default.mega_authInfos = JsonConvert.SerializeObject(_authInfos);
                }
                Settings.Default.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to save new MegaApiClient.AuthInfos - " + ex.ToStringDemystified());
            }

            if (!_client.IsLoggedIn)
            {
                goto retryLoginWithAuth;
            }
        }
Ejemplo n.º 17
0
 public Task <MegaApiClient.LogonSessionToken> LoginAsync(MegaApiClient.AuthInfos authInfos)
 {
     return(this.client.LoginAsync(authInfos));
 }
Ejemplo n.º 18
0
 public MegaApiClient.LogonSessionToken Login(MegaApiClient.AuthInfos authInfos)
 {
     return(this.UnwrapException(() => this.client.LoginAsync(authInfos).Result));
 }
 public Task <MegaApiClient.LogonSessionToken> LoginAsync(MegaApiClient.AuthInfos authInfos)
 {
     throw new NotImplementedException();
 }
 public Task LoginAsync(MegaApiClient.AuthInfos authInfos)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 21
0
 public Mega(MegaApiClient.AuthInfos authInfos) : this(authInfos, null)
 {
 }
 public void Login(MegaApiClient.AuthInfos authInfos)
 {
     this.UnwrapException(() => this.client.LoginAsync(authInfos).Wait());
 }
Ejemplo n.º 23
0
 public MegaAuthInfos(MegaApiClient.AuthInfos authInfos)
 {
     Email          = authInfos.Email;
     Hash           = authInfos.Hash;
     PasswordAesKey = Convert.ToBase64String(authInfos.PasswordAesKey);
 }