private async Task _continueVerify(string mode, string callback, string topic, string lease_seconds, string secret, APEntity user) { await Task.Delay(2000); var hc = new HttpClient(); var testurl = callback; if (callback.Contains("?")) { testurl += "&"; } else { testurl += "?"; } string challenge = Guid.NewGuid().ToString(); testurl += $"hub.mode={mode}&hub.topic={Uri.EscapeDataString(topic)}&hub.lease_seconds={lease_seconds}&hub.challenge={Uri.EscapeDataString(challenge)}"; var result = await hc.GetAsync(testurl); if (!result.IsSuccessStatusCode) { return; } if (await result.Content.ReadAsStringAsync() != challenge) { return; } WebsubSubscription subscription = await _context.WebsubSubscriptions.FirstOrDefaultAsync(a => a.Callback == callback); if (subscription != null) { subscription.Expiry = DateTime.Now.AddSeconds(int.Parse(lease_seconds ?? "86400")); subscription.Secret = secret; if (mode == "unsubscribe") { _context.WebsubSubscriptions.Remove(subscription); } } else if (mode == "subscribe") { subscription = new WebsubSubscription() { Callback = callback, Expiry = DateTime.Now.AddSeconds(int.Parse(lease_seconds ?? "86400")), Secret = secret, User = user }; _context.WebsubSubscriptions.Add(subscription); } await _context.SaveChangesAsync(); }
public override async Task Go() { _subscription = await _connection.QuerySingleOrDefaultAsync <WebsubSubscription>("select * from WebsubSubscriptions where Id = @Id", new { Id = Data.Subscription }); if (_subscription == null) { return; // gone } var objdata = await _object(); // building fake feed because it's wanted var hc = new HttpClient(); var serialized = Encoding.UTF8.GetBytes((await _entryGenerator.Build(objdata)).ToString(System.Xml.Linq.SaveOptions.DisableFormatting)); var content = new ByteArrayContent(serialized); content.Headers.ContentType = new MediaTypeHeaderValue("application/atom+xml"); var request = new HttpRequestMessage(HttpMethod.Post, _subscription.Callback) { Content = content }; if (!string.IsNullOrWhiteSpace(_subscription.Secret)) { var sig = _calculateSignature(serialized); request.Headers.TryAddWithoutValidation("X-Hub-Signature", sig); } var result = await hc.SendAsync(request); if (!result.IsSuccessStatusCode && (int)result.StatusCode / 100 == 5) { throw new Exception("Failed to deliver. Retrying later."); } }
private async Task _continueVerify(string mode, string callback, string topic, string lease_seconds, string secret, APEntity user) { await Task.Delay(2000); var hc = new HttpClient(); var testurl = callback; if (callback.Contains("?")) { testurl += "&"; } else { testurl += "?"; } string challenge = Guid.NewGuid().ToString(); testurl += $"hub.mode={mode}&hub.topic={Uri.EscapeDataString(topic)}&hub.lease_seconds={lease_seconds}&hub.challenge={Uri.EscapeDataString(challenge)}"; var result = await hc.GetAsync(testurl); if (!result.IsSuccessStatusCode) { return; } if (await result.Content.ReadAsStringAsync() != challenge) { return; } WebsubSubscription subscription = await _connection.QueryFirstOrDefaultAsync <WebsubSubscription>("select * from \"WebsubSubscriptions\" where \"Callback\" = @Callback", new { Callback = callback }); if (subscription != null) { if (mode == "unsubscribe") { await _connection.ExecuteAsync("delete from \"WebsubSubscriptions\" where \"Id\" = @Id", new { Id = subscription.Id }); } else { subscription.Expiry = DateTime.Now.AddSeconds(int.Parse(lease_seconds ?? "86400")); subscription.Secret = secret; await _connection.ExecuteAsync("update \"WebsubSubscriptions\" set \"Expiry\"=@Expiry, \"Secret\"=@Secret where \"Id\" = @Id", subscription); } } else if (mode == "subscribe") { subscription = new WebsubSubscription() { Callback = callback, Expiry = DateTime.Now.AddSeconds(int.Parse(lease_seconds ?? "86400")), Secret = secret, UserId = user.DbId }; await _connection.ExecuteAsync("insert into \"WebsubSubscriptions\" (\"Callback\", \"Expiry\", \"Secret\", \"UserId\") values (@Callback, @Expiry, @Secret, @UserId)", subscription); } }