public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            // assumes that this middleware has been mapped
            var token = context.Request.Path.ToString();

            if (token.StartsWith("/"))
            {
                token = token.Substring(1);
            }

            if (!_responseStore.TryGetResponse(token, out var value))
            {
                await next(context);

                return;
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogDebug("Confirmed challenge request for {token}", token);
            }

            context.Response.ContentLength = value.Length;
            context.Response.ContentType   = "application/octet-stream";
            await context.Response.WriteAsync(value, context.RequestAborted);
        }
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var token = context.Request.Path.ToString();

            _logger.LogDebug("Requested challenge request for {token}", token);

            // assumes that this middleware has been mapped
            if (token.StartsWith("/"))
            {
                token = token.Substring(1);
            }

            if (!_httpChallengeResponseStore.TryGetResponse(token, out var orderInfo))
            {
                await next(context);

                return;
            }

            _logger.LogDebug("Confirmed challenge request for {token}", token);

            context.Response.ContentLength = orderInfo.Challenge.KeyAuthz.Length;
            context.Response.ContentType   = "application/octet-stream";
            await context.Response.WriteAsync(orderInfo.Challenge.KeyAuthz, context.RequestAborted);

            _ = Task.Run(async() =>
            {
                // Give some time to Let´s Encrypt to process our response
                await Task.Delay(30 * 1000);
                await _certificateBuilderService.BuildCertificate(orderInfo.Order, orderInfo.HostName);
            });
        }
        public async Task InvokeAsync(HttpContext context, IHttpChallengeResponseStore responseStore, IEnumerable <ILogger> loggers)
        {
            var    logger = loggers.FirstOrDefault();
            string token  = context.GetRouteValue("acmeToken") as string;

            logger?.LogDebug("ACME http challenge request with token {token} received.", token);
            if (!responseStore.TryGetResponse(token, out string response))
            {
                logger?.LogWarning("Transmitted ACME http challenge token invalid. Token received: {token}", token);
                await context.Response.WriteAsync("ACME challenge token invalid");

                return;
            }

            logger?.LogInformation("Transmitted ACME http challenge token valid.");
            context.Response.ContentLength = response.Length;
            context.Response.ContentType   = "application/octet-stream";
            await context.Response.WriteAsync(response);
        }