Beispiel #1
0
        public string AddComponentToken(string tenantName, string productName, string componentName, ComponentToken componentToken, bool shoudGenerateToken = true)
        {
            List <TenantConfiguration> tenants = TenantIOReader.ReadTenantsFromConfigFile();
            var tenant = tenants.Find(x => x.Name == tenantName);

            if (tenant == null)
            {
                return(null);
            }

            var product = tenant.Products.Find(x => x.Name == productName);

            if (product == null)
            {
                return(null);
            }

            var component = product.Components.Find(x => x.Name == componentName);

            if (component == null)
            {
                return(null);
            }

            if (shoudGenerateToken == true)
            {
                string apiKey = KeyGenerators.GenerateApiKey();
                componentToken.Token = apiKey;
            }

            component.Settings.Tokens.Add(componentToken);

            // store token in memory!
            _tenantRepository.AddComponentToken(tenantName, productName, componentName, componentToken);

            // Write into file
            if (TenantIOWriter.WriteTenantsConfiguration(tenants) == true)
            {
                if (shoudGenerateToken == true)
                {
                    // Send to the Cluster
                    _storageHubService.SendCreateComponentTokenStorage(new Model.Storages.Requests.Components.CreateComponentTokenDetails()
                    {
                        Tenant    = tenantName,
                        Product   = productName,
                        Component = componentName,
                        Token     = componentToken,

                        StoragesAlreadySent = new List <string>()
                    });
                }
                return(componentToken.Token);
            }

            return(null);
        }
Beispiel #2
0
        public string AddToken(string tenantName, DateTime expireDate)
        {
            List <TenantConfiguration> tenants = TenantIOReader.ReadTenantsFromConfigFile();
            var tenant = tenants.Find(x => x.Name == tenantName);

            if (tenant == null)
            {
                return(null);
            }

            string apiKey = KeyGenerators.GenerateApiKey();

            var tenantToken = new TenantToken()
            {
                ExpireDate = expireDate,
                Token      = apiKey,
                IsActive   = true,
                IssuedDate = DateTime.Now,
                IssuedFor  = $"Issued for tenant {tenantName}"
            };

            tenant.Settings.Tokens.Add(tenantToken);
            _tenantRepository.AddTenantToken(tenantName, tenantToken);

            // Write into file
            if (TenantIOWriter.WriteTenantsConfiguration(tenants) == true)
            {
                // Send to the Cluster
                _storageHubService.SendCreateTenantTokenStorage(new Model.Storages.Requests.Tenants.CreateTenantTokenDetails()
                {
                    Tenant = tenantName,
                    Token  = tenantToken,
                    StoragesAlreadySent = new List <string>()
                });

                return(apiKey);
            }

            return(null);
        }