using Biskilog_Accounting.Shared.ClientContractModels; using Biskilog_Accounting.Shared.Enums; using Biskilog_Accounting.Shared.Interfaces; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; namespace Biskilog_Accounting.ServiceRepo { public class TokenService : ITokenService { private IConfiguration m_configuration { get; } public TokenService(IConfiguration a_configuration) { m_configuration = a_configuration; } /// /// Validates a user access token /// /// AuthEnums.Valid if token is a valid and unexpired token public AuthEnums ValidateToken(string a_token) { try { string token = a_token.Substring(6).Trim(); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); if (jwtToken.ValidFrom <= DateTime.Now && jwtToken.ValidTo > DateTime.Now) return AuthEnums.Valid; return AuthEnums.Expired; } catch (Exception ex) { Console.WriteLine(ex.Message); return AuthEnums.Invalid; } } /// /// Generates an access token based on the user /// /// A tokenized string public string GenerateToken(Userauth a_user, Contract a_clientContract, Databasemap a_database) { try { //create claims details based on the user information var claims = new[] { new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()), new Claim("ContractStart",a_clientContract.StartDate !.Value.ToString()), new Claim("ContractEnd",a_clientContract.EndDate!.Value.ToString()), new Claim("UserId", a_user.UserId.ToString()), new Claim("Username", a_user.Username.ToString()), new Claim("DbId",a_database.DbNo.ToString()), new Claim("ClientId", a_user.ClientId.ToString()), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(m_configuration["Jwt:Key"]!)); var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken(m_configuration["Jwt:Issuer"], m_configuration["Jwt:Audience"], claims, expires: DateTime.UtcNow.AddDays(14), signingCredentials: signIn); return $"{new JwtSecurityTokenHandler().WriteToken(token)}"; } catch (Exception ex) { Console.WriteLine(ex.Message); return AuthEnums.Error.ToString(); } } /// ///Deserializes the token string if valid to return the specified user role id in the token string /// /// /// RoleId public int? GetDatabaseIdFromToken(string a_token) { if (ValidateToken(a_token) == AuthEnums.Valid) { string token = a_token.Substring(6).Trim(); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); return int.Parse(jwtToken.Claims.First(claim => claim.Type == "DbId").Value); } return null; } /// ///Deserializes the token string if valid to return the specified user id in the token string /// /// /// UserId public int? GetUserIdFromToken(string a_token) { if (ValidateToken(a_token) == AuthEnums.Valid) { string token = a_token.Substring(6).Trim(); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); return int.Parse(jwtToken.Claims.First(claim => claim.Type == "UserId").Value); } return null; } /// ///Deserializes the token string if valid to return the specified username in the token string /// /// /// Username public string? GetUserNameFromToken(string a_token) { if (ValidateToken(a_token) == AuthEnums.Valid) { string token = a_token.Substring(6).Trim(); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); return jwtToken.Claims.First(claim => claim.Type == "Username").Value; } return null; } } }