コード例 #1
2
        public async Task<IHttpActionResult> Post(WebHook webHook)
        {
            if (webHook == null)
            {
                return BadRequest();
            }

            string userId = await GetUserId();
            await VerifyFilters(webHook);
            await VerifyWebHook(webHook);

            try
            {
                // Validate the provided WebHook ID (or force one to be created on server side)
                IWebHookIdValidator idValidator = Configuration.DependencyResolver.GetIdValidator();
                await idValidator.ValidateIdAsync(Request, webHook);

                // Add WebHook for this user.
                StoreResult result = await _store.InsertWebHookAsync(userId, webHook);
                if (result == StoreResult.Success)
                {
                    return CreatedAtRoute(WebHookRouteNames.RegistrationLookupAction, new { id = webHook.Id }, webHook);
                }
                return CreateHttpResult(result);
            }
            catch (Exception ex)
            {
                string msg = string.Format(CultureInfo.CurrentCulture, CustomApiResources.RegistrationController_RegistrationFailure, ex.Message);
                Configuration.DependencyResolver.GetLogger().Error(msg, ex);
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, msg, ex);
                return ResponseMessage(error);
            }
        }
コード例 #2
2
 public void the_WebHook_will_be_invalid_if_they_signature_is_empty()
 {
     var webHook = new WebHook(secret, string.Empty, validBody);
     Assert.IsFalse(webHook.IsValid);
 }
コード例 #3
1
ファイル: WebHook.cs プロジェクト: pusher/pusher-http-dotnet
        public void the_WebHook_will_be_invalid_if_they_signature_is_null()
        {
            var webHook = new WebHook(secret, null, validBody);

            Assert.IsFalse(webHook.IsValid);
            Assert.AreEqual(2, webHook.ValidationErrors.Length);
            StringAssert.IsMatch(@"The supplied signature to check was null or empty\. A signature to check must be provided\.", webHook.ValidationErrors[0]);
            StringAssert.IsMatch(@"The signature did not validate\. Expected \. Got 003a63ce4da20830c4fecffb63d5b3944b64989b6458e15b26e08e244f758954", webHook.ValidationErrors[1]);
        }
コード例 #4
1
        public async Task VerifyFilter_SetsWildcard_IfNoFiltersProvided()
        {
            // Arrange
            WebHook webHook = new WebHook();

            // Act
            await _controller.VerifyFilters(webHook);

            // Assert
            Assert.Equal("*", webHook.Filters.Single());
        }
コード例 #5
0
 public WebHookExtensionsTests()
 {
     _webHook = new WebHook();
     _webHook.Filters.Add("action");
     _webHook.Filters.Add("你好");
     _webHook.Filters.Add("世界");
 }
コード例 #6
0
        public async Task<IHttpActionResult> Post(WebHook webHook)
        {
            if (webHook == null)
            {
                return BadRequest();
            }

            string userId = await GetUserId();
            await VerifyFilters(webHook);
            await VerifyWebHook(webHook);

            try
            {
                // Ensure we have a normalized ID for the WebHook
                webHook.Id = null;

                // Add WebHook for this user.
                StoreResult result = await _store.InsertWebHookAsync(userId, webHook);
                if (result == StoreResult.Success)
                {
                    return CreatedAtRoute(WebHookRouteNames.RegistrationLookupAction, new { id = webHook.Id }, webHook);
                }
                return CreateHttpResult(result);
            }
            catch (Exception ex)
            {
                string msg = string.Format(CultureInfo.CurrentCulture, CustomApiResources.RegistrationController_RegistrationFailure, ex.Message);
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, msg, ex);
                return ResponseMessage(error);
            }
        }
コード例 #7
0
ファイル: WebHook.cs プロジェクト: pusher/pusher-http-dotnet
        public void the_WebHook_will_be_invalid_if_they_body_is_null()
        {
            var webHook = new WebHook(secret, validSignature, null);

            Assert.IsFalse(webHook.IsValid);
            Assert.AreEqual(1, webHook.ValidationErrors.Length);
            StringAssert.IsMatch(@"The supplied body to check was null or empty\. A body to check must be provided\.", webHook.ValidationErrors[0]);
        }
コード例 #8
0
        public void the_WebHook_time_in_ms_is_correctly_parsed()
        {
            var fakeMillis = "1423850522000";
            var expectedDate = new DateTime(2015, 2, 13, 18, 2, 2, DateTimeKind.Utc);
            var secret = "1c9c753dddfd049dd7f1";
            var body = "{\"time_ms\":" + fakeMillis + ",\"events\":[{\"channel\":\"test_channel\",\"name\":\"channel_occupied\"}]}";
            var expectedSignature = GenerateValidSignature(secret, body);

            var webHook = new WebHook(secret, expectedSignature, body);
            Assert.AreEqual(expectedDate, webHook.Time);
        }
コード例 #9
0
        public async Task ValidateIfAsync_ForcesDefaultId(string id)
        {
            // Arrange
            WebHook webHook = new WebHook { Id = id };

            // Act
            await _validator.ValidateIdAsync(_request, webHook);

            // Assert
            Assert.NotEmpty(webHook.Id);
            Assert.NotEqual(id, webHook.Id);
        }
コード例 #10
0
        public void the_WebHook_can_contain_multiple_events()
        {
            var body = "{\"time_ms\": 1327078148132, \"events\": " +
                "[" +
                    "{\"name\": \"channel_occupied\", \"channel\": \"test_channel\" }," +
                    "{\"name\": \"channel_vacated\", \"channel\": \"test_channel2\" }" +
                "]}";

            var webHook = new WebHook(secret, validSignature, body);
            Assert.AreEqual("test_channel", webHook.Events[0]["channel"]);
            Assert.AreEqual("channel_occupied", webHook.Events[0]["name"]);

            Assert.AreEqual("test_channel2", webHook.Events[1]["channel"]);
            Assert.AreEqual("channel_vacated", webHook.Events[1]["name"]);
        }
コード例 #11
0
 public static IEnumerable<WebHookWorkItem> CreateWorkItems(int count)
 {
     WebHookWorkItem[] workItems = new WebHookWorkItem[count];
     for (int cnt = 0; cnt < count; cnt++)
     {
         WebHook webHook = new WebHook
         {
             WebHookUri = new Uri("http://localhost/path/" + count),
             Secret = "0123456789012345678901234567890123456789" + count
         };
         NotificationDictionary notification = new NotificationDictionary("a" + cnt, cnt);
         WebHookWorkItem workItem = new WebHookWorkItem(webHook, new[] { notification });
         workItem.Properties[AzureWebHookDequeueManager.QueueMessageKey] = new CloudQueueMessage("content");
         workItems[cnt] = workItem;
     }
     return workItems;
 }
コード例 #12
0
        public async Task<IHttpActionResult> Put(string id, WebHook webHook)
        {
            if (webHook == null)
            {
                return BadRequest();
            }
            if (!string.Equals(id, webHook.Id, StringComparison.OrdinalIgnoreCase))
            {
                return BadRequest();
            }

            string userId = await GetUserId();
            await VerifyFilters(webHook);
            await VerifyWebHook(webHook);

            StoreResult result = await _store.UpdateWebHookAsync(userId, webHook);
            return CreateHttpResult(result);
        }
コード例 #13
0
        /// <summary>
        /// Ensures that the provided <paramref name="webHook"/> has a reachable Web Hook URI.
        /// </summary>
        protected virtual async Task VerifyWebHook(WebHook webHook)
        {
            if (webHook == null)
            {
                throw new ArgumentNullException("webHook");
            }

            try
            {
                await _manager.VerifyWebHookAsync(webHook);
            }
            catch (Exception ex)
            {
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message, ex);
                throw new HttpResponseException(error);
            }
        }
コード例 #14
0
        /// <summary>
        /// Ensure that the provided <paramref name="webHook"/> only has registered filters.
        /// </summary>
        protected virtual async Task VerifyFilters(WebHook webHook)
        {
            if (webHook == null)
            {
                throw new ArgumentNullException("webHook");
            }

            // If there are no filters then add our wildcard filter.
            if (webHook.Filters.Count == 0)
            {
                webHook.Filters.Add(WildcardWebHookFilterProvider.Name);
                return;
            }

            IWebHookFilterManager filterManager = Configuration.DependencyResolver.GetFilterManager();
            IDictionary<string, WebHookFilter> filters = await filterManager.GetAllWebHookFiltersAsync();
            HashSet<string> normalizedFilters = new HashSet<string>();
            List<string> invalidFilters = new List<string>();
            foreach (string filter in webHook.Filters)
            {
                WebHookFilter hookFilter;
                if (filters.TryGetValue(filter, out hookFilter))
                {
                    normalizedFilters.Add(hookFilter.Name);
                }
                else
                {
                    invalidFilters.Add(filter);
                }
            }

            if (invalidFilters.Count > 0)
            {
                string invalidFiltersMsg = string.Join(", ", invalidFilters);
                string link = Url.Link(WebHookRouteNames.FiltersGetAction, routeValues: null);
                string msg = string.Format(CultureInfo.CurrentCulture, CustomApiResources.RegistrationController_InvalidFilters, invalidFiltersMsg, link);
                Configuration.DependencyResolver.GetLogger().Info(msg);

                HttpResponseMessage response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                throw new HttpResponseException(response);
            }
            else
            {
                webHook.Filters.Clear();
                foreach (string filter in normalizedFilters)
                {
                    webHook.Filters.Add(filter);
                }
            }
        }
コード例 #15
0
 public void the_WebHook_will_throw_exception_if_secret_is_null()
 {
     var webHook = new WebHook(null, validSignature, validBody);
 }
コード例 #16
0
 public void the_event_name_can_be_retrieved_from_the_WebHook()
 {
     var webHook = new WebHook(secret, validSignature, validBody);
     Assert.AreEqual("channel_occupied", webHook.Events[0]["name"]);
 }
コード例 #17
0
        //public ActionResult Verify()
        //{
        //    string url =
        //}
        public async Task <ActionResult> Index()
        {
            await WebHook.GetProjects();

            return(View());
        }
コード例 #18
0
        public async Task VerifyFilter_Throws_IfInvalidFilters()
        {
            // Arrange
            WebHook webHook = new WebHook();
            webHook.Filters.Add("Unknown");
            Collection<WebHookFilter> filters = new Collection<WebHookFilter>
            {
            };
            _filterProviderMock.Setup(p => p.GetFiltersAsync())
                .ReturnsAsync(filters)
                .Verifiable();

            // Act
            HttpResponseException ex = await Assert.ThrowsAsync<HttpResponseException>(() => _controller.VerifyFilters(webHook));

            // Assert
            HttpError error = await ex.Response.Content.ReadAsAsync<HttpError>();
            Assert.Equal("The following filters are not valid: 'Unknown'. A list of valid filters can be obtained from the path 'http://localhost/'.", error.Message);
            _filterProviderMock.Verify();
        }
コード例 #19
0
 private static WebHook CreateWebHook(string filterName = FilterName)
 {
     WebHook webHook = new WebHook()
     {
         Id = TestWebHookId,
         WebHookUri = new Uri(Address)
     };
     webHook.Filters.Add(filterName);
     return webHook;
 }
コード例 #20
0
 public new Task VerifyFilters(WebHook webHook)
 {
     return base.VerifyFilters(webHook);
 }
コード例 #21
0
ファイル: RESTServer.cs プロジェクト: ziccardi/fehbot
        void RemoveWebhook(WebHook webHook, HttpListenerResponse response)
        {
            var result = db.DeleteWebHooks(webHook);

            SendResponse(response, "deleted " + result.DeletedCount, 200);
        }
コード例 #22
0
 private static WebHook CreateWebHook(string user, int offset, string filter = "a1", bool addPrivateFilter = false)
 {
     WebHook hook = new WebHook
     {
         Id = offset.ToString(),
         Description = user,
         Secret = "123456789012345678901234567890123456789012345678",
         WebHookUri = new Uri("http://localhost/hook/" + offset),
     };
     hook.Headers.Add("h1", "hv1");
     hook.Properties.Add("p1", "pv1");
     hook.Filters.Add(filter);
     if (addPrivateFilter)
     {
         string privateFilter = WebHookRegistrar.PrivateFilterPrefix + "abc";
         hook.Filters.Add(privateFilter);
     }
     return hook;
 }
コード例 #23
0
 /// <summary>
 /// Determines whether a given <paramref name="eventName"/> matches the subscribed events for a given <see cref="WebHook"/>.
 /// The action can either match an event directly or match a wildcard.
 /// </summary>
 /// <param name="webHook">The <see cref="WebHook"/> instance to operate on.</param>
 /// <param name="eventName">The topic to match against the subscribed <paramref name="webHook"/> topics.</param>
 /// <returns><c>true</c> if the <paramref name="eventName"/> matches, otherwise <c>false</c>.</returns>
 public static bool MatchesEvent(this WebHook webHook, string eventName)
 {
     return(webHook != null && (webHook.Events.Contains(WildcardWebHookEvent.WildcardEvent.Name) || webHook.Events.Contains(eventName)));
 }
コード例 #24
0
        public async Task VerifyFilter_Throws_IfRegistrarThrows()
        {
            // Arrange
            Exception ex = new Exception("Catch this!");
            WebHook webHook = new WebHook();
            webHook.Filters.Add(FilterName);
            _registrarMock.Setup(r => r.RegisterAsync(_controllerContext.Request, webHook))
                .Throws(ex);

            // Act
            HttpResponseException rex = await Assert.ThrowsAsync<HttpResponseException>(() => _controller.VerifyFilters(webHook));

            // Assert
            HttpError error = await rex.Response.Content.ReadAsAsync<HttpError>();
            Assert.Equal("The 'IWebHookRegistrarProxy' implementation of 'IWebHookRegistrar' caused an exception: Catch this!", error.Message);
        }
コード例 #25
0
        public async Task VerifyFilter_Calls_RegistrarWithFilter()
        {
            // Arrange
            WebHook webHook = new WebHook();
            webHook.Filters.Add(FilterName);
            _registrarMock.Setup(r => r.RegisterAsync(_controllerContext.Request, webHook))
                .Returns(Task.FromResult(true))
                .Verifiable();

            // Act
            await _controller.VerifyFilters(webHook);

            // Assert
            _registrarMock.Verify();
        }
コード例 #26
0
 public void the_WebHook_will_be_invalid_if_they_signature_is_null()
 {
     var webHook = new WebHook(secret, null, validBody);
     Assert.IsFalse(webHook.IsValid);
 }
コード例 #27
0
 public void the_WebHook_will_be_valid_if_all_params_are_as_expected()
 {
     var webHook = new WebHook(secret, validSignature, validBody);
     Assert.IsTrue(webHook.IsValid);
 }
コード例 #28
0
        public void the_WebHook_will_be_valid_given_alternative_values()
        {
            var signature = "851f492bab8f7652a2e4c82cd0212d97b4e678edf085c06bf640ed45ee7b1169";
            var secret = "1c9c753dddfd049dd7f1";
            var body = "{\"time_ms\":1423778833207,\"events\":[{\"channel\":\"test_channel\",\"name\":\"channel_occupied\"}]}";

            var webHook = new WebHook(secret, signature, body);
            Assert.IsTrue(webHook.IsValid);
        }
コード例 #29
0
 public void the_WebHook_will_throw_exception_if_secret_is_empty()
 {
     var webHook = new WebHook(string.Empty, validSignature, validBody);
 }
コード例 #30
0
        public async Task VerifyFilter_Throws_HttpException_IfRegistrarThrows()
        {
            // Arrange
            HttpResponseException rex = new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Conflict));
            WebHook webHook = new WebHook();
            webHook.Filters.Add(FilterName);
            _registrarMock.Setup(r => r.RegisterAsync(_controllerContext.Request, webHook))
                .Throws(rex);

            // Act
            HttpResponseException ex = await Assert.ThrowsAsync<HttpResponseException>(() => _controller.VerifyFilters(webHook));

            // Assert
            Assert.Same(rex, ex);
        }
コード例 #31
0
        public async Task VerifyFilter_Adds_NormalizedFilters(string input, string expected)
        {
            // Arrange
            WebHook webHook = new WebHook();
            webHook.Filters.Add(input);
            Collection<WebHookFilter> filters = new Collection<WebHookFilter>
            {
                new WebHookFilter { Name = expected }
            };
            _filterProviderMock.Setup(p => p.GetFiltersAsync())
                .ReturnsAsync(filters)
                .Verifiable();

            // Act
            await _controller.VerifyFilters(webHook);

            // Assert
            _filterProviderMock.Verify();
            Assert.Equal(expected, webHook.Filters.Single());
        }
コード例 #32
0
 public dynamic Create(WebHook webhook)
 {
     return(ApiRequest("webhooks", webhook, Method.POST, typeof(WebHooksResponse)));
 }
コード例 #33
0
        public void RemovePrivateFilters_Succeeds(int count, string[] input, string[] expected)
        {
            // Arrange
            List<WebHook> webHooks = new List<WebHook>();
            for (int cnt = 0; cnt < count; cnt++)
            {
                WebHook webHook = new WebHook();
                foreach (string i in input)
                {
                    webHook.Filters.Add(i);
                }
                webHooks.Add(webHook);
            }

            // Act
            _controller.RemovePrivateFilters(webHooks);

            // Assert
            for (int cnt = 0; cnt < count; cnt++)
            {
                Assert.Equal(expected, webHooks[cnt].Filters);
            }
        }
コード例 #34
0
ファイル: DbWebHookStore.cs プロジェクト: niels013/WebHooks
 private static bool DefaultPredicate(WebHook webHook, string user)
 {
     return(true);
 }
コード例 #35
0
        public async Task <IHttpActionResult> HandleWebhook(string id, WebHook webhook)
        {
            return(BadRequest("Unable to determine webhook or id"));

            return(Ok());
        }
コード例 #36
0
 /// <inheritdoc />
 public abstract Task RegisterAsync(HttpRequest request, WebHook webHook);
コード例 #37
0
 public new Task AddPrivateFilters(string user, WebHook webHook)
 {
     return(base.AddPrivateFilters(user, webHook));
 }
コード例 #38
0
        public async Task <IHttpActionResult> RegisterWebHook(WebHook webHook)
        {
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            if (webHook == null)
            {
                return(BadRequest());
            }

            string userId = GetUserId();

            await VerifyFilters(webHook);
            await VerifyWebHook(webHook);

            // In order to ensure that a web hook filter is not registered multiple times for the same uri
            // we remove the already registered filters from the current web hook.
            // If the same filters are registered multiple times with the same uri, the web hook event will be
            // sent for each registration.
            IEnumerable <WebHook> existingWebhooks = await GetAllWebHooks();

            IEnumerable <WebHook> existingWebhooksForTheSameUri = existingWebhooks.Where(wh => wh.WebHookUri == webHook.WebHookUri);

            foreach (var existingWebHook in existingWebhooksForTheSameUri)
            {
                webHook.Filters.ExceptWith(existingWebHook.Filters);

                if (!webHook.Filters.Any())
                {
                    string msg = _localizationService.GetResource("Api.WebHooks.CouldNotRegisterDuplicateWebhook");
                    HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.Conflict, msg);
                    return(ResponseMessage(error));
                }
            }

            try
            {
                // Validate the provided WebHook ID (or force one to be created on server side)
                if (Request == null)
                {
                    throw new ArgumentNullException(nameof(Request));
                }

                // Ensure we have a normalized ID for the WebHook
                webHook.Id = null;

                // Add WebHook for this user.
                StoreResult result = await _store.InsertWebHookAsync(userId, webHook);

                if (result == StoreResult.Success)
                {
                    return(CreatedAtRoute(WebHookNames.GetWebhookByIdAction, new { id = webHook.Id }, webHook));
                }
                return(CreateHttpResult(result));
            }
            catch (Exception ex)
            {
                string msg = string.Format(CultureInfo.InvariantCulture, _localizationService.GetResource("Api.WebHooks.CouldNotRegisterWebhook"), ex.Message);
                Configuration.DependencyResolver.GetLogger().Error(msg, ex);
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, msg, ex);
                return(ResponseMessage(error));
            }
        }
コード例 #39
0
        /// <param name='id'>
        /// </param>
        /// <param name='webHook'>
        /// </param>
        /// <param name='customHeaders'>
        /// Headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        /// <return>
        /// A response object containing the response body and response headers.
        /// </return>
        public async Task <HttpOperationResponse <object> > PutWithHttpMessagesAsync(string id, WebHook webHook, Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (id == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "id");
            }
            if (webHook == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "webHook");
            }
            if (webHook != null)
            {
                webHook.Validate();
            }
            // Tracing
            bool   _shouldTrace  = ServiceClientTracing.IsEnabled;
            string _invocationId = null;

            if (_shouldTrace)
            {
                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("id", id);
                tracingParameters.Add("webHook", webHook);
                tracingParameters.Add("cancellationToken", cancellationToken);
                ServiceClientTracing.Enter(_invocationId, this, "Put", tracingParameters);
            }
            // Construct URL
            var _baseUrl = this.Client.BaseUri.AbsoluteUri;
            var _url     = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "api/webhooks/registrations/{id}").ToString();

            _url = _url.Replace("{id}", Uri.EscapeDataString(id));
            // Create HTTP transport objects
            HttpRequestMessage  _httpRequest  = new HttpRequestMessage();
            HttpResponseMessage _httpResponse = null;

            _httpRequest.Method     = new HttpMethod("PUT");
            _httpRequest.RequestUri = new Uri(_url);
            // Set Headers
            if (customHeaders != null)
            {
                foreach (var _header in customHeaders)
                {
                    if (_httpRequest.Headers.Contains(_header.Key))
                    {
                        _httpRequest.Headers.Remove(_header.Key);
                    }
                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
                }
            }

            // Serialize Request
            string _requestContent = null;

            if (webHook != null)
            {
                _requestContent      = SafeJsonConvert.SerializeObject(webHook, this.Client.SerializationSettings);
                _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
                _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
            }
            // Send Request
            if (_shouldTrace)
            {
                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

            if (_shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
            }
            HttpStatusCode _statusCode = _httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string _responseContent = null;

            if ((int)_statusCode != 200)
            {
                var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                ex.Request  = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
                if (_shouldTrace)
                {
                    ServiceClientTracing.Error(_invocationId, ex);
                }
                _httpRequest.Dispose();
                if (_httpResponse != null)
                {
                    _httpResponse.Dispose();
                }
                throw ex;
            }
            // Create Result
            var _result = new HttpOperationResponse <object>();

            _result.Request  = _httpRequest;
            _result.Response = _httpResponse;
            // Deserialize Response
            if ((int)_statusCode == 200)
            {
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                try
                {
                    _result.Body = SafeJsonConvert.DeserializeObject <object>(_responseContent, this.Client.DeserializationSettings);
                }
                catch (JsonException ex)
                {
                    _httpRequest.Dispose();
                    if (_httpResponse != null)
                    {
                        _httpResponse.Dispose();
                    }
                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
                }
            }
            if (_shouldTrace)
            {
                ServiceClientTracing.Exit(_invocationId, _result);
            }
            return(_result);
        }
コード例 #40
0
 /// <summary>
 /// Register Web Hook
 /// </summary>
 /// <remarks>
 /// Registers a new web hook.
 /// </remarks>
 public WebHook RegisterWebHook(int businessId, WebHook webHook)
 {
     return(ApiRequest <WebHook, WebHook>($"/business/{businessId}/webhookregistrations", webHook, Method.POST));
 }
コード例 #41
0
 /// <summary>
 /// Determines whether a given <paramref name="action"/> matches the filters for a given <see cref="WebHook"/>.
 /// The action can either match a filter directly or match a wildcard.
 /// </summary>
 /// <param name="webHook">The <see cref="WebHook"/> instance to operate on.</param>
 /// <param name="action">The action to match against the <paramref name="webHook"/> filters.</param>
 /// <returns><c>true</c> if the <paramref name="action"/> matches, otherwise <c>false</c>.</returns>
 public static bool MatchesAction(this WebHook webHook, string action)
 {
     return(webHook != null && (webHook.Filters.Contains(WildcardWebHookFilterProvider.Name) || webHook.Filters.Contains(action)));
 }
コード例 #42
0
ファイル: WebHookManager.cs プロジェクト: niels013/WebHooks
    /// <summary>
    /// Verifies the WebHook by submitting a GET request with a query token intended by the echoed back.
    /// </summary>
    /// <param name="webHook">The <see cref="WebHook"/> to verify.</param>
    protected virtual async Task VerifyEchoAsync(WebHook webHook)
    {
        // Create the echo query parameter that we want returned in response body as plain text.
        var echo = Guid.NewGuid().ToString("N");

        HttpResponseMessage response;

        try
        {
            // If WebHook URI contains a "NoEcho" query parameter then we don't verify the URI using a GET request
            var parameters = webHook.WebHookUri.ParseQueryString();
            if (parameters.TryGetValue(NoEchoParameter, out string value))
            {
                var message = string.Format(CultureInfo.CurrentCulture, CustomResources.Manager_NoEcho);
                _logger.LogInformation(message);
                return;
            }

            // Get request URI with echo query parameter
            var webHookUri = new UriBuilder(webHook.WebHookUri)
            {
                Query = EchoParameter + "=" + echo + "&code=" + webHook.Secret
            };

            // Create request adding any additional request headers (not entity headers) from Web Hook
            var hookRequest = new HttpRequestMessage(HttpMethod.Get, webHookUri.Uri);
            foreach (var kvp in webHook.Headers)
            {
                hookRequest.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
            }

            response = await _httpClient.SendAsync(hookRequest);
        }
        catch (Exception ex)
        {
            var message = string.Format(CultureInfo.CurrentCulture, CustomResources.Manager_VerifyFailure, ex.Message);
            _logger.LogInformation(message, ex);
            throw new InvalidOperationException(message);
        }

        if (!response.IsSuccessStatusCode)
        {
            var message = string.Format(CultureInfo.CurrentCulture, CustomResources.Manager_VerifyFailure, response.StatusCode);
            _logger.LogInformation(message);
            throw new InvalidOperationException(message);
        }

        // Verify response body
        if (response.Content == null)
        {
            var message = CustomResources.Manager_VerifyNoBody;
            _logger.LogInformation(message);
            throw new InvalidOperationException(message);
        }

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

        if (!string.Equals(actualEcho, echo, StringComparison.Ordinal))
        {
            var message = CustomResources.Manager_VerifyBadEcho;
            _logger.LogInformation(message);
            throw new InvalidOperationException(message);
        }
    }
コード例 #43
0
ファイル: SrvCrash.cs プロジェクト: aSunnyWillow/DiscordCraft
 public static async Task Send() => await WebHook.SendHook("Server Crashed!");
コード例 #44
0
 public WebHookTests()
 {
     _webHook = new WebHook();
 }
コード例 #45
0
 protected override async Task VerifyEchoAsync(WebHook webHook) => await Task.FromResult <object>(null);
コード例 #46
0
        public async Task <ApiResponse> Index(string token)
        {
            var headers = HttpContext.Request.Headers.Keys.ToDictionary <string, string, string>(key => key, key => HttpContext.Request.Headers[key]);
            var body    = "";

            var contentType = HttpContext.Request.ContentType.ToLower();

            if (contentType.Contains("application/json"))
            {
                using (var stream = new StreamReader(HttpContext.Request.Body))
                {
                    body = stream.ReadToEnd();
                }
            }
            else if (contentType.Contains("application/x-www-form-urlencoded"))
            {
                var data = HttpContext.Request.Form.ToDictionary <KeyValuePair <string, StringValues>, string, object>(form => form.Key, form =>
                {
                    try
                    {
                        return(JsonConvert.DeserializeObject(form.Value));
                    }
                    catch (Exception e)
                    {
                        return(form.Value.Count > 1 ? (object)form.Value : form.Value.First());
                    }
                });
                body = JsonConvert.SerializeObject(data);
            }

            var result = new ApiResponse();

            try
            {
                var stack = await _stackModel.GetByToken(token);

                if (stack == null)
                {
                    result.Error = "Invalid token";
                }
                else
                {
                    await _logModel.WriteReceivedLog(new ReceivedLog
                    {
                        StackId = stack.Id,
                        Header  = JsonConvert.SerializeObject(headers),
                        Body    = body
                    });

                    var post = JObject.Parse(body);

                    var webHook = new WebHook(post, _httpClientFactory);

                    foreach (var task in stack.Tasks.Where(t => t.Enabled))
                    {
                        var taskContent = JsonConvert.DeserializeObject <TaskContent>(task.Content);
                        var(sentBody, response) = await webHook.Send(taskContent);

                        if (response != null)
                        {
                            await _logModel.WriteSentLog(new SentLog()
                            {
                                TaskId   = task.Id,
                                Url      = taskContent.Url,
                                Content  = sentBody,
                                Response = await response.Content.ReadAsStringAsync()
                            });
                        }
                    }

                    result.Result = "OK";
                }
            }
            catch (Exception ex)
            {
            }

            var message = new
            {
                Token  = token,
                Header = headers,
                Body   = body,
                Result = result.Result
            };
            await _systemLogModel.Write(Define.LogType.WebHook, JsonConvert.SerializeObject(message));

            return(result);
        }
コード例 #47
0
ファイル: Message.cs プロジェクト: aSunnyWillow/DiscordCraft
 public static async Task Send(SocketUserMessage msg) =>
 await WebHook.SendHook(msg.Author.GetAvatarUrl(), msg.Author.Username, msg.Content.Remove(0, 6));
コード例 #48
0
 public int Create(WebHook WebHook)
 {
     return(base.Post <int>(WEBHOOK, WebHook));
 }
コード例 #49
0
 /// <summary>
 /// Update Web Hook Registration
 /// </summary>
 /// <remarks>
 /// Updates the web hook registration with the specified ID.
 /// </remarks>
 public void UpdateWebHookRegistration(int businessId, string id, WebHook webHook)
 {
     ApiRequest($"/business/{businessId}/webhookregistrations/{id}", webHook, Method.PUT);
 }
コード例 #50
0
        public async Task <IActionResult> RegisterWebHook([FromBody] WebHook webHook)
        {
            if (!ModelState.IsValid)
            {
                return(await Error());
            }

            if (webHook == null)
            {
                return(BadRequest());
            }

            var userId = GetUserId();

            try
            {
                await VerifyFilters(webHook);
                await VerifyWebHook(webHook);
            }
            catch (VerificationException ex)
            {
                return(BadRequest(ex.Message));
            }

            var existingWebhooks = await GetAllWebHooks();

            var existingWebhooksForTheSameUri = existingWebhooks.Where(wh => wh.WebHookUri == webHook.WebHookUri);

            foreach (var existingWebHook in existingWebhooksForTheSameUri)
            {
                webHook.Filters.ExceptWith(existingWebHook.Filters);

                if (!webHook.Filters.Any())
                {
                    return(await Error(HttpStatusCode.Conflict, ErrorPropertyKey, "Could not register WebHook because a Webhook with the same URI and Filters is already registered."));
                }
            }

            try
            {
                if (Request == null)
                {
                    throw new ArgumentNullException(nameof(Request));
                }

                webHook.Id = null;

                var result = await _store.InsertWebHookAsync(userId, webHook);

                if (result == StoreResult.Success)
                {
                    return(CreatedAtRoute(WebHookNames.GetWebhookByIdAction, new { id = webHook.Id }, webHook));
                }

                return(await CreateHttpResult(result));
            }
            catch (Exception ex)
            {
                return(await Error(HttpStatusCode.Conflict, ErrorPropertyKey, $"Could not register WebHook due to error: {ex.Message}"));
            }
        }
コード例 #51
0
 /// <summary>
 /// Update Web Hook Registration
 /// </summary>
 /// <remarks>
 /// Updates the web hook registration with the specified ID.
 /// </remarks>
 public Task UpdateWebHookRegistrationAsync(int businessId, string id, WebHook webHook, CancellationToken cancellationToken = default)
 {
     return(ApiRequestAsync($"/business/{businessId}/webhookregistrations/{id}", webHook, Method.PUT, cancellationToken));
 }
コード例 #52
0
 public async Task UpdateAsync(WebHook entity)
 {
     await Client.PutAsJson($"api/webhook/{entity.Id}", entity);
 }
コード例 #53
0
 /// <summary>
 /// Register Web Hook
 /// </summary>
 /// <remarks>
 /// Registers a new web hook.
 /// </remarks>
 public Task <WebHook> RegisterWebHookAsync(int businessId, WebHook webHook, CancellationToken cancellationToken = default)
 {
     return(ApiRequestAsync <WebHook, WebHook>($"/business/{businessId}/webhookregistrations", webHook, Method.POST, cancellationToken));
 }
コード例 #54
0
 public async Task DeleteAsync(WebHook webHook)
 {
     await Client.DeleteAsync($"api/webhook/{webHook.Id}");
 }
コード例 #55
0
 /// <summary>
 /// Determines whether any of the given <paramref name="actions"/> match the filters for a given <see cref="WebHook"/>.
 /// The actions can either match a filter directly or match a wildcard.
 /// </summary>
 /// <param name="webHook">The <see cref="WebHook"/> instance to operate on.</param>
 /// <param name="actions">The set of actions to match against the <paramref name="webHook"/> filters.</param>
 /// <returns><c>true</c> if one or more of the <paramref name="actions"/> match, otherwise <c>false</c>.</returns>
 public static bool MatchesAnyAction(this WebHook webHook, IEnumerable <string> actions)
 {
     return(webHook != null && actions != null &&
            (webHook.Filters.Contains(WildcardWebHookFilterProvider.Name) ||
             actions.FirstOrDefault(f => webHook.Filters.Contains(f)) != null));
 }
コード例 #56
0
        public async Task <IHttpActionResult> Create([FromBody] WebHook webHook)
        {
            await _webHookStore.InsertWebHookAsync(RequestContext.Principal.Identity.Name, webHook);

            return(CreatedAtRoute("RegistrationLookupAction", new { id = webHook.Id }, webHook));
        }
コード例 #57
0
 public new Task VerifyFilters(WebHook webHook)
 {
     return(base.VerifyFilters(webHook));
 }
コード例 #58
0
 public new Task RemovePrivateFilters(string user, WebHook webHook)
 {
     return(base.RemovePrivateFilters(user, webHook));
 }
コード例 #59
0
 private bool Predicate(WebHook arg1, string user)
 {
     return(arr.Contains(user));
 }
コード例 #60
-1
ファイル: WebHook.cs プロジェクト: pusher/pusher-http-dotnet
        public void the_WebHook_will_not_be_valid_when_given_invalid_JSON_for_the_body()
        {
            var secret = "1c9c753dddfd049dd7f1";
            var body = "{Invalid JSON}";
            var expectedSignature = GenerateValidSignature(secret, body);

            var webHook = new WebHook(secret, expectedSignature, body);

            Assert.IsFalse(webHook.IsValid);
            StringAssert.IsMatch("Exception occurred parsing the body as JSON: .*", webHook.ValidationErrors[0]);
        }