diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-12-11 20:45:15 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-12-11 20:45:15 +0200 |
| commit | 09aeb13a95ab573b05813ba563c322e854540c3e (patch) | |
| tree | 774a1ba82f4003a5691e554c77e3cc3470d8cb7c /API/Service/UserService.cs | |
| parent | 29b2a82d7ef2613b3e56eba7ed959243a98ae92d (diff) | |
| download | DevHive-09aeb13a95ab573b05813ba563c322e854540c3e.tar DevHive-09aeb13a95ab573b05813ba563c322e854540c3e.tar.gz DevHive-09aeb13a95ab573b05813ba563c322e854540c3e.zip | |
Implemented very basic and rough autorization for user
Diffstat (limited to 'API/Service/UserService.cs')
| -rw-r--r-- | API/Service/UserService.cs | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index 3c3b390..5d59f61 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -5,6 +5,12 @@ using Data.Models.Classes; using Data.Models.DTOs; using Microsoft.AspNetCore.Mvc; +using System.IdentityModel.Tokens.Jwt; +using Microsoft.IdentityModel.Tokens; +using System.Security.Claims; +using System; +using System.Text; + namespace API.Service { public class UserService @@ -12,12 +18,44 @@ namespace API.Service private readonly UserDbRepository _userDbRepository; private readonly IMapper _userMapper; + private static Random rnd = new Random(); // FOR TESTING PURPOSES ONLY + public UserService(DevHiveContext context, IMapper mapper) { this._userDbRepository = new UserDbRepository(context); this._userMapper = mapper; } - + + public async Task<IActionResult> LoginUser(UserDTO userDTO) + { + if (userDTO == null) + return new NotFoundObjectResult("User does not exist!"); + + User user = this._userMapper.Map<User>(userDTO); + + + + + // Key generation + var key = Encoding.ASCII.GetBytes(")H@McQfTB?E(H+Mb8x/A?D(Gr4u7x!A%WnZr4t7weThWmZq4KbPeShVm*G-KaPdSz%C*F-Ja6w9z$C&F"); //Startup.Configuration.GetSection("AppSettings").GetValue("Secret", "bruh")); + + var tokenHandler = new JwtSecurityTokenHandler(); + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Name, user.Id.ToString()) + }), + Expires = DateTime.UtcNow.AddDays(7), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) + }; + var token = tokenHandler.CreateToken(tokenDescriptor); + var tokenString = tokenHandler.WriteToken(token); + + return new OkObjectResult(tokenString); + } + + public async Task<IActionResult> CreateUser(UserDTO userDTO) { if (this._userDbRepository.DoesUsernameExist(userDTO.UserName)) |
