| | 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 | |
|
| | 9 | | namespace LGDXRobotCloud.API.Services.Administration; |
| | 10 | |
|
| | 11 | | public interface IApiKeyService |
| | 12 | | { |
| | 13 | | Task<(IEnumerable<ApiKeyBusinessModel>, PaginationHelper)> GetApiKeysAsync(string? name, bool isThirdParty, int pageNu |
| | 14 | | Task<ApiKeyBusinessModel> GetApiKeyAsync(int apiKeyId); |
| | 15 | | Task<ApiKeySecretBusinessModel> GetApiKeySecretAsync(int apiKeyId); |
| | 16 | | Task<ApiKeyBusinessModel> AddApiKeyAsync(ApiKeyCreateBusinessModel apiKeyCreateBusinessModel); |
| | 17 | | Task<bool> UpdateApiKeyAsync(int apiKeyId, ApiKeyUpdateBusinessModel apiKeyUpdateBusinessModel); |
| | 18 | | Task<bool> UpdateApiKeySecretAsync(int apiKeyId, ApiKeySecretUpdateBusinessModel apiKeySecretUpdateBusinessModel); |
| | 19 | | Task<bool> DeleteApiKeyAsync(int apiKeyId); |
| | 20 | |
|
| | 21 | | Task<IEnumerable<ApiKeySearchBusinessModel>> SearchApiKeysAsync(string? name); |
| | 22 | | } |
| | 23 | |
|
| 1 | 24 | | public class ApiKeyService(LgdxContext context) : IApiKeyService |
| | 25 | | { |
| 1 | 26 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 27 | |
|
| | 28 | | public async Task<(IEnumerable<ApiKeyBusinessModel>, PaginationHelper)>GetApiKeysAsync(string? name, bool isThirdParty |
| 1 | 29 | | { |
| 1 | 30 | | var query = _context.ApiKeys as IQueryable<ApiKey>; |
| 1 | 31 | | query = query.Where(a => a.IsThirdParty == isThirdParty); |
| 1 | 32 | | if(!string.IsNullOrWhiteSpace(name)) |
| 0 | 33 | | { |
| 0 | 34 | | name = name.Trim(); |
| 0 | 35 | | query = query.Where(a => a.Name.Contains(name)); |
| 0 | 36 | | } |
| 1 | 37 | | var itemCount = await query.CountAsync(); |
| 1 | 38 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 1 | 39 | | var apiKeys = await query.AsNoTracking() |
| 1 | 40 | | .OrderBy(a => a.Id) |
| 1 | 41 | | .Skip(pageSize * (pageNumber - 1)) |
| 1 | 42 | | .Take(pageSize) |
| 1 | 43 | | .Select(a => new ApiKeyBusinessModel { |
| 1 | 44 | | Id = a.Id, |
| 1 | 45 | | Name = a.Name, |
| 1 | 46 | | IsThirdParty = a.IsThirdParty, |
| 1 | 47 | | }) |
| 1 | 48 | | .ToListAsync(); |
| 1 | 49 | | return (apiKeys, PaginationHelper); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | public async Task<ApiKeyBusinessModel> GetApiKeyAsync(int apiKeyId) |
| 0 | 53 | | { |
| 0 | 54 | | return await _context.ApiKeys.AsNoTracking() |
| 0 | 55 | | .Where(a => a.Id == apiKeyId) |
| 0 | 56 | | .Select(a => new ApiKeyBusinessModel { |
| 0 | 57 | | Id = a.Id, |
| 0 | 58 | | Name = a.Name, |
| 0 | 59 | | IsThirdParty = a.IsThirdParty, |
| 0 | 60 | | }) |
| 0 | 61 | | .FirstOrDefaultAsync() |
| 0 | 62 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async Task<ApiKeySecretBusinessModel> GetApiKeySecretAsync(int apiKeyId) |
| 0 | 66 | | { |
| 0 | 67 | | return await _context.ApiKeys.AsNoTracking() |
| 0 | 68 | | .Where(a => a.Id == apiKeyId) |
| 0 | 69 | | .Select(a => new ApiKeySecretBusinessModel { |
| 0 | 70 | | Secret = a.Secret, |
| 0 | 71 | | }) |
| 0 | 72 | | .FirstOrDefaultAsync() |
| 0 | 73 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private static string GenerateApiKeys() |
| 0 | 77 | | { |
| 0 | 78 | | var bytes = RandomNumberGenerator.GetBytes(32); |
| 0 | 79 | | string base64String = Convert.ToBase64String(bytes) |
| 0 | 80 | | .Replace("+", "-") |
| 0 | 81 | | .Replace("/", "_"); |
| 0 | 82 | | return "LGDX" + base64String; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public async Task<ApiKeyBusinessModel> AddApiKeyAsync(ApiKeyCreateBusinessModel apiKeyCreateBusinessModel) |
| 0 | 86 | | { |
| 0 | 87 | | apiKeyCreateBusinessModel.Secret ??= GenerateApiKeys(); |
| 0 | 88 | | var apikey = apiKeyCreateBusinessModel.ToEntity(); |
| 0 | 89 | | await _context.ApiKeys.AddAsync(apikey); |
| 0 | 90 | | await _context.SaveChangesAsync(); |
| 0 | 91 | | return new ApiKeyBusinessModel{ |
| 0 | 92 | | Id = apikey.Id, |
| 0 | 93 | | Name = apikey.Name, |
| 0 | 94 | | IsThirdParty = apikey.IsThirdParty |
| 0 | 95 | | }; |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public async Task<bool> UpdateApiKeyAsync(int apiKeyId, ApiKeyUpdateBusinessModel apiKeyUpdateBusinessModel) |
| 0 | 99 | | { |
| 0 | 100 | | return await _context.ApiKeys |
| 0 | 101 | | .Where(a => a.Id == apiKeyId) |
| 0 | 102 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 103 | | .SetProperty(a => a.Name, apiKeyUpdateBusinessModel.Name)) == 1; |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public async Task<bool> UpdateApiKeySecretAsync(int apiKeyId, ApiKeySecretUpdateBusinessModel apiKeySecretUpdateBusine |
| 0 | 107 | | { |
| 0 | 108 | | var apiKey = await _context.ApiKeys.Where(a => a.Id == apiKeyId).FirstOrDefaultAsync() |
| 0 | 109 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 110 | | if (!apiKey.IsThirdParty && !string.IsNullOrEmpty(apiKeySecretUpdateBusinessModel.Secret)) |
| 0 | 111 | | { |
| 0 | 112 | | throw new LgdxValidation400Expection(nameof(apiKeySecretUpdateBusinessModel.Secret), "The LGDXRobot Cloud API Key |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | apiKey.Secret = apiKeySecretUpdateBusinessModel.Secret; |
| 0 | 116 | | return await _context.SaveChangesAsync() == 1; |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public async Task<bool> DeleteApiKeyAsync(int apiKeyId) |
| 0 | 120 | | { |
| 0 | 121 | | return await _context.ApiKeys.Where(a => a.Id == apiKeyId) |
| 0 | 122 | | .ExecuteDeleteAsync() == 1; |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | public async Task<IEnumerable<ApiKeySearchBusinessModel>> SearchApiKeysAsync(string? name) |
| 0 | 126 | | { |
| 0 | 127 | | if (string.IsNullOrWhiteSpace(name)) |
| 0 | 128 | | { |
| 0 | 129 | | return await _context.ApiKeys.AsNoTracking() |
| 0 | 130 | | .Take(10) |
| 0 | 131 | | .Select(a => new ApiKeySearchBusinessModel{ |
| 0 | 132 | | Id = a.Id, |
| 0 | 133 | | Name = a.Name |
| 0 | 134 | | }) |
| 0 | 135 | | .ToListAsync(); |
| | 136 | | } |
| | 137 | | else |
| 0 | 138 | | { |
| 0 | 139 | | return await _context.ApiKeys.AsNoTracking() |
| 0 | 140 | | .Where(w => w.Name.Contains(name)) |
| 0 | 141 | | .Take(10) |
| 0 | 142 | | .Select(a => new ApiKeySearchBusinessModel{ |
| 0 | 143 | | Id = a.Id, |
| 0 | 144 | | Name = a.Name |
| 0 | 145 | | }) |
| 0 | 146 | | .ToListAsync(); |
| | 147 | | } |
| 0 | 148 | | } |
| | 149 | | } |