using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Text; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using WebApi.Entities; using WebApi.Helpers; namespace WebApi.Services { public interface IUserService { User Authenticate(string username, string password); IEnumerable GetAll(); } 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 = "Test", LastName = "User", Username = "test", Password = "test" } }; private readonly AppSettings _appSettings; public UserService(IOptions appSettings) { _appSettings = appSettings.Value; } public User Authenticate(string username, string password) { var user = _users.SingleOrDefault(x => x.Username == username && x.Password == password); // return null if user not found if (user == null) return null; // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); 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); user.Token = tokenHandler.WriteToken(token); // remove password before returning user.Password = null; return user; } public IEnumerable GetAll() { // return users without passwords return _users.Select(x => { x.Password = null; return x; }); } } }