using Cloud_Manager.Models.ClientContractModels; using Cloud_Manager.Models.Enums; using Cloud_Manager.Models.Interfaces; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; using System; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; namespace Cloud_Manager.Models.ServiceRepo { public class TokenService : IKeyService { private IConfiguration m_configuration { get; } private readonly Random m_random; private BiskilogContext m_context; public TokenService(IConfiguration a_configuration, BiskilogContext a_context) { m_configuration = a_configuration; m_context = a_context; m_random = new Random(); } public AuthEnums ValidateKey(string a_Key) { if (!string.IsNullOrEmpty(a_Key)) { Clientapikey? keyInfo = m_context.Clientapikeys.FirstOrDefault(k => k.Key == a_Key); if (keyInfo != null) { if (keyInfo.IsActive == 0) { //Key is not active return AuthEnums.Inactive; } if (TryDecodeKey(a_Key, out int businessId)) { Contract? contract = m_context.Contracts.FirstOrDefault(c => c.ContractId == keyInfo.ContractId && c.BusinessId == businessId && c.StartDate <= DateTime.Now && c.EndDate > DateTime.Now); if (contract == null) { contract = m_context.Contracts.FirstOrDefault(c => c.ContractId == keyInfo.ContractId && c.BusinessId == businessId); //If contract start date is not past the key should inactive if (contract?.StartDate > DateTime.Now) { return AuthEnums.Inactive; } //Anyother reason contract is expired return AuthEnums.Expired; } //Key is valid and contract not expired return AuthEnums.Valid; } } else { return AuthEnums.NotFound; } } return AuthEnums.Invalid; } public async Task GenerateKey(Contract a_clientContract) { const string prefix = "AI"; const char delimiter = '@'; const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; StringBuilder key = new StringBuilder(32); key.Append(prefix); key.Append(a_clientContract.BusinessId); key.Append(delimiter); for (int i = key.Length; i < 32; i++) { key.Append(chars[m_random.Next(chars.Length)]); } Clientapikey clientapikey = new Clientapikey(); clientapikey.Key = key.ToString(); clientapikey.ContractId = a_clientContract.ContractId; m_context.Clientapikeys.Add(clientapikey); if (await m_context.SaveChangesAsync() > 0) { return true; } else { return false; } } public int? GetDatabaseIdFromKey(string a_Key) { if (ValidateKey(a_Key) == AuthEnums.Valid) { if (TryDecodeKey(a_Key, out int businessId)) { Clientapikey? keyInfo = m_context.Clientapikeys.FirstOrDefault(k => k.Key == a_Key); Contract? contract = m_context.Contracts.FirstOrDefault(c => c.ContractId == keyInfo.ContractId && c.BusinessId == businessId && c.StartDate <= DateTime.Now && c.EndDate > DateTime.Now); Databasemap? databaseMap = m_context.Databasemaps.FirstOrDefault(c => c.ClientId == contract.ClientId); return databaseMap?.DbNo; } } return null; } public string GetBaseBranch(string a_Key) { if (ValidateKey(a_Key) == AuthEnums.Valid) { if (TryDecodeKey(a_Key, out int businessId)) { Clientapikey? keyInfo = m_context.Clientapikeys.FirstOrDefault(k => k.Key == a_Key); Contract? contract = m_context.Contracts.FirstOrDefault(c => c.ContractId == keyInfo.ContractId && c.BusinessId == businessId && c.StartDate <= DateTime.Now && c.EndDate > DateTime.Now); if (contract != null) { Clientbusiness? clientbusiness = m_context.Clientbusinesses.FirstOrDefault(cb => cb.ClientId == contract.ClientId && cb.BusinessId == businessId); if (clientbusiness != null) { return clientbusiness.BusinessExternalId; } } } } return String.Empty; } public static bool TryDecodeKey(string a_key, out int o_businessId) { char delimiter = '@'; o_businessId = 0; // Check if the key has the expected length and starts with the expected prefix if (a_key.Length == 32 && a_key.StartsWith("AI")) { // Find the index of the delimiter int delimiterIndex = a_key.IndexOf(delimiter, 2); // Check if the delimiter is found and there are characters after it if (delimiterIndex != -1 && delimiterIndex < a_key.Length - 1) { // Attempt to parse the embedded integer value if (int.TryParse(a_key.Substring(2, delimiterIndex - 2), out o_businessId)) { return true; // Successfully decoded } } } return false; // Failed to decode } } }