public async Task <GenerateKeyResponse> GenerateKey(GenerateKeyRequest req, IMemoryCache cache, MySQLDB db) { _cache = cache; Db = db; string APIKey = string.Empty; GenerateKeyResponse response = new GenerateKeyResponse(); using (var cryptoProvider = new RNGCryptoServiceProvider()) { byte[] secretKeyByteArray = new byte[32]; //256 bit cryptoProvider.GetBytes(secretKeyByteArray); APIKey = Convert.ToBase64String(secretKeyByteArray); } string AppID = string.Empty; if (!req.forSession) { AppID = GetMachineGuid(); } else { AppID = Guid.NewGuid().ToString(); } response.AppID = AppID; response.ApiKey = APIKey; if (req.cache) { MemoryCacheEntryOptions cacheExpirationOptions = new MemoryCacheEntryOptions(); cacheExpirationOptions.AbsoluteExpiration = DateTime.Now.AddMinutes(30); cacheExpirationOptions.Priority = CacheItemPriority.High; _cache.Set("AppID", AppID, cacheExpirationOptions); _cache.Set("ApiKey", APIKey, cacheExpirationOptions); } await Db.Connection.OpenAsync(); ApiKeys model = new ApiKeys() { AppID = AppID, ApiKey = APIKey }; model.Db = Db; var query = new ApiKeysQuery(Db); var queryResult = await query.FindOneByIDAsync(AppID); if (queryResult is null) { await model.InsertAsync(); } else { await model.UpdateAsync(); } await Db.Connection.CloseAsync(); return(response); }
public async Task <ApiKey[]> GetApiKeys(ApiKeysQuery query) { if (String.IsNullOrEmpty(query.AppId)) { throw new ArgumentException("NoDbStorage always requires the AppId property"); } var apiKeys = await _apiKeyQueries.GetAllAsync(query.AppId); if (!string.IsNullOrEmpty(query.Name)) { apiKeys = apiKeys.Where(a => a.Name == query.Name); } if (!string.IsNullOrEmpty(query.Key)) { apiKeys = apiKeys.Where(a => a.Key == query.Key); } return(apiKeys.OrderBy(a => a.Name).ToArray()); }
public Task <ApiKey[]> GetApiKeys(ApiKeysQuery query) { var q = _liteRepository.Query <ApiKey>(); if (!string.IsNullOrEmpty(query.AppId)) { q = q.Where(a => a.AppId == query.AppId); } if (!string.IsNullOrEmpty(query.Name)) { q = q.Where(a => a.Name == query.Name); } if (!string.IsNullOrEmpty(query.Key)) { q = q.Where(a => a.Key == query.Key); } return(Task.FromResult(q.OrderBy(a => a.Name).ToArray())); }
public async Task <IActionResult> GetData(ExchangeRateFromDatesRequest req) { _logger.LogInformation("/ExchangeRate/GetFromDates"); try { if (req.ApiKey == _cache.Get("ApiKey") as string) { var result = await new ExchangeRateFromDatesBL().GetData(req, _cache); return(Ok(JsonConvert.SerializeObject(result))); } else { await Db.Connection.OpenAsync(); var query = new ApiKeysQuery(Db); var queryResult = await query.FindOneByKeyAsync(req.ApiKey); if (queryResult is null) { await Db.Connection.CloseAsync(); return(BadRequest("Nieprawidłowy klucz")); } else { var result = await new ExchangeRateFromDatesBL().GetData(req, _cache); await Db.Connection.CloseAsync(); return(Ok(JsonConvert.SerializeObject(result))); } } } catch (Exception ex) { return(NotFound(ex.Message)); } }