| | 1 | | using System.Security.Cryptography; |
| | 2 | | using LGDXRobotCloud.API.Exceptions; |
| | 3 | | using LGDXRobotCloud.Data.DbContexts; |
| | 4 | | using LGDXRobotCloud.Data.Entities; |
| | 5 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 6 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 7 | | using Microsoft.EntityFrameworkCore; |
| | 8 | | using Microsoft.Extensions.Caching.Memory; |
| | 9 | |
|
| | 10 | | namespace LGDXRobotCloud.API.Services.Administration; |
| | 11 | |
|
| | 12 | | public interface IApiKeyService |
| | 13 | | { |
| | 14 | | Task<(IEnumerable<ApiKeyBusinessModel>, PaginationHelper)> GetApiKeysAsync(string? name, bool isThirdParty, int pageNu |
| | 15 | | Task<ApiKeyBusinessModel> GetApiKeyAsync(int apiKeyId); |
| | 16 | | Task<ApiKeySecretBusinessModel> GetApiKeySecretAsync(int apiKeyId); |
| | 17 | | Task<ApiKeyBusinessModel> AddApiKeyAsync(ApiKeyCreateBusinessModel apiKeyCreateBusinessModel); |
| | 18 | | Task<bool> UpdateApiKeyAsync(int apiKeyId, ApiKeyUpdateBusinessModel apiKeyUpdateBusinessModel); |
| | 19 | | Task<bool> UpdateApiKeySecretAsync(int apiKeyId, ApiKeySecretUpdateBusinessModel apiKeySecretUpdateBusinessModel); |
| | 20 | | Task<bool> DeleteApiKeyAsync(int apiKeyId); |
| | 21 | |
|
| | 22 | | Task<IEnumerable<ApiKeySearchBusinessModel>> SearchApiKeysAsync(string? name); |
| | 23 | | Task<bool> ValidateApiKeyAsync(string apiKey); |
| | 24 | | } |
| | 25 | |
|
| 27 | 26 | | public class ApiKeyService( |
| 27 | 27 | | IMemoryCache memoryCache, |
| 27 | 28 | | LgdxContext context |
| 27 | 29 | | ) : IApiKeyService |
| | 30 | | { |
| 27 | 31 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| 27 | 32 | | private readonly IMemoryCache _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache)); |
| | 33 | |
|
| | 34 | | public async Task<(IEnumerable<ApiKeyBusinessModel>, PaginationHelper)>GetApiKeysAsync(string? name, bool isThirdParty |
| 6 | 35 | | { |
| 6 | 36 | | var query = _context.ApiKeys as IQueryable<ApiKey>; |
| 6 | 37 | | query = query.Where(a => a.IsThirdParty == isThirdParty); |
| 6 | 38 | | if(!string.IsNullOrWhiteSpace(name)) |
| 3 | 39 | | { |
| 3 | 40 | | name = name.Trim(); |
| 3 | 41 | | query = query.Where(a => a.Name.ToLower().Contains(name.ToLower())); |
| 3 | 42 | | } |
| 6 | 43 | | var itemCount = await query.CountAsync(); |
| 6 | 44 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 6 | 45 | | var apiKeys = await query.AsNoTracking() |
| 6 | 46 | | .OrderBy(a => a.Id) |
| 6 | 47 | | .Skip(pageSize * (pageNumber - 1)) |
| 6 | 48 | | .Take(pageSize) |
| 6 | 49 | | .Select(a => new ApiKeyBusinessModel { |
| 6 | 50 | | Id = a.Id, |
| 6 | 51 | | Name = a.Name, |
| 6 | 52 | | IsThirdParty = a.IsThirdParty, |
| 6 | 53 | | }) |
| 6 | 54 | | .ToListAsync(); |
| 6 | 55 | | return (apiKeys, PaginationHelper); |
| 6 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task<ApiKeyBusinessModel> GetApiKeyAsync(int apiKeyId) |
| 5 | 59 | | { |
| 5 | 60 | | return await _context.ApiKeys.AsNoTracking() |
| 5 | 61 | | .Where(a => a.Id == apiKeyId) |
| 5 | 62 | | .Select(a => new ApiKeyBusinessModel { |
| 5 | 63 | | Id = a.Id, |
| 5 | 64 | | Name = a.Name, |
| 5 | 65 | | IsThirdParty = a.IsThirdParty, |
| 5 | 66 | | }) |
| 5 | 67 | | .FirstOrDefaultAsync() |
| 5 | 68 | | ?? throw new LgdxNotFound404Exception(); |
| 4 | 69 | | } |
| | 70 | |
|
| | 71 | | public async Task<ApiKeySecretBusinessModel> GetApiKeySecretAsync(int apiKeyId) |
| 5 | 72 | | { |
| 5 | 73 | | return await _context.ApiKeys.AsNoTracking() |
| 5 | 74 | | .Where(a => a.Id == apiKeyId) |
| 5 | 75 | | .Select(a => new ApiKeySecretBusinessModel { |
| 5 | 76 | | Secret = a.Secret, |
| 5 | 77 | | }) |
| 5 | 78 | | .FirstOrDefaultAsync() |
| 5 | 79 | | ?? throw new LgdxNotFound404Exception(); |
| 4 | 80 | | } |
| | 81 | |
|
| | 82 | | private static string GenerateApiKeys() |
| 1 | 83 | | { |
| 1 | 84 | | var bytes = RandomNumberGenerator.GetBytes(32); |
| 1 | 85 | | string base64String = Convert.ToBase64String(bytes) |
| 1 | 86 | | .Replace("+", "-") |
| 1 | 87 | | .Replace("/", "_"); |
| 1 | 88 | | return "LGDX" + base64String; |
| 1 | 89 | | } |
| | 90 | |
|
| | 91 | | public async Task<ApiKeyBusinessModel> AddApiKeyAsync(ApiKeyCreateBusinessModel apiKeyCreateBusinessModel) |
| 2 | 92 | | { |
| 2 | 93 | | if (!apiKeyCreateBusinessModel.IsThirdParty) |
| 1 | 94 | | apiKeyCreateBusinessModel.Secret = GenerateApiKeys(); |
| 2 | 95 | | var apikey = apiKeyCreateBusinessModel.ToEntity(); |
| 2 | 96 | | await _context.ApiKeys.AddAsync(apikey); |
| 2 | 97 | | await _context.SaveChangesAsync(); |
| 2 | 98 | | return new ApiKeyBusinessModel{ |
| 2 | 99 | | Id = apikey.Id, |
| 2 | 100 | | Name = apikey.Name, |
| 2 | 101 | | IsThirdParty = apikey.IsThirdParty |
| 2 | 102 | | }; |
| 2 | 103 | | } |
| | 104 | |
|
| | 105 | | public async Task<bool> UpdateApiKeyAsync(int apiKeyId, ApiKeyUpdateBusinessModel apiKeyUpdateBusinessModel) |
| 0 | 106 | | { |
| 0 | 107 | | return await _context.ApiKeys |
| 0 | 108 | | .Where(a => a.Id == apiKeyId) |
| 0 | 109 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 110 | | .SetProperty(a => a.Name, apiKeyUpdateBusinessModel.Name)) == 1; |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public async Task<bool> UpdateApiKeySecretAsync(int apiKeyId, ApiKeySecretUpdateBusinessModel apiKeySecretUpdateBusine |
| 4 | 114 | | { |
| 4 | 115 | | var apiKey = await _context.ApiKeys.Where(a => a.Id == apiKeyId).FirstOrDefaultAsync() |
| 4 | 116 | | ?? throw new LgdxNotFound404Exception(); |
| 3 | 117 | | if (!apiKey.IsThirdParty && !string.IsNullOrEmpty(apiKeySecretUpdateBusinessModel.Secret)) |
| 1 | 118 | | { |
| 1 | 119 | | throw new LgdxValidation400Expection(nameof(apiKeySecretUpdateBusinessModel.Secret), "The LGDXRobot Cloud API Key |
| | 120 | | } |
| | 121 | |
|
| 2 | 122 | | apiKey.Secret = apiKeySecretUpdateBusinessModel.Secret; |
| 2 | 123 | | return await _context.SaveChangesAsync() == 1; |
| 2 | 124 | | } |
| | 125 | |
|
| | 126 | | public async Task<bool> DeleteApiKeyAsync(int apiKeyId) |
| 0 | 127 | | { |
| 0 | 128 | | return await _context.ApiKeys.Where(a => a.Id == apiKeyId) |
| 0 | 129 | | .ExecuteDeleteAsync() == 1; |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public async Task<IEnumerable<ApiKeySearchBusinessModel>> SearchApiKeysAsync(string? name) |
| 5 | 133 | | { |
| 5 | 134 | | var n = name ?? string.Empty; |
| 5 | 135 | | return await _context.ApiKeys.AsNoTracking() |
| 5 | 136 | | .Where(w => w.Name.ToLower().Contains(n.ToLower())) |
| 5 | 137 | | .Where(w => w.IsThirdParty == true) |
| 5 | 138 | | .Take(10) |
| 5 | 139 | | .Select(t => new ApiKeySearchBusinessModel { |
| 5 | 140 | | Id = t.Id, |
| 5 | 141 | | Name = t.Name, |
| 5 | 142 | | }) |
| 5 | 143 | | .ToListAsync(); |
| 5 | 144 | | } |
| | 145 | |
|
| | 146 | | public async Task<bool> ValidateApiKeyAsync(string apiKey) |
| 0 | 147 | | { |
| 0 | 148 | | string hashed = LgdxHelper.GenerateSha256Hash(apiKey); |
| 0 | 149 | | _memoryCache.TryGetValue($"ValidateApiKeyAsync_{hashed}", out bool? exist); |
| 0 | 150 | | if (exist != null) |
| 0 | 151 | | { |
| 0 | 152 | | return (bool)exist; |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | var result = await _context.ApiKeys.AsNoTracking() |
| 0 | 156 | | .Where(a => a.Secret == apiKey) |
| 0 | 157 | | .Where(a => !a.IsThirdParty) |
| 0 | 158 | | .AnyAsync(); |
| | 159 | |
|
| 0 | 160 | | if (result) |
| 0 | 161 | | { |
| 0 | 162 | | _memoryCache.Set($"ValidateApiKeyAsync_{hashed}", true, TimeSpan.FromMinutes(5)); |
| 0 | 163 | | } |
| | 164 | |
|
| 0 | 165 | | return result; |
| 0 | 166 | | } |
| | 167 | | } |