public async Task PingValidatorAsync([FromBody] PingValidatorRequest request)
        {
            var(auth, tenantId, adminId, adminEmail) = Authorize();

            if (!auth)
            {
                return;
            }

            var entity = _validationReader.Get(ValidatorLinkEntity.GeneratePartitionKey(tenantId),
                                               ValidatorLinkEntity.GenerateRowKey(request.ApiKeyId));

            if (entity == null)
            {
                _logger.LogInformation("Cannot send ping. ValidatorLinkEntity not found by API key: {ApiKeyId}", request.ApiKeyId);
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return;
            }

            if (!entity.IsAccepted)
            {
                _logger.LogInformation("Cannot send ping. ValidatorLinkEntity is not accepted: {ApiKeyId}", request.ApiKeyId);
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return;
            }

            if (string.IsNullOrEmpty(request.Message))
            {
                _logger.LogInformation("Cannot send ping. Message cannot be empty: {ApiKeyId}", request.ApiKeyId);
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return;
            }

            if (Encoding.UTF8.GetBytes(request.Message).Length > 100)
            {
                _logger.LogInformation("Cannot send ping. Message length in bytes more that 100: {ApiKeyId}; Message: {Message}", request.ApiKeyId, request.Message);
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return;
            }

            var message = PingMessageMyNoSqlEntity.Generate(entity.ValidatorId, request.Message);

            await _pingMessageWriter.InsertOrReplaceAsync(message);

            _logger.LogInformation("Ping message are created. API key: {ApiKeyId}; ValidatorId: {ValidatorId}; TenantId: {TenantId}; Name: {Name}; AdminId: {AdminId}", request.ApiKeyId, entity.ValidatorId, entity.TenantId, entity.Name, adminId);
        }
Exemple #2
0
        public override async Task <PingResponse> GetPing(PingRequest request, ServerCallContext context)
        {
            var validatorId = context.GetHttpContext().User.GetClaimOrDefault(Claims.KeyKeeperId);
            var publicKey   = context.GetHttpContext().User.GetClaimOrDefault(Claims.PublicKeyPem);

            if (string.IsNullOrEmpty(publicKey))
            {
                return(new PingResponse()
                {
                    Error = new ValidatorApiError()
                    {
                        Code = ValidatorApiError.Types.ErrorCodes.InternalServerError,
                        Message = "Incorrect Bearer Token."
                    }
                });
            }

            var message = _pingMessageReader.Get(PingMessageMyNoSqlEntity.GeneratePartitionKey(validatorId),
                                                 PingMessageMyNoSqlEntity.GenerateRowKey());

            var response = new PingResponse();

            if (message == null)
            {
                response.MessageEnc       = string.Empty;
                response.SignatureMessage = string.Empty;
            }
            else
            {
                var asynccrypto = new AsymmetricEncryptionService();
                var messageEnc  = asynccrypto.Encrypt(Encoding.UTF8.GetBytes(message.Message), publicKey);

                response.MessageEnc       = Convert.ToBase64String(messageEnc);
                response.SignatureMessage = "not-implemented-please-skip";

                await _pingMessageWriter.DeleteAsync(message.PartitionKey, message.RowKey);
            }

            _logger.LogInformation("GetPing response. ValidatorId='{ValidatorId}'; HasMessage={HasMessage}", validatorId, !string.IsNullOrEmpty(response.MessageEnc));

            return(response);
        }