private static async Task <bool> VerifyUnsubscribeIntentAsync(HttpRequestMessage request, WebSubSubscription subscription, string topic) { bool verified = false; if (subscription.State == WebSubSubscriptionState.UnsubscribeRequested) { IWebSubSubscriptionsStore subscriptionsStore = request.GetWebSubSubscriptionsStore(); IWebSubSubscriptionsService subscriptionsService = request.GetWebSubSubscriptionsService(); if (subscription.TopicUrl != topic) { if (subscriptionsService != null) { await subscriptionsService.OnInvalidUnsubscribeIntentVerificationAsync(subscription, subscriptionsStore); } } else if ((subscriptionsService == null) || (await subscriptionsService.OnUnsubscribeIntentVerificationAsync(subscription, subscriptionsStore))) { subscription.State = WebSubSubscriptionState.UnsubscribeValidated; subscription.VerificationRequestTimeStampUtc = DateTime.UtcNow; await subscriptionsStore.UpdateAsync(subscription); verified = true; } } return(verified); }
private async Task <IActionResult> HandleIntentVerificationAsync(WebSubSubscription subscription, IQueryCollection requestQuery, IServiceProvider requestServices) { IActionResult intentVerificationResult = new NotFoundResult(); if (subscription != null) { IWebSubSubscriptionsStore subscriptionsStore = requestServices.GetRequiredService <IWebSubSubscriptionsStore>(); IWebSubSubscriptionsService subscriptionsService = requestServices.GetService <IWebSubSubscriptionsService>(); switch (requestQuery[WebSubConstants.MODE_QUERY_PARAMETER_NAME]) { case WebSubConstants.MODE_DENIED: intentVerificationResult = await HandleSubscribeIntentDenyAsync(subscription, subscriptionsStore, subscriptionsService, requestQuery); break; case WebSubConstants.MODE_SUBSCRIBE: intentVerificationResult = await HandleSubscribeIntentVerificationAsync(subscription, subscriptionsStore, subscriptionsService, requestQuery); break; case WebSubConstants.MODE_UNSUBSCRIBE: intentVerificationResult = await HandleUnsubscribeIntentVerificationAsync(subscription, subscriptionsStore, subscriptionsService, requestQuery); break; default: intentVerificationResult = HandleBadRequest($"A '{WebSubConstants.ReceiverName}' WebHook intent verification request contains unknown '{WebSubConstants.MODE_QUERY_PARAMETER_NAME}' query parameter value."); break; } } return(intentVerificationResult); }
private static async Task <HttpResponseMessage> HandleSubscribeIntentDenyAsync(HttpRequestMessage request, Dictionary <string, string> requestQuery, WebSubSubscription subscription) { if (!requestQuery.ContainsKey(TOPIC_QUERY_PARAMETER_NAME)) { return(HandleBadRequest(request, $"A '{ReceiverName}' WebHook subscribe intent deny request must contain a '{TOPIC_QUERY_PARAMETER_NAME}' query parameter.")); } requestQuery.TryGetValue(INTENT_DENY_REASON_QUERY_PARAMETER_NAME, out string reason); subscription.State = WebSubSubscriptionState.SubscribeDenied; IWebSubSubscriptionsStore subscriptionsStore = request.GetWebSubSubscriptionsStore(); await subscriptionsStore.UpdateAsync(subscription); IWebSubSubscriptionsService subscriptionsService = request.GetWebSubSubscriptionsService(); if (subscriptionsService != null) { await subscriptionsService.OnSubscribeIntentDenyAsync(subscription, reason, subscriptionsStore); } request.GetWebHooksLogger().Info($"Received a subscribe intent deny request for the '{ReceiverName}' WebHook receiver -- subscription denied, returning confirmation response."); return(request.CreateResponse(HttpStatusCode.NoContent)); }
private async Task <IActionResult> HandleUnsubscribeIntentVerificationAsync(WebSubSubscription subscription, IWebSubSubscriptionsStore subscriptionsStore, IWebSubSubscriptionsService subscriptionsService, IQueryCollection requestQuery) { StringValues topicValues = requestQuery[WebSubConstants.TOPIC_QUERY_PARAMETER_NAME]; if (StringValues.IsNullOrEmpty(topicValues)) { return(HandleMissingIntentVerificationParameter(WebSubConstants.TOPIC_QUERY_PARAMETER_NAME)); } StringValues challengeValues = requestQuery[WebSubConstants.INTENT_VERIFICATION_CHALLENGE_QUERY_PARAMETER_NAME]; if (StringValues.IsNullOrEmpty(challengeValues)) { return(HandleMissingIntentVerificationParameter(WebSubConstants.INTENT_VERIFICATION_CHALLENGE_QUERY_PARAMETER_NAME)); } if (await VerifyUnsubscribeIntentAsync(subscription, subscriptionsStore, subscriptionsService, topicValues)) { _logger.LogInformation("Received an unsubscribe intent verification request for the '{ReceiverName}' WebHook receiver -- verification passed, returning challenge response.", WebSubConstants.ReceiverName); return(new ContentResult { Content = challengeValues }); } else { _logger.LogInformation("Received an unsubscribe intent verification request for the '{ReceiverName}' WebHook receiver -- verification failed, returning challenge response.", WebSubConstants.ReceiverName); return(new NotFoundResult()); } }
private async Task <bool> VerifySubscribeIntentAsync(WebSubSubscription subscription, IWebSubSubscriptionsStore subscriptionsStore, IWebSubSubscriptionsService subscriptionsService, string topic, int leaseSeconds) { bool verified = false; if (subscription.State == WebSubSubscriptionState.SubscribeRequested) { if (subscription.TopicUrl != topic) { if (subscriptionsService != null) { await subscriptionsService.OnInvalidSubscribeIntentVerificationAsync(subscription, subscriptionsStore); } } else if ((subscriptionsService == null) || (await subscriptionsService.OnSubscribeIntentVerificationAsync(subscription, subscriptionsStore))) { subscription.State = WebSubSubscriptionState.SubscribeValidated; subscription.VerificationRequestTimeStampUtc = DateTime.UtcNow; subscription.LeaseSeconds = leaseSeconds; await subscriptionsStore.UpdateAsync(subscription); verified = true; } } return(verified); }
private async Task <IActionResult> HandleSubscribeIntentDenyAsync(WebSubSubscription subscription, IWebSubSubscriptionsStore subscriptionsStore, IWebSubSubscriptionsService subscriptionsService, IQueryCollection requestQuery) { StringValues topicValues = requestQuery[WebSubConstants.TOPIC_QUERY_PARAMETER_NAME]; if (StringValues.IsNullOrEmpty(topicValues)) { return(HandleBadRequest($"A '{WebSubConstants.ReceiverName}' WebHook subscribe intent deny request must contain a '{WebSubConstants.TOPIC_QUERY_PARAMETER_NAME}' query parameter.")); } StringValues reason = requestQuery[WebSubConstants.INTENT_DENY_REASON_QUERY_PARAMETER_NAME]; subscription.State = WebSubSubscriptionState.SubscribeDenied; await subscriptionsStore.UpdateAsync(subscription); if (subscriptionsService != null) { await subscriptionsService.OnSubscribeIntentDenyAsync(subscription, reason, subscriptionsStore); } _logger.LogInformation("Received a subscribe intent deny request for the '{ReceiverName}' WebHook receiver -- subscription denied, returning confirmation response.", WebSubConstants.ReceiverName); return(new NoContentResult()); }
public WebSubDependencyResolver(IDictionary <string, WebSubSubscription> webSubSubscriptionsStore) { _webSubSubscriptionsStore = new DictionaryWebSubSubscriptionsStore(webSubSubscriptionsStore); }
public SubscriptionsController(IWebSubSubscriptionsStore webSubSubscriptionsStore) { _webSubSubscriptionsStore = webSubSubscriptionsStore; }
private static Task <WebSubSubscription> RetrieveWebSubSubscriptionAsync(ResourceExecutingContext context, string subscriptionId) { IWebSubSubscriptionsStore subscriptionsStore = context.HttpContext.RequestServices.GetRequiredService <IWebSubSubscriptionsStore>(); return(subscriptionsStore.RetrieveAsync(subscriptionId)); }
public WebSubRequestServices(IDictionary <string, WebSubSubscription> webSubSubscriptionsStore) { _webSubSubscriptionsStore = new DictionaryWebSubSubscriptionsStore(webSubSubscriptionsStore); }
public async Task <bool> OnUnsubscribeIntentVerificationAsync(WebSubSubscription subscription, IWebSubSubscriptionsStore subscriptionsStore) { await _serverSentEventsService.SendEventAsync($"OnUnsubscribeIntentVerificationAsync ({subscription.Id})"); return(true); }
public Task OnInvalidUnsubscribeIntentVerificationAsync(WebSubSubscription subscription, IWebSubSubscriptionsStore subscriptionsStore) { return(_serverSentEventsService.SendEventAsync($"OnInvalidUnsubscribeIntentVerificationAsync ({subscription.Id})")); }
public Task OnSubscribeIntentDenyAsync(WebSubSubscription subscription, string reason, IWebSubSubscriptionsStore subscriptionsStore) { return(_serverSentEventsService.SendEventAsync($"OnSubscribeIntentDenyAsync ({subscription.Id})")); }