namespace TomatenMusic_Api.Auth.Services; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using TomatenMusic_Api.Auth.Entities; using TomatenMusic_Api.Auth.Helpers; using TomatenMusic_Api.Auth.Models; public interface IUserService { AuthenticateResponse Authenticate(AuthenticateRequest model); IEnumerable GetAll(); User GetById(int id); } public class UserService : IUserService { // users hardcoded for simplicity, store in a db with hashed passwords in production applications private List _users = new List { new User { Id = 1, FirstName = "Jannick", LastName = "Voss", Username = "Glowman", Password = "RX5GXstLLBvdt#_N" }, new User { Id = 2, FirstName = "Tim", LastName= "Müller", Password= "SGWaldsolms9", Username = "Tueem"} }; private readonly AppSettings _appSettings; public UserService(IOptions appSettings) { _appSettings = appSettings.Value; } public AuthenticateResponse Authenticate(AuthenticateRequest model) { var user = _users.SingleOrDefault(x => x.Username == model.Username && x.Password == model.Password); // return null if user not found if (user == null) return null; // authentication successful so generate jwt token var token = generateJwtToken(user); return new AuthenticateResponse(user, token); } public IEnumerable GetAll() { return _users; } public User GetById(int id) { return _users.FirstOrDefault(x => x.Id == id); } // helper methods private string generateJwtToken(User user) { // generate token that is valid for 7 days var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim("id", user.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(1), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } }