Esempio n. 1
0
        public async Task Kafka(
            [KafkaTrigger(
                 "notification.services.tonlabs.io:29092",
                 "notifications-9",
                 ConsumerGroup = "$Default",
                 Protocol = BrokerProtocol.SaslPlaintext,
                 AuthenticationMode = BrokerAuthenticationMode.ScramSha512,
                 Username = "******",
                 Password = "******")]
            KafkaEventData <string> kafkaEvent, ILogger log)
        {
            var hash      = string.Empty;
            var nonce     = string.Empty;
            var encrypted = string.Empty;

            try
            {
                var input = kafkaEvent.Value.Split(' ');
                hash      = input[0];
                nonce     = input[1].Base64ToHex();
                encrypted = input[2];
                var eventReceiver = _eventReceivers
                                    .Query <EventReceiver>($"PartitionKey eq '{hash}'")
                                    .Single();

                if (eventReceiver.Url != "https://freeton.org" && !eventReceiver.IsVerified)
                {
                    return;
                }

                var context = new Context().WithLogger(log);
                var client  = _httpClientFactory.CreateClient("pollyClient");
                var request = new HttpRequestMessage(HttpMethod.Post, eventReceiver.Url);
                request.Content = new StringContent(encrypted, Encoding.UTF8, "text/plain");
                request.SetPolicyExecutionContext(context);
                var response = await client.SendAsync(request);

                log.LogInformation(nameof(Kafka), new[] { new { hash, statusCode = response.StatusCode } });
                await _kafkaMessages.AddEntityAsync(new KafkaMessage
                {
                    PartitionKey = hash,
                    RowKey       = hash,
                    Nonce        = nonce,
                    Encrypted    = encrypted
                });
            }
            catch (Exception ex)
            {
                log.LogError(
                    ex,
                    nameof(Kafka),
                    new[] { new
                            {
                                hash,
                                nonce,
                                encrypted
                            } });
                throw;
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Verify(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "verify/{token}")] HttpRequest req,
            string token,
            ILogger log)
        {
            try
            {
                log.LogInformation(nameof(Verify), new[] { new { token } });
                var httpClient    = new HttpClient();
                var eventReceiver = _eventReceivers
                                    .Query <EventReceiver>($"Token eq '{token}'")
                                    .Single();

                if (eventReceiver.IsVerified)
                {
                    return(new OkObjectResult("This domain already was verified"));
                }

                var verifiers = new Func <Task <bool> >[]
                {
                    async() =>
                    {
                        var response = await httpClient.GetAsync(eventReceiver.Url);

                        var content = await response.Content.ReadAsStringAsync();

                        return(response.StatusCode == HttpStatusCode.OK &&
                               content == token);
                    },
                    async() =>
                    {
                        var uri    = new Uri(eventReceiver.Url);
                        var lookup = new LookupClient(new[]
                        {
                            IPAddress.Parse("8.8.8.8")
                        });

                        return((await lookup
                                .QueryAsync(uri.Host, QueryType.TXT, QueryClass.IN))
                               .Answers
                               .TxtRecords()
                               .Any(x => x.Text.Any(t => t == token)));
                    }
                };

                return(new OkObjectResult(await Verify(eventReceiver, verifiers)));
            }
            catch (Exception ex)
            {
                log.LogError(
                    ex,
                    nameof(Verify),
                    new[] { new
                            {
                                token
                            } });
                throw;
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Register(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, ILogger log)
        {
            var query = string.Empty;

            try
            {
                var responseMessage = string.Empty;
                var token           = Guid.NewGuid().ToString().Replace("-", "");
                query = await req.ReadAsStringAsync();

                var queryString = HttpUtility.ParseQueryString(query);
                var hash        = queryString["hash"];
                var url         = queryString["data"].Base64ToUtf8();
                log.LogInformation(
                    nameof(Register),
                    new[] { new
                            {
                                hash,
                                url,
                                token
                            } });

                if (!Uri.TryCreate(url, UriKind.Absolute, out Uri _))
                {
                    return(new OkObjectResult("Please provide absolute URL."));
                }

                var entity = _eventReceivers
                             .Query <EventReceiver>($"PartitionKey eq '{hash }'")
                             .SingleOrDefault();

                if (entity == null)
                {
                    var response = await _eventReceivers.AddEntityAsync(new EventReceiver
                    {
                        PartitionKey = hash,
                        RowKey       = hash,
                        Url          = url,
                        Token        = token
                    });

                    responseMessage += $"URL '{url}' successfully added.\n\n";
                }
                else if (entity.Url != url)
                {
                    entity.Url        = url;
                    entity.IsVerified = false;
                    entity.Token      = token;
                    var response = await _eventReceivers.UpdateEntityAsync(entity, entity.ETag);

                    responseMessage += "URL '{url}' successfully updated.\n\n";
                }

                if (entity == null || !entity.IsVerified)
                {
                    responseMessage +=
                        $"Your token is: {token}\n\n" +

                        $"Please confirm ownership of entered domain.\n" +
                        $"You can verify it with one of the options:\n" +
                        $" - respond to HTTP GET request with a token\n" +
                        $" - add token TXT record to your DNS configuration with\n\n" +

                        $"After you will complete this step you can " +
                        $"proceed with domain verification by navigating " +
                        $"to: https://ton.azurewebsites.net/api/verify/{token}\n\n";
                }

                return(new OkObjectResult(responseMessage));
            }
            catch (Exception ex)
            {
                log.LogError(
                    ex,
                    nameof(Register),
                    new[] { new
                            {
                                query
                            } });
                throw;
            }
        }