diff --git a/Manager.Framework/Business/ProfileLogic.cs b/Manager.Framework/Business/ProfileLogic.cs index 4da450d..5a56304 100644 --- a/Manager.Framework/Business/ProfileLogic.cs +++ b/Manager.Framework/Business/ProfileLogic.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using Manager.Framework.Helpers; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; @@ -10,6 +11,11 @@ namespace Manager.Framework.Business { private readonly ILogger _logger; + /// + /// Scrypt algorithm pepper + /// + private const string PasswordsPepper = "m6rOay9Sg8pDGFRyHVWBWLZ8DahGdYgX"; + public ProfileLogic(ILogger logger) : base() { @@ -31,5 +37,15 @@ namespace Manager.Framework.Business return true; } + + /// + /// Hash a password + /// + /// Password to hash + /// Hashed password + public string HashPassword(string password) + { + return PasswordUtils.Encode(password, PasswordsPepper); + } } } diff --git a/Manager.Framework/Helpers/Passwords/PasswordUtils.cs b/Manager.Framework/Helpers/Passwords/PasswordUtils.cs new file mode 100644 index 0000000..db264ba --- /dev/null +++ b/Manager.Framework/Helpers/Passwords/PasswordUtils.cs @@ -0,0 +1,170 @@ +using Scrypt; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; + +namespace Manager.Framework.Helpers +{ + /// + /// Password utils + /// + public static class PasswordUtils + { + private const int DefaultIterationCount = 16384; + private const int DefaultBlockSize = 8; + private const int DefaultThreadCount = 1; + + private static readonly Random s_random = new Random((int)(DateTimeOffset.Now.Ticks % 100000)); + + #region Secure password management + + /// + /// Generate a random key of the specified length (source: https://stackoverflow.com/a/31959204/249000) + /// + /// Length of the resulting key + /// Allowed characters for the resulting key + /// + public static string GetUniqueKey(int length = 32, string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") + { + using (var crypto = new RNGCryptoServiceProvider()) + { + var data = new byte[length]; + + // If chars.Length isn't a power of 2 then there is a bias if + // we simply use the modulus operator. The first characters of + // chars will be more probable than the last ones. + + // buffer used if we encounter an unusable random byte. We will + // regenerate it in this buffer + byte[] smallBuffer = null; + + // Maximum random number that can be used without introducing a + // bias + int maxRandom = byte.MaxValue - ((byte.MaxValue + 1) % chars.Length); + + crypto.GetBytes(data); + + var result = new char[length]; + + for (int i = 0; i < length; i++) + { + byte v = data[i]; + + while (v > maxRandom) + { + if (smallBuffer == null) + { + smallBuffer = new byte[1]; + } + + crypto.GetBytes(smallBuffer); + v = smallBuffer[0]; + } + + result[i] = chars[v % chars.Length]; + } + + return new string(result); + } + } + + /// + /// Encode the specified password with the specified pepper with the scrypt algorithm + /// + /// Password to encode + /// Pepper to use + /// scrypt algorithm iteration count + /// scrypt algorithm block size + /// scrypt algorithm thread count + /// Encoded password + public static string Encode(string password, string pepper = "", int iterationCount = DefaultIterationCount, int blockSize = DefaultBlockSize, int threadCount = DefaultThreadCount) + { + var encoder = new ScryptEncoder(iterationCount, blockSize, threadCount); + return encoder.Encode(pepper + password); + } + + /// + /// Compare the specified encoded password with the specified password with the specified pepper with the scrypt algorithm + /// + /// Hashed password + /// Password to compare + /// Pepper to use + /// scrypt algorithm iteration count + /// scrypt algorithm block size + /// scrypt algorithm thread count + /// + public static bool Compare(string hashedPassword, string password, string pepper = "", int iterationCount = DefaultIterationCount, int blockSize = DefaultBlockSize, int threadCount = DefaultThreadCount) + { + var encoder = new ScryptEncoder(iterationCount, blockSize, threadCount); + return encoder.Compare(pepper + password, hashedPassword); + } + + /// + /// Generate a random password + /// + /// Pepper to use + /// scrypt algorithm iteration count + /// scrypt algorithm block size + /// scrypt algorithm thread count + /// Generated password + public static string Generate(string pepper = "", int iterationCount = DefaultIterationCount, int blockSize = DefaultBlockSize, int threadCount = DefaultThreadCount) + { + return Encode(GetUniqueKey(), pepper, iterationCount, blockSize, threadCount); + } + + #endregion + + /// + /// Get a random string of the specified length + /// + /// Number of characters + /// Random string of the specified length + public static string GetRandomString(int length) + { + string pass = ""; + for (int i = 0; i < length; i++) + { + int r = s_random.Next(10 + 26 + 26); + char c; + if (r < 10) c = (char)(r + 48); + else if (r < 10 + 26) c = (char)((r - 10) + 65); + else c = (char)((r - 10 - 26) + 97); + pass += c; + } + return pass; + } + + /// + /// Get a random number with the specified maximum number of digits + /// + /// Maximum number of digits + /// Random number with the specified maximum number of digits + public static int GetRandomNumber(int maxDigits) + { + return s_random.Next((int)Math.Pow(10, maxDigits)); + } + + /// + /// Calculate the MD5 sum of the specified byte array and remove potential dashes + /// + /// Source array + /// Source array MD5 sum + public static string MD5Sum(byte[] source) + { + return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(source)).Replace("-", "").ToLower(); + } + + /// + /// Calculate the MD5 sum of the specified string and remove potential dashes + /// + /// Source string + /// Source string MD5 sum + public static string MD5Sum(string source) + { + return MD5Sum(Encoding.ASCII.GetBytes(source)); + } + } +} diff --git a/Manager.Framework/Manager.Framework.csproj b/Manager.Framework/Manager.Framework.csproj index 7e43e1d..2e573fe 100644 --- a/Manager.Framework/Manager.Framework.csproj +++ b/Manager.Framework/Manager.Framework.csproj @@ -6,6 +6,7 @@ + diff --git a/Manager.Interfaces/DTO/ConfigurationDTO.cs b/Manager.Interfaces/DTO/ConfigurationDTO.cs index 710a749..d162daa 100644 --- a/Manager.Interfaces/DTO/ConfigurationDTO.cs +++ b/Manager.Interfaces/DTO/ConfigurationDTO.cs @@ -6,11 +6,11 @@ namespace Manager.Interfaces.DTO { public class ConfigurationDTO { - public string Id { get; set; } - public string Label { get; set; } - public string PrimaryColor { get; set; } - public string SecondaryColor { get; set; } - public List Languages { get; set; } // fr, en, de, nl => Sélection dans une liste déjà établie dans l'application ! - public DateTime DateCreation { get; set; } + public string id { get; set; } + public string label { get; set; } + public string primaryColor { get; set; } + public string secondaryColor { get; set; } + public List languages { get; set; } // fr, en, de, nl => Sélection dans une liste déjà établie dans l'application ! + public DateTime dateCreation { get; set; } } } diff --git a/Manager.Interfaces/DTO/DeviceDTO.cs b/Manager.Interfaces/DTO/DeviceDTO.cs index b233281..0cd62f5 100644 --- a/Manager.Interfaces/DTO/DeviceDTO.cs +++ b/Manager.Interfaces/DTO/DeviceDTO.cs @@ -6,23 +6,23 @@ namespace Manager.Interfaces.DTO { public class DeviceDTO { - public string Id { get; set; } - public string Identifier { get; set; } - public string Name { get; set; } - public string IpAddressWLAN { get; set; } - public string IpAddressETH { get; set; } - public string ConfigurationId { get; set; } - public string Configuration { get; set; } - public bool Connected{ get; set; } - public DateTime DateCreation{ get; set; } - public DateTime DateUpdate { get; set; } + public string id { get; set; } + public string identifier { get; set; } + public string name { get; set; } + public string ipAddressWLAN { get; set; } + public string ipAddressETH { get; set; } + public string configurationId { get; set; } + public string configuration { get; set; } + public bool connected{ get; set; } + public DateTime dateCreation{ get; set; } + public DateTime dateUpdate { get; set; } } public class DeviceDetailDTO : DeviceDTO { - public string ConnectionLevel { get; set; } - public DateTime LastConnectionLevel { get; set; } - public string BatteryLevel { get; set; } - public DateTime LastBatteryLevel { get; set; } + public string connectionLevel { get; set; } + public DateTime lastConnectionLevel { get; set; } + public string batteryLevel { get; set; } + public DateTime lastBatteryLevel { get; set; } } } diff --git a/Manager.Interfaces/DTO/LoginDTO.cs b/Manager.Interfaces/DTO/LoginDTO.cs index 5e3cfc3..bda6d0f 100644 --- a/Manager.Interfaces/DTO/LoginDTO.cs +++ b/Manager.Interfaces/DTO/LoginDTO.cs @@ -2,7 +2,7 @@ namespace Manager.Interfaces.DTO { public class LoginDTO { - public string Email { get; set; } - public string Password { get; set; } + public string email { get; set; } + public string password { get; set; } } } diff --git a/Manager.Interfaces/DTO/ResourceDTO.cs b/Manager.Interfaces/DTO/ResourceDTO.cs index 9f2e9da..c9bb169 100644 --- a/Manager.Interfaces/DTO/ResourceDTO.cs +++ b/Manager.Interfaces/DTO/ResourceDTO.cs @@ -7,9 +7,9 @@ namespace Manager.Interfaces.DTO { public class ResourceDTO { - public string Id { get; set; } - public ResourceType Type { get; set; } - public string Label { get; set; } - public string Data { get; set; } + public string id { get; set; } + public ResourceType type { get; set; } + public string label { get; set; } + public string data { get; set; } } } diff --git a/Manager.Interfaces/DTO/ResourceDetailDTO.cs b/Manager.Interfaces/DTO/ResourceDetailDTO.cs index 9c15a33..ee392b0 100644 --- a/Manager.Interfaces/DTO/ResourceDetailDTO.cs +++ b/Manager.Interfaces/DTO/ResourceDetailDTO.cs @@ -7,10 +7,10 @@ namespace Manager.Interfaces.DTO { public class ResourceDetailDTO { - public string Id { get; set; } - public ResourceType Type { get; set; } - public string Label { get; set; } - public DateTime DateCreation { get; set; } - public string Data { get; set; } + public string id { get; set; } + public ResourceType type { get; set; } + public string label { get; set; } + public DateTime dateCreation { get; set; } + public string data { get; set; } } } diff --git a/Manager.Interfaces/DTO/SectionDTO.cs b/Manager.Interfaces/DTO/SectionDTO.cs index c54409b..273dadf 100644 --- a/Manager.Interfaces/DTO/SectionDTO.cs +++ b/Manager.Interfaces/DTO/SectionDTO.cs @@ -7,18 +7,18 @@ namespace Manager.Interfaces.DTO { public class SectionDTO { - public string Id { get; set; } - public string Label { get; set; } // use in manager - public List Title { get; set; } - public List Description { get; set; } - public string ImageId { get; set; } // == ResourceId - public string ImageSource { get; set; } // == Image url - public string ConfigurationId { get; set; } - public bool IsSubSection { get; set; } // true if part of menu type - public string ParentId { get; set; } // only if it's an subsection - public SectionType Type { get; set; } // !! If IsSubSection == true => Type can't not be menu ! - public string Data { get; set; } // == Include section type info - public DateTime DateCreation { get; set; } // == Include section type info - public int Order { get; set; } // Order to show + public string id { get; set; } + public string label { get; set; } // use in manager + public List title { get; set; } + public List description { get; set; } + public string imageId { get; set; } // == ResourceId + public string imageSource { get; set; } // == Image url + public string configurationId { get; set; } + public bool isSubSection { get; set; } // true if part of menu type + public string parentId { get; set; } // only if it's an subsection + public SectionType type { get; set; } // !! If IsSubSection == true => Type can't not be menu ! + public string data { get; set; } // == Include section type info + public DateTime dateCreation { get; set; } // == Include section type info + public int order { get; set; } // Order to show } } diff --git a/Manager.Interfaces/DTO/TranslationDTO.cs b/Manager.Interfaces/DTO/TranslationDTO.cs index 6ca5ef1..f311bb8 100644 --- a/Manager.Interfaces/DTO/TranslationDTO.cs +++ b/Manager.Interfaces/DTO/TranslationDTO.cs @@ -6,7 +6,7 @@ namespace Manager.Interfaces.DTO { public class TranslationDTO { - public string Language { get; set; } - public string Value { get; set; } + public string language { get; set; } + public string value { get; set; } } } diff --git a/Manager.Interfaces/DTO/UserDetailDTO.cs b/Manager.Interfaces/DTO/UserDetailDTO.cs index cb8b5fa..e9d0a5e 100644 --- a/Manager.Interfaces/DTO/UserDetailDTO.cs +++ b/Manager.Interfaces/DTO/UserDetailDTO.cs @@ -6,9 +6,9 @@ namespace Manager.Interfaces.DTO { public class UserDetailDTO { - public string Id { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } + public string id { get; set; } + public string email { get; set; } + public string firstName { get; set; } + public string lastName { get; set; } } } diff --git a/Manager.Interfaces/Models/Configuration.cs b/Manager.Interfaces/Models/Configuration.cs index b1412cb..aab5f8d 100644 --- a/Manager.Interfaces/Models/Configuration.cs +++ b/Manager.Interfaces/Models/Configuration.cs @@ -38,12 +38,12 @@ namespace Manager.Interfaces.Models { return new ConfigurationDTO() { - Id = Id, - Label = Label, - DateCreation = DateCreation, - PrimaryColor = PrimaryColor, - Languages = Languages, - SecondaryColor = SecondaryColor + id = Id, + label = Label, + dateCreation = DateCreation, + primaryColor = PrimaryColor, + languages = Languages, + secondaryColor = SecondaryColor }; } } diff --git a/Manager.Interfaces/Models/Device.cs b/Manager.Interfaces/Models/Device.cs index 7ba67d2..07997e3 100644 --- a/Manager.Interfaces/Models/Device.cs +++ b/Manager.Interfaces/Models/Device.cs @@ -65,16 +65,16 @@ namespace Manager.Interfaces.Models { return new DeviceDTO() { - Id = Id, - Identifier = Identifier, - Name = Name, - IpAddressWLAN = IpAddressWLAN, - IpAddressETH = IpAddressETH, - Connected = Connected, - Configuration = Configuration, - ConfigurationId = ConfigurationId, - DateUpdate = DateUpdate, - DateCreation = DateCreation + id = Id, + identifier = Identifier, + name = Name, + ipAddressWLAN = IpAddressWLAN, + ipAddressETH = IpAddressETH, + connected = Connected, + configuration = Configuration, + configurationId = ConfigurationId, + dateUpdate = DateUpdate, + dateCreation = DateCreation }; } @@ -82,20 +82,20 @@ namespace Manager.Interfaces.Models { return new DeviceDetailDTO() { - Id = Id, - Identifier = Identifier, - Name = Name, - IpAddressWLAN = IpAddressWLAN, - IpAddressETH = IpAddressETH, - Connected = Connected, - Configuration = Configuration, - ConfigurationId = ConfigurationId, - ConnectionLevel = ConnectionLevel, - LastConnectionLevel = LastConnectionLevel, - BatteryLevel = BatteryLevel, - LastBatteryLevel = LastBatteryLevel, - DateUpdate = DateUpdate, - DateCreation = DateCreation + id = Id, + identifier = Identifier, + name = Name, + ipAddressWLAN = IpAddressWLAN, + ipAddressETH = IpAddressETH, + connected = Connected, + configuration = Configuration, + configurationId = ConfigurationId, + connectionLevel = ConnectionLevel, + lastConnectionLevel = LastConnectionLevel, + batteryLevel = BatteryLevel, + lastBatteryLevel = LastBatteryLevel, + dateUpdate = DateUpdate, + dateCreation = DateCreation }; } } diff --git a/Manager.Interfaces/Models/Resource.cs b/Manager.Interfaces/Models/Resource.cs index 66b4611..46baeb5 100644 --- a/Manager.Interfaces/Models/Resource.cs +++ b/Manager.Interfaces/Models/Resource.cs @@ -35,10 +35,10 @@ namespace Manager.Interfaces.Models { return new ResourceDTO() { - Id = Id, - Label = Label, - Type = Type, - Data = Type != ResourceType.Image ? Data : null, + id = Id, + label = Label, + type = Type, + data = Type != ResourceType.Image ? Data : null, }; } @@ -46,11 +46,11 @@ namespace Manager.Interfaces.Models { return new ResourceDetailDTO() { - Id = Id, - Label = Label, - Type = Type, - Data = Data, - DateCreation = DateCreation + id = Id, + label = Label, + type = Type, + data = Data, + dateCreation = DateCreation }; } diff --git a/Manager.Interfaces/Models/Section.cs b/Manager.Interfaces/Models/Section.cs index a0c9912..7264285 100644 --- a/Manager.Interfaces/Models/Section.cs +++ b/Manager.Interfaces/Models/Section.cs @@ -65,19 +65,19 @@ namespace Manager.Interfaces.Models { return new SectionDTO() { - Id = Id, - Label = Label, - Title = Title.OrderBy(t => t.Language).ToList(), - Description = Description.OrderBy(d => d.Language).ToList(), - Order = Order, - Type = Type, - ImageId = ImageId, - ImageSource = ImageSource, - ConfigurationId = ConfigurationId, - IsSubSection = IsSubSection, - ParentId = ParentId, - Data = Data, - DateCreation = DateCreation + id = Id, + label = Label, + title = Title.OrderBy(t => t.language).ToList(), + description = Description.OrderBy(d => d.language).ToList(), + order = Order, + type = Type, + imageId = ImageId, + imageSource = ImageSource, + configurationId = ConfigurationId, + isSubSection = IsSubSection, + parentId = ParentId, + data = Data, + dateCreation = DateCreation }; } } diff --git a/Manager.Interfaces/Models/User.cs b/Manager.Interfaces/Models/User.cs index 5998f1b..c06d2c5 100644 --- a/Manager.Interfaces/Models/User.cs +++ b/Manager.Interfaces/Models/User.cs @@ -42,10 +42,10 @@ namespace Manager.Interfaces.Models { return new UserDetailDTO() { - Id = Id, - Email = Email, - FirstName = FirstName, - LastName = LastName, + id = Id, + email = Email, + firstName = FirstName, + lastName = LastName, }; } diff --git a/ManagerService/Controllers/AuthenticationController.cs b/ManagerService/Controllers/AuthenticationController.cs index fb31ee2..195928c 100644 --- a/ManagerService/Controllers/AuthenticationController.cs +++ b/ManagerService/Controllers/AuthenticationController.cs @@ -105,7 +105,7 @@ namespace ManagerService.Service.Controllers [ProducesResponseType(typeof(string), (int)HttpStatusCode.InternalServerError)] public ObjectResult AuthenticateWithJson([FromBody] LoginDTO login) { - return Authenticate(login.Email.ToLower(), login.Password); + return Authenticate(login.email.ToLower(), login.password); } } } diff --git a/ManagerService/Controllers/ConfigurationController.cs b/ManagerService/Controllers/ConfigurationController.cs index 8ae1fa4..61648b3 100644 --- a/ManagerService/Controllers/ConfigurationController.cs +++ b/ManagerService/Controllers/ConfigurationController.cs @@ -99,9 +99,9 @@ namespace ManagerService.Controllers // Todo add some verification ? Configuration configuration = new Configuration(); - configuration.Label = newConfiguration.Label; - configuration.PrimaryColor = newConfiguration.PrimaryColor; - configuration.SecondaryColor = newConfiguration.SecondaryColor; + configuration.Label = newConfiguration.label; + configuration.PrimaryColor = newConfiguration.primaryColor; + configuration.SecondaryColor = newConfiguration.secondaryColor; configuration.Languages = new List { "FR", "NL", "EN", "DE" }; // by default all languages configuration.DateCreation = DateTime.Now; @@ -140,18 +140,18 @@ namespace ManagerService.Controllers if (updatedConfiguration == null) throw new ArgumentNullException("configuration param is null"); - Configuration configuration = _configurationService.GetById(updatedConfiguration.Id); + Configuration configuration = _configurationService.GetById(updatedConfiguration.id); if (configuration == null) throw new KeyNotFoundException("Configuration does not exist"); // Todo add some verification ? - configuration.Label = updatedConfiguration.Label; - configuration.PrimaryColor = updatedConfiguration.PrimaryColor; - configuration.SecondaryColor = updatedConfiguration.SecondaryColor; - configuration.Languages = updatedConfiguration.Languages; + configuration.Label = updatedConfiguration.label; + configuration.PrimaryColor = updatedConfiguration.primaryColor; + configuration.SecondaryColor = updatedConfiguration.secondaryColor; + configuration.Languages = updatedConfiguration.languages; - Configuration configurationModified = _configurationService.Update(updatedConfiguration.Id, configuration); + Configuration configurationModified = _configurationService.Update(updatedConfiguration.id, configuration); MqttClientService.PublishMessage($"config/{configurationModified.Id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); diff --git a/ManagerService/Controllers/DeviceController.cs b/ManagerService/Controllers/DeviceController.cs index 35484e0..4601688 100644 --- a/ManagerService/Controllers/DeviceController.cs +++ b/ManagerService/Controllers/DeviceController.cs @@ -103,36 +103,36 @@ namespace ManagerService.Controllers if (newDevice == null) throw new ArgumentNullException("Device param is null"); - var configuration = _configurationService.GetById(newDevice.ConfigurationId); + var configuration = _configurationService.GetById(newDevice.configurationId); if (configuration == null) throw new KeyNotFoundException("Configuration does not exist"); Device device = new Device(); - if (_deviceService.IsExistIdentifier(newDevice.Identifier)) + if (_deviceService.IsExistIdentifier(newDevice.identifier)) { // Update info - device = _deviceService.GetByIdentifier(newDevice.Identifier); + device = _deviceService.GetByIdentifier(newDevice.identifier); device.DateUpdate = DateTime.Now; } else { // Creation - device.Identifier = newDevice.Identifier; + device.Identifier = newDevice.identifier; device.DateCreation = DateTime.Now; } - device.Name = newDevice.Name; + device.Name = newDevice.name; device.Configuration = configuration.Label; - device.ConfigurationId = newDevice.ConfigurationId; - device.IpAddressETH = newDevice.IpAddressETH; - device.IpAddressWLAN = newDevice.IpAddressWLAN; - device.Connected = newDevice.Connected; - device.ConnectionLevel = newDevice.ConnectionLevel; - device.LastConnectionLevel = newDevice.LastConnectionLevel; - device.BatteryLevel = newDevice.BatteryLevel; - device.LastBatteryLevel = newDevice.LastBatteryLevel; + device.ConfigurationId = newDevice.configurationId; + device.IpAddressETH = newDevice.ipAddressETH; + device.IpAddressWLAN = newDevice.ipAddressWLAN; + device.Connected = newDevice.connected; + device.ConnectionLevel = newDevice.connectionLevel; + device.LastConnectionLevel = newDevice.lastConnectionLevel; + device.BatteryLevel = newDevice.batteryLevel; + device.LastBatteryLevel = newDevice.lastBatteryLevel; - Device deviceCreated = _deviceService.IsExistIdentifier(newDevice.Identifier) ? _deviceService.Update(device.Id, device) : _deviceService.Create(device); + Device deviceCreated = _deviceService.IsExistIdentifier(newDevice.identifier) ? _deviceService.Update(device.Id, device) : _deviceService.Create(device); return new OkObjectResult(deviceCreated.ToDTO()); } @@ -171,23 +171,23 @@ namespace ManagerService.Controllers if (updatedDevice == null) throw new ArgumentNullException("Device param is null"); - Device device = _deviceService.GetById(updatedDevice.Id); + Device device = _deviceService.GetById(updatedDevice.id); if (device == null) throw new KeyNotFoundException("Device does not exist"); // Todo add some verification ? - device.Name = updatedDevice.Name; - device.Identifier = updatedDevice.Identifier; - device.IpAddressWLAN = updatedDevice.IpAddressWLAN; - device.IpAddressETH = updatedDevice.IpAddressETH; - device.Connected = updatedDevice.Connected; - device.ConnectionLevel = updatedDevice.ConnectionLevel; - device.LastConnectionLevel = updatedDevice.LastConnectionLevel; - device.BatteryLevel = updatedDevice.BatteryLevel; - device.LastBatteryLevel = updatedDevice.LastBatteryLevel; + device.Name = updatedDevice.name; + device.Identifier = updatedDevice.identifier; + device.IpAddressWLAN = updatedDevice.ipAddressWLAN; + device.IpAddressETH = updatedDevice.ipAddressETH; + device.Connected = updatedDevice.connected; + device.ConnectionLevel = updatedDevice.connectionLevel; + device.LastConnectionLevel = updatedDevice.lastConnectionLevel; + device.BatteryLevel = updatedDevice.batteryLevel; + device.LastBatteryLevel = updatedDevice.lastBatteryLevel; - Device deviceModified = _deviceService.Update(updatedDevice.Id, device); + Device deviceModified = _deviceService.Update(updatedDevice.id, device); return new OkObjectResult(deviceModified.ToDTO()); } @@ -221,21 +221,21 @@ namespace ManagerService.Controllers if (deviceIn == null) throw new ArgumentNullException("Device param is null"); - Device device = _deviceService.GetById(deviceIn.Id); + Device device = _deviceService.GetById(deviceIn.id); if (device == null) throw new KeyNotFoundException("Device does not exist"); - var configuration = _configurationService.GetById(deviceIn.ConfigurationId); + var configuration = _configurationService.GetById(deviceIn.configurationId); if (configuration == null) throw new KeyNotFoundException("Configuration does not exist"); // Todo add some verification ? - device.Name = deviceIn.Name; - device.Connected = deviceIn.Connected; + device.Name = deviceIn.name; + device.Connected = deviceIn.connected; device.Configuration = configuration.Label; - device.ConfigurationId = deviceIn.ConfigurationId; + device.ConfigurationId = deviceIn.configurationId; Device deviceModified = _deviceService.Update(device.Id, device); diff --git a/ManagerService/Controllers/ResourceController.cs b/ManagerService/Controllers/ResourceController.cs index 7cf78ef..62da95c 100644 --- a/ManagerService/Controllers/ResourceController.cs +++ b/ManagerService/Controllers/ResourceController.cs @@ -144,8 +144,8 @@ namespace ManagerService.Controllers /*if (uploadResource == null) throw new ArgumentNullException("Resource param is null");*/ ResourceDetailDTO uploadResource = new ResourceDetailDTO(); - uploadResource.Type = (ResourceType) Enum.Parse(typeof(ResourceType), type); - uploadResource.Label = label; + uploadResource.type = (ResourceType) Enum.Parse(typeof(ResourceType), type); + uploadResource.label = label; var file = Request.Form.Files[0]; @@ -163,8 +163,8 @@ namespace ManagerService.Controllers } // Todo add some verification ? Resource resource = new Resource(); - resource.Label = uploadResource.Label; - resource.Type = uploadResource.Type; + resource.Label = uploadResource.label; + resource.Type = uploadResource.type; resource.DateCreation = DateTime.Now; resource.Data = stringResult; Resource resourceCreated = _resourceService.Create(resource); @@ -207,10 +207,10 @@ namespace ManagerService.Controllers // Todo add some verification ? Resource resource = new Resource(); - resource.Label = newResource.Label; - resource.Type = newResource.Type; + resource.Label = newResource.label; + resource.Type = newResource.type; resource.DateCreation = DateTime.Now; - resource.Data = newResource.Data; + resource.Data = newResource.data; Resource resourceCreated = _resourceService.Create(resource); @@ -247,17 +247,17 @@ namespace ManagerService.Controllers if (updatedResource == null) throw new ArgumentNullException("Resource param is null"); - Resource resource = _resourceService.GetById(updatedResource.Id); + Resource resource = _resourceService.GetById(updatedResource.id); if (resource == null) throw new KeyNotFoundException("Resource does not exist"); // Todo add some verification ? - resource.Label = updatedResource.Label; - resource.Type = updatedResource.Type; - resource.Data = updatedResource.Data; + resource.Label = updatedResource.label; + resource.Type = updatedResource.type; + resource.Data = updatedResource.data; - Resource resourceModified = _resourceService.Update(updatedResource.Id, resource); + Resource resourceModified = _resourceService.Update(updatedResource.id, resource); return new OkObjectResult(resourceModified.ToDTO()); } diff --git a/ManagerService/Controllers/SectionController.cs b/ManagerService/Controllers/SectionController.cs index 2ce3e94..decf860 100644 --- a/ManagerService/Controllers/SectionController.cs +++ b/ManagerService/Controllers/SectionController.cs @@ -216,25 +216,25 @@ namespace ManagerService.Controllers if (newSection == null) throw new ArgumentNullException("Section param is null"); - if (newSection.ConfigurationId == null) + if (newSection.configurationId == null) throw new ArgumentNullException("Configuration param is null"); - if (!_configurationService.IsExist(newSection.ConfigurationId) ) + if (!_configurationService.IsExist(newSection.configurationId) ) throw new KeyNotFoundException("Configuration does not exist"); // Todo add some verification ? Section section = new Section(); - section.Label = newSection.Label; - section.ImageId = newSection.ImageId; - section.ImageSource = newSection.ImageSource; - section.ConfigurationId = newSection.ConfigurationId; + section.Label = newSection.label; + section.ImageId = newSection.imageId; + section.ImageSource = newSection.imageSource; + section.ConfigurationId = newSection.configurationId; section.DateCreation = DateTime.Now; - section.IsSubSection = newSection.IsSubSection; - section.ParentId = newSection.ParentId; - section.Type = newSection.Type; + section.IsSubSection = newSection.isSubSection; + section.ParentId = newSection.parentId; + section.Type = newSection.type; section.Title = new List(); section.Description = new List(); - section.Order = _sectionService.GetAllFromConfiguration(newSection.ConfigurationId).Count; + section.Order = _sectionService.GetAllFromConfiguration(newSection.configurationId).Count; // Preparation List languages = new List { "FR", "NL", "EN", "DE" };//_configurationService.GetById(newSection.ConfigurationId).Languages; @@ -245,35 +245,35 @@ namespace ManagerService.Controllers { TranslationDTO title = new TranslationDTO(); TranslationDTO description = new TranslationDTO(); - title.Language = language.ToUpper(); - description.Language = language.ToUpper(); + title.language = language.ToUpper(); + description.language = language.ToUpper(); switch (language.ToUpper()) { case "FR": - title.Value = "Titre en français"; - description.Value = "Description en français"; + title.value = "Titre en français"; + description.value = "Description en français"; break; case "EN": - title.Value = "Title in english"; - description.Value = "Description en anglais"; + title.value = "Title in english"; + description.value = "Description en anglais"; break; case "NL": - title.Value = "Titre in dutch"; - description.Value = "Description en néerlandais"; + title.value = "Titre in dutch"; + description.value = "Description en néerlandais"; break; case "DE": - title.Value = "Titre en allemand"; - description.Value = "Description en allemand"; + title.value = "Titre en allemand"; + description.value = "Description en allemand"; break; } section.Title.Add(title); section.Description.Add(description); } - section.Title = section.Title.OrderBy(t => t.Language).ToList(); - section.Description = section.Description.OrderBy(d => d.Language).ToList(); + section.Title = section.Title.OrderBy(t => t.language).ToList(); + section.Description = section.Description.OrderBy(d => d.language).ToList(); - switch (newSection.Type) { + switch (newSection.type) { case SectionType.Map: mapDTO = new MapDTO(); mapDTO.mapType = MapTypeApp.hybrid; @@ -316,6 +316,30 @@ namespace ManagerService.Controllers case SectionType.Menu: MenuDTO menuDTO = new MenuDTO(); menuDTO.sections = new List(); + /*SectionDTO section0DTO = new SectionDTO(); + section0DTO.IsSubSection = true; + section0DTO.Label = newSection.Label; + section0DTO.ImageId = newSection.ImageId; + section0DTO.ConfigurationId = newSection.ConfigurationId; + section0DTO.DateCreation = DateTime.Now; + section0DTO.ParentId = null; + section0DTO.Type = SectionType.Map; + section0DTO.Data = JsonConvert.SerializeObject(mapDTO); + + + SectionDTO section1DTO = new SectionDTO(); + section0DTO.IsSubSection = true; + section0DTO.Label = newSection.Label; + section0DTO.ImageId = newSection.ImageId; + section0DTO.ConfigurationId = newSection.ConfigurationId; + section0DTO.DateCreation = DateTime.Now; + section0DTO.ParentId = null; + section0DTO.Type = SectionType.Slider; + section1DTO.IsSubSection = true; + section1DTO.Data = JsonConvert.SerializeObject(sliderDTO);*/ + + /*menuDTO.Sections.Add(section0DTO); + menuDTO.Sections.Add(section1DTO);*/ section.Data = JsonConvert.SerializeObject(menuDTO); // Include all info from specific section as JSON break; } @@ -406,24 +430,24 @@ namespace ManagerService.Controllers if (updatedSection == null) throw new ArgumentNullException("Section param is null"); - Section section = _sectionService.GetById(updatedSection.Id); + Section section = _sectionService.GetById(updatedSection.id); if (section == null) throw new KeyNotFoundException("Section does not exist"); // Todo add some verification ? - section.Label = updatedSection.Label; - section.Title = updatedSection.Title; - section.Description = updatedSection.Description; - section.Type = updatedSection.Type; - section.ImageId = updatedSection.ImageId; - section.ImageSource = updatedSection.ImageSource; - section.ConfigurationId = updatedSection.ConfigurationId; - section.IsSubSection = updatedSection.IsSubSection; - section.ParentId = updatedSection.ParentId; - section.Data = updatedSection.Data; + section.Label = updatedSection.label; + section.Title = updatedSection.title; + section.Description = updatedSection.description; + section.Type = updatedSection.type; + section.ImageId = updatedSection.imageId; + section.ImageSource = updatedSection.imageSource; + section.ConfigurationId = updatedSection.configurationId; + section.IsSubSection = updatedSection.isSubSection; + section.ParentId = updatedSection.parentId; + section.Data = updatedSection.data; - Section sectionModified = _sectionService.Update(updatedSection.Id, section); + Section sectionModified = _sectionService.Update(updatedSection.id, section); MqttClientService.PublishMessage($"config/{sectionModified.ConfigurationId}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); @@ -461,20 +485,20 @@ namespace ManagerService.Controllers foreach (var section in updatedSectionsOrder) { - if (!_sectionService.IsExist(section.Id)) - throw new KeyNotFoundException($"Section {section.Label} with id {section.Id} does not exist"); + if (!_sectionService.IsExist(section.id)) + throw new KeyNotFoundException($"Section {section.label} with id {section.id} does not exist"); } foreach (var updatedSection in updatedSectionsOrder) { - Section section = _sectionService.GetById(updatedSection.Id); - section.Order = updatedSection.Order; + Section section = _sectionService.GetById(updatedSection.id); + section.Order = updatedSection.order; _sectionService.Update(section.Id, section); } if (updatedSectionsOrder.Count > 0) { - MqttClientService.PublishMessage($"config/{updatedSectionsOrder[0].ConfigurationId}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); + MqttClientService.PublishMessage($"config/{updatedSectionsOrder[0].configurationId}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); } return new ObjectResult("Sections order has been successfully modified") { StatusCode = 200 }; diff --git a/ManagerService/Controllers/UserController.cs b/ManagerService/Controllers/UserController.cs index d5840ef..ea2ca10 100644 --- a/ManagerService/Controllers/UserController.cs +++ b/ManagerService/Controllers/UserController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Manager.Framework.Business; using Manager.Interfaces.DTO; using Manager.Interfaces.Models; using Manager.Services; @@ -21,12 +22,14 @@ namespace ManagerService.Controllers private UserDatabaseService _userService; private TokensService _tokenService; private readonly ILogger _logger; + private readonly ProfileLogic _profileLogic; - public UserController(ILogger logger, UserDatabaseService userService, TokensService tokenService) + public UserController(ILogger logger, UserDatabaseService userService, TokensService tokenService, ProfileLogic profileLogic) { _logger = logger; _userService = userService; _tokenService = tokenService; + _profileLogic = profileLogic; } /// @@ -104,6 +107,8 @@ namespace ManagerService.Controllers if (users.Select(u => u.Email).Contains(newUser.Email)) throw new InvalidOperationException("This Email is already used"); + newUser.Password = _profileLogic.HashPassword(newUser.Password); + User userCreated = _userService.Create(newUser); return new OkObjectResult(userCreated.ToDTO()); diff --git a/ManagerService/Extensions/MqttClientService.cs b/ManagerService/Extensions/MqttClientService.cs index 922ad23..a13ba88 100644 --- a/ManagerService/Extensions/MqttClientService.cs +++ b/ManagerService/Extensions/MqttClientService.cs @@ -64,7 +64,8 @@ namespace Mqtt.Client.AspNetCore.Services { var deserialized = JsonConvert.DeserializeObject>(payload); - switch (topic.Split("/")[0]) { + switch (topic.Split("/")[0]) + { case "player": var test = topic.Split("/")[1]; // Get device id