From ea25a28a78038477f0f49d9a1cddbe4076dd0902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Andr=C3=A9?= Date: Thu, 12 Mar 2020 17:11:36 +0100 Subject: [PATCH] Update "User" API --- Back/skydiveLogs-api.Business/UserService.cs | 40 +++++++++++++++--- Back/skydiveLogs-api.Ioc/IocService.cs | 2 + .../Controllers/UserController.cs | 6 +-- Back/skydiveLogs-api/Data/JumpsDb.db | Bin 155648 -> 180224 bytes 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Back/skydiveLogs-api.Business/UserService.cs b/Back/skydiveLogs-api.Business/UserService.cs index 5336c4f..d3d39ca 100644 --- a/Back/skydiveLogs-api.Business/UserService.cs +++ b/Back/skydiveLogs-api.Business/UserService.cs @@ -1,10 +1,10 @@ -using System; -using System.Collections.Generic; - -using skydiveLogs_api.Business.Interface; +using skydiveLogs_api.Business.Interface; using skydiveLogs_api.Model; using skydiveLogs_api.Data.Interface; - +using System.Security.Cryptography; +using System.Text; +using System.IO; +using System; namespace skydiveLogs_api.Business { @@ -17,16 +17,44 @@ namespace skydiveLogs_api.Business public User GetByLogin(string login, string password) { - var tmp = _userRepository.GetByLogin(login, password); + var tmp = _userRepository.GetByLogin(login, EncryptPassword(password)); return tmp; } public void AddNewUser(User newUser) { + newUser.Password = EncryptPassword(newUser.Password); _userRepository.Add(newUser); } + private string EncryptPassword(string password) + { + var encryptionKey = "skydivelogsangular"; //we can change the code converstion key as per our requirement + byte[] clearBytes = Encoding.Unicode.GetBytes(password); + var encryptedPassword = string.Empty; + + using (Aes encryptor = Aes.Create()) + { + Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, + new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); + encryptor.Key = pdb.GetBytes(32); + encryptor.IV = pdb.GetBytes(16); + using (MemoryStream ms = new MemoryStream()) + { + using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) + { + cs.Write(clearBytes, 0, clearBytes.Length); + cs.Close(); + } + + encryptedPassword = Convert.ToBase64String(ms.ToArray()); + } + } + + return encryptedPassword; + } + private readonly IUserRepository _userRepository; } } diff --git a/Back/skydiveLogs-api.Ioc/IocService.cs b/Back/skydiveLogs-api.Ioc/IocService.cs index df5860b..a9bc297 100644 --- a/Back/skydiveLogs-api.Ioc/IocService.cs +++ b/Back/skydiveLogs-api.Ioc/IocService.cs @@ -25,12 +25,14 @@ namespace skydiveLogs_api.Ioc _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); + _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); + _services.AddScoped(); string connectionString = _configuration.GetConnectionString("DefaultConnection"); _services.AddSingleton(c => new Data.LiteDbProvider(connectionString)); diff --git a/Back/skydiveLogs-api/Controllers/UserController.cs b/Back/skydiveLogs-api/Controllers/UserController.cs index 96b9134..20dfb22 100644 --- a/Back/skydiveLogs-api/Controllers/UserController.cs +++ b/Back/skydiveLogs-api/Controllers/UserController.cs @@ -22,11 +22,11 @@ namespace skydiveLogs_api.Controllers } // POST: api/User - [HttpPost] + [HttpPost("Authenticate")] [EnableCors] - public UserResp Authenticate([FromBody] string login, [FromBody] string password) + public UserResp Authenticate([FromBody] UserReq value) { - var result = _userService.GetByLogin(login, password); + var result = _userService.GetByLogin(value.Login, value.Password); return _mapper.Map(result); } diff --git a/Back/skydiveLogs-api/Data/JumpsDb.db b/Back/skydiveLogs-api/Data/JumpsDb.db index ee60975cce9e7e34a9d75981c76b228d45b15a4f..1bddf28555e64b9f383d967fa4d3db5fb90766a6 100644 GIT binary patch delta 363 zcmZoTz}e8iJ;8xdbfRNDqwmCnj_d-V#i>OM!kZZxzsof#v?(wyQ($sX6lP#xVET^? z7(hZ`puq5-L3FaBf-jEhHH<|- z@ktLc}PkE x5{rw=^NUg#B!Rj@a=k)x9Mc57%e*R`ozf!HE%c)d9n)=XVIh#qQpBLZ005>Xe60Wg delta 35 rcmZo@;BGj;Il+NZXrg01qw~arj+;do-^fjD5Nc9rQ(#=Cz~le`-tY^i