You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.3 KiB
116 lines
4.3 KiB
using DealerSelection.Api.CommonUtil;
|
|
using DealerSelection.Api.Infrastructure.Jwt;
|
|
using DealerSelection.Api.Interface;
|
|
using DealerSelection.Api.Models;
|
|
using DealerSelection.Common.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
|
|
public class JwtTokenApi : IJwtTokenApi
|
|
{
|
|
private IJwtRepository Repository { get; }
|
|
private readonly ILogger _logger;
|
|
public static string _key = ConfigurationHelper.GetSetting<string>("Jwt:Key", true);
|
|
public static string _expireMinutes = ConfigurationHelper.GetSetting<string>("Jwt:ExpireMinutes", true);
|
|
public static string _issuer = ConfigurationHelper.GetSetting<string>("Jwt:Issuer", true);
|
|
public static string _audience = ConfigurationHelper.GetSetting<string>("Jwt:Audience", true);
|
|
public static string _clientId = ConfigurationHelper.GetSetting<string>("Jwt:ClientId", true);
|
|
public static string _clientSecret = ConfigurationHelper.GetSetting<string>("Jwt:ClientSecret", true);
|
|
public JwtTokenApi(IJwtRepository repository, ILogger<JwtTokenApi> logger)
|
|
{
|
|
Repository = repository;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<string> GenerateToken(AuthValidateModel user)
|
|
{
|
|
_logger.LogInformation($"DS Api:-GenerateToken Started:- ");
|
|
try
|
|
{
|
|
bool isValidUser = await Authenticate(user);
|
|
if (isValidUser)
|
|
{
|
|
AuthModel auth = new AuthModel
|
|
{
|
|
ClientId = user.client_id,
|
|
SecretId = user.client_secret,
|
|
Role = "Admin"
|
|
};
|
|
|
|
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_key));
|
|
string expireMinutes = _expireMinutes;
|
|
int tokenExpireMinutes = string.IsNullOrEmpty(expireMinutes) ? 1439 : int.Parse(expireMinutes);
|
|
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
|
|
var claims = new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier,auth.ClientId),
|
|
new Claim(ClaimTypes.Role,auth.Role),
|
|
};
|
|
var token = new JwtSecurityToken(
|
|
_issuer,
|
|
_audience,
|
|
claims,
|
|
expires: DateTime.UtcNow.AddMinutes(tokenExpireMinutes),
|
|
signingCredentials: credentials);
|
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"DS Api:-JwtTokenApi Api GenerateToken:- " + ex.Message.ToString());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<bool> Authenticate(AuthValidateModel userLogin)
|
|
{
|
|
try
|
|
{
|
|
|
|
return _clientId.ToLower() == userLogin.client_id.ToLower() &&
|
|
_clientSecret.ToLower() == userLogin.client_secret.ToLower();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"DS Api:-JwtTokenApi Api Authenticate:- " + ex.Message.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<TokenValidProperty> IsTokenExpired(string tokenValue)
|
|
{
|
|
TokenValidProperty tokenValid = new TokenValidProperty();
|
|
try
|
|
{
|
|
var tokenTicks = GetTokenExpirationTime(tokenValue);
|
|
var tokenDate = DateTimeOffset.FromUnixTimeSeconds(tokenTicks).UtcDateTime;
|
|
|
|
var now = DateTime.UtcNow;
|
|
var valid = tokenDate >= now;
|
|
|
|
tokenValid.isValid = valid;
|
|
tokenValid.Ttl = tokenDate.TimeOfDay.TotalSeconds - now.TimeOfDay.TotalSeconds;
|
|
|
|
return tokenValid;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"DS Api:-JwtTokenApi Api IsTokenExpired:- " + ex.Message.ToString());
|
|
return tokenValid;
|
|
}
|
|
}
|
|
|
|
private static long GetTokenExpirationTime(string token)
|
|
{
|
|
var handler = new JwtSecurityTokenHandler();
|
|
var jwtSecurityToken = handler.ReadJwtToken(token);
|
|
var tokenExp = jwtSecurityToken.Claims.First(claim => claim.Type.Equals("exp")).Value;
|
|
var ticks = long.Parse(tokenExp);
|
|
return ticks;
|
|
}
|
|
|
|
}
|