private void Button_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         _ = ShieldClient.CreateInstance(ApiKeyBox.Password);
         _viewModel.IsValidClient        = true;
         ExtensionConfiguration.ApiToken = ApiKeyBox.Password;
         ShieldControl.SelectedIndex     = 0;
         SaveExtensionConfiguration();
     }
     catch (Exception)
     {
         _viewModel.IsValidClient = false;
         MessageBox.Show("The api key is not valid, check that it has not been revoked and the associated scopes.", "Invalid Shield API Key", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        public ConfigurationWindowControl(ConfigurationViewModel viewModel)
        {
            InitializeComponent();

            _viewModel  = viewModel;
            DataContext = viewModel;

            LocalStorage = new SecureLocalStorage(
                new CustomLocalStorageConfig(null, "DotnetsaferShieldForVisualStudio").WithDefaultKeyBuilder()
                );

            ExtensionConfiguration = LocalStorage.Exists(ExtensionConfigurationFile) ?
                                     LocalStorage.Get <ShieldExtensionConfiguration>(ExtensionConfigurationFile) :
                                     new ShieldExtensionConfiguration();

            if (!string.IsNullOrEmpty(ExtensionConfiguration.ApiToken))
            {
                try
                {
                    _ = ShieldClient.CreateInstance(ExtensionConfiguration.ApiToken);
                    _viewModel.IsValidClient = true;
                    ApiKeyBox.Password       = ExtensionConfiguration.ApiToken;
                    ConnectButton.Content    = ExtensionConfiguration.ApiToken != ApiKeyBox.Password ? "Connect and save" : "Retry connection";
                }
                catch (Exception)
                {
                    _viewModel.IsValidClient = false;
                }
            }
            else
            {
                _viewModel.IsValidClient = false;
            }

            if (!_viewModel.IsValidClient)
            {
                ShieldControl.SelectedIndex = 1;
            }
        }
示例#3
0
        private bool TryConnectShield()
        {
            if (ShieldApiClient != null)
            {
                return(ShieldApiClient.CheckConnection(out _));
            }

            if (!TryReloadStorage())
            {
                return(false);
            }

            try
            {
                ShieldApiClient = ShieldClient.CreateInstance(ExtensionConfiguration.ApiToken);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
        internal async Task RunWithQueues()
        {
            _logger.LogInformation("Application Started at {dateTime}", DateTime.UtcNow);

            const string directory =
                @"C:\Users\juana\source\repos\Ejemplo\bin\Debug\netcoreapp3.1\";

            var appPath = $"{directory}Ejemplo.dll";

            var save = $"{directory}Ejemplo_protegida.dll";

            var dependencies = Directory.GetFiles($"{directory}").ToList();

            var client = ShieldClient.CreateInstance(
                "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjE4ODgzMmEyLTUxODktNDMwZS05NGFmLTc3MTJkZTBiM2FmZCIsInVuaXF1ZV9uYW1lIjoiOTE4ZDgxNmYtZDI4Zi00YThjLWE3MWItMzZiM2VkYTdlNjY4IiwidmVyc2lvbiI6IjEuMC4wIiwic2VydmljZSI6ImRvdG5ldHNhZmVyIiwiZWRpdGlvbiI6ImNvbW11bml0eSIsImp0aSI6IjY5YTlkNjdiLWM4ZTgtNGNhYS05MWM3LTk5NDIwZGE2ZDU5YyIsImV4cCI6MTYxNzQwMjg1Mn0.Ohr4WeJaU5w_2CP1QhzAepis_xKmDheLYxz4BN2rLEo"
                , _logger);

            var projectTest = await client.Project.FindOrCreateExternalProjectAsync("hola");

            var uploadApplicationDirectly = await client.Application.UploadApplicationDirectlyAsync(projectTest.Key, appPath, null);

            if (uploadApplicationDirectly.RequiresDependencies)
            {
                uploadApplicationDirectly.RequiredDependencies.ForEach(dependency =>
                                                                       _logger.LogCritical($"Dependency ${dependency} is required."));
                throw new Exception("Some dependencies are required.");
            }

            if (string.IsNullOrEmpty(uploadApplicationDirectly.ApplicationBlob))
            {
                throw new Exception("Unknown exception uploading application.");
            }

            var appBlob = uploadApplicationDirectly.ApplicationBlob;

            var connection = client.Connector.CreateQueueConnection();

            var taskConnection = client.Connector.InstanceQueueConnectorWithLogger(connection);

            await taskConnection.StartAsync();

            var config = client.Configuration.FindApplicationConfiguration(
                directory,
                Path.GetFileName(appPath)) ??
                         client.Configuration.MakeApplicationCustomConfiguration("consts mutation");

            var protect = await client.Tasks.ProtectSingleFileAsync(
                projectTest.Key,
                appBlob,
                connection,
                config,
                connection.OnLogger);

            protect.OnError(taskConnection, error => _logger.LogCritical(error));

            protect.OnClose(taskConnection, _ => _logger.LogDebug("Connection close. Task finished."));

            protect.OnSuccess(taskConnection, async dto =>
                              await(await client.Application.DownloadApplicationAsArrayAsync(dto))
                              .SaveOnAsync(save, true)
                              .ContinueWith(_ => _logger.LogInformation($"La aplicación protegida ha sido guardada en {save}"))
                              );

            System.Console.ReadKey();
        }