public async Task <IActionResult> ProcessApiKeyTopic([FromBody] TopicData <ApiKey> apiKeyTopicData) { if (!string.IsNullOrWhiteSpace(apiKeyTopicData.Data.Id)) { _logger.LogInformation($"Receive apiKey topic type {apiKeyTopicData.TopicType} id: {apiKeyTopicData.Data.Id}"); string cacheKey = $"{CacheConstant.ApiKey_}{apiKeyTopicData.Data.Id}"; switch (apiKeyTopicData.TopicType) { case TopicType.Remove: await _apiKeyCacheService.ClearCacheAsync(_daprClient, cacheKey); break; case TopicType.Modify: await _apiKeyCacheService.UpdateCacheAsync(_daprClient, cacheKey, apiKeyTopicData.Data); break; } } else { _logger.LogWarning($"Receive apiKey topic without id"); } return(Ok()); }
protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.TryGetValue(HttpHeaderConstant.ApiKey, out var apiKeyHeaderValues)) { return(AuthenticateResult.NoResult()); } var providedApiKey = apiKeyHeaderValues.FirstOrDefault(); if (string.IsNullOrWhiteSpace(providedApiKey)) { return(AuthenticateResult.NoResult()); } var splitValues = providedApiKey.Split('.'); if (splitValues == null || splitValues.Length != 2) { return(AuthenticateResult.Fail("Invalid API Key provided.")); } ApiKey?apiKey = null; string tenantId = splitValues[0]; providedApiKey = splitValues[1]; var secretValues = await _daprClient.GetSecretAsync(ConfigConstant.CodexKey, ConfigConstant.MicroserviceApiKey); var microserviceApiKey = secretValues[ConfigConstant.MicroserviceApiKey]; if (providedApiKey == microserviceApiKey) { apiKey = new(id : "inter-call-api-key", name : "Inter call Micro Services", new() { RoleConstant.ADMIN }); } else { try { string cacheKey = $"{CacheConstant.ApiKey_}{providedApiKey}"; apiKey = await _apiKeyCacheService.GetCacheAsync(_daprClient, cacheKey); if (apiKey == null) { apiKey = await _daprClient.InvokeMethodAsync <ApiKey>(ApiNameConstant.SecurityApi, $"ApiKey/{providedApiKey}", new HTTPExtension() { Verb = HTTPVerb.Get, Headers = { { HttpHeaderConstant.TenantId, tenantId }, { HttpHeaderConstant.ApiKey, $"{tenantId}.{microserviceApiKey}" } } } ); await _apiKeyCacheService.UpdateCacheAsync(_daprClient, cacheKey, apiKey); } } catch (Exception exception) { if (exception is Grpc.Core.RpcException rpcException && rpcException.Status.StatusCode == Grpc.Core.StatusCode.NotFound) { _logger.LogInformation(rpcException, $"ApiKey not found : '{apiKey?.Id}'"); }