Esempio n. 1
0
        private static async Task <Authenticator> GetAuthenticator()
        {
            while (true)
            {
                var a = await TrySecureStore();

                if (a == null)
                {
                    Console.Write("Username:"******"Password:"******"Wrong username or password");
                }
                else
                {
                    return(a);
                }
            }
        }
Esempio n. 2
0
        private static async Task <Authenticator> TrySecureStore()
        {
            var secureStore = new SecureStore();
            var username    = secureStore.ReadKey(keyUsername);
            var password    = secureStore.ReadKey(keyPassword);

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            var token = secureStore.ReadKey(keyToken);

            var authenticator = new Authenticator(username, password, token);

            if (string.IsNullOrEmpty(token))
            {
                var result = await authenticator.Authenticate();

                if (result)
                {
                    Console.Write("Please type code:");
                    var code = Console.ReadLine();
                    Console.WriteLine();

                    await authenticator.TwoFactorAuthentication(code);

                    secureStore.SetKey(keyToken, authenticator.AccessToken);

                    return(authenticator);
                }

                secureStore.SetKey(keyUsername, "");
                secureStore.SetKey(keyPassword, "");
                return(null);
            }

            try
            {
                var d = new DeviceManager(authenticator);

                var deviceList = await d.GetDevices();

                if (deviceList == null && !string.IsNullOrEmpty(token))
                {
                    secureStore.SetKey(keyToken, string.Empty);
                    return(await TrySecureStore());
                }

                if (deviceList != null)
                {
                    return(authenticator);
                }
            }
            catch
            {
                return(null);
            }

            return(null);
        }