From dee2e37a4a8759108390c664e06bf147b8385cbf Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 14 Dec 2020 23:29:14 +0200 Subject: Stabalized project for compilation. Next step after init architecture --- src/DevHive.Services/DevHive.Services.csproj | 9 +- .../Models/Identity/LoginServiceModel.cs | 8 ++ .../Models/Identity/RegisterServiceModel.cs | 11 +++ .../Models/Identity/UpdateUserServiceModel.cs | 9 ++ .../Models/Identity/UserServiceModel.cs | 11 +++ src/DevHive.Services/Options/JWTOptions.cs | 14 +++ src/DevHive.Services/Services/RoleService.cs | 26 ++++++ src/DevHive.Services/Services/UserService.cs | 103 +++++++++++++++++++++ 8 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/DevHive.Services/Models/Identity/LoginServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/RegisterServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/UserServiceModel.cs create mode 100644 src/DevHive.Services/Options/JWTOptions.cs create mode 100644 src/DevHive.Services/Services/RoleService.cs create mode 100644 src/DevHive.Services/Services/UserService.cs (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Services/DevHive.Services.csproj b/src/DevHive.Services/DevHive.Services.csproj index 563e6f9..277a342 100644 --- a/src/DevHive.Services/DevHive.Services.csproj +++ b/src/DevHive.Services/DevHive.Services.csproj @@ -1,7 +1,14 @@ - net5.0 + + + + + + + + diff --git a/src/DevHive.Services/Models/Identity/LoginServiceModel.cs b/src/DevHive.Services/Models/Identity/LoginServiceModel.cs new file mode 100644 index 0000000..d056f9a --- /dev/null +++ b/src/DevHive.Services/Models/Identity/LoginServiceModel.cs @@ -0,0 +1,8 @@ +namespace DevHive.Services.Models.Identity +{ + public class LoginServiceModel + { + public string UserName { get; set; } + public string Password { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/RegisterServiceModel.cs new file mode 100644 index 0000000..45f42d2 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/RegisterServiceModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Services.Models.Identity +{ + public class RegisterServiceModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Password { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs new file mode 100644 index 0000000..94600ae --- /dev/null +++ b/src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Identity +{ + public class UpdateUserServiceModel : UserServiceModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Identity/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/UserServiceModel.cs new file mode 100644 index 0000000..387afdd --- /dev/null +++ b/src/DevHive.Services/Models/Identity/UserServiceModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Services.Models.Identity +{ + public class UserServiceModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Role { get; set;} + } +} diff --git a/src/DevHive.Services/Options/JWTOptions.cs b/src/DevHive.Services/Options/JWTOptions.cs new file mode 100644 index 0000000..95458f5 --- /dev/null +++ b/src/DevHive.Services/Options/JWTOptions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Options; + +namespace DevHive.Services.Options +{ + public class JWTOptions + { + public JWTOptions(string secret) + { + this.Secret = secret; + } + + public string Secret { get; init; } + } +} diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs new file mode 100644 index 0000000..202c611 --- /dev/null +++ b/src/DevHive.Services/Services/RoleService.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Repositories; + +namespace DevHive.Services.Services +{ + public class RoleService + { + /* private readonly DevHiveContext _context; + + public RoleService(DevHiveContext context) + { + this._context = context; + } + + public Task CreatePost(string name) + { + throw new NotImplementedException(); + } + + public Task GetPostById(uint postId) + { + throw new NotImplementedException(); + }*/ + } +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs new file mode 100644 index 0000000..f06198c --- /dev/null +++ b/src/DevHive.Services/Services/UserService.cs @@ -0,0 +1,103 @@ +namespace DevHive.Services.Services +{ + public class UserService + { + /* private readonly UserRepository _userDbRepository; + private readonly IMapper _userMapper; + private readonly JWTOptions _jwtOptions; + + public UserService(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) + { + this._userDbRepository = new UserRepository(context); + this._userMapper = mapper; + this._jwtOptions = jwtOptions; + } + + public async Task LoginUser(LoginDTO loginDTO) + { + User user = this._userDbRepository.FindByUsername(loginDTO.UserName); + + if (user == null) + return new NotFoundObjectResult("User does not exist!"); + + byte[] key = Encoding.ASCII.GetBytes(_jwtOptions.Secret); + + if (user.PasswordHash != GeneratePasswordHash(loginDTO.Password)) + return new BadRequestObjectResult("Incorrect password!"); + + // Create Jwt Token configuration + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Role, user.Role) // Authorize user by role + }), + Expires = DateTime.UtcNow.AddDays(7), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) + }; + + var tokenHandler = new JwtSecurityTokenHandler(); + var token = tokenHandler.CreateToken(tokenDescriptor); + var tokenString = tokenHandler.WriteToken(token); + + return new OkObjectResult(new { Token = tokenString }); + } + + public async Task RegisterUser(RegisterDTO registerDTO) + { + + if (this._userDbRepository.DoesUsernameExist(registerDTO.UserName)) + return new BadRequestObjectResult("Username already exists!"); + + User user = this._userMapper.Map(registerDTO); + + user.Role = UserRoles.User; + user.PasswordHash = GeneratePasswordHash(registerDTO.Password); + + await this._userDbRepository.AddAsync(user); + + return new CreatedResult("CreateUser", user); + } + + private string GeneratePasswordHash(string password) + { + //TODO: Implement + return password; + } + + public async Task GetUserById(Guid id) + { + User user = await this._userDbRepository.FindByIdAsync(id); + + if (user == null) + return new NotFoundObjectResult("User does not exist!"); + + return new OkObjectResult(user); + } + + public async Task UpdateUser(Guid id, UserDTO userDTO) + { + if (!this._userDbRepository.DoesUserExist(id)) + return new NotFoundObjectResult("User does not exist!"); + + if (!this._userDbRepository.HasThisUsername(id, userDTO.UserName) + && this._userDbRepository.DoesUsernameExist(userDTO.UserName)) + return new BadRequestObjectResult("Username already exists!"); + + User user = this._userMapper.Map(userDTO); + await this._userDbRepository.EditAsync(id, user); + + return new AcceptedResult("UpdateUser", user); + } + + public async Task DeleteUser(Guid id) + { + if (!this._userDbRepository.DoesUserExist(id)) + return new NotFoundObjectResult("User does not exist!"); + + await this._userDbRepository.DeleteAsync(id); + + return new OkResult(); + }*/ + } +} -- cgit v1.2.3