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));
            }
        }
Exemple #2
0
        public void GetIdValidator_ReturnsSingleInstance()
        {
            // Act
            IWebHookIdValidator actual1 = CustomApiServices.GetIdValidator();
            IWebHookIdValidator actual2 = CustomApiServices.GetIdValidator();

            // Assert
            Assert.Same(actual1, actual2);
        }
Exemple #3
0
 /// <inheritdoc />
 public WebHookRegistrationsController(IWebHookRegistrationsManager registrationsManager,
                                       IWebHookIdValidator idValidator,
                                       IEnumerable <IWebHookRegistrar> webHookRegistrars,
                                       ILogger <WebHookRegistrationsController> logger)
 {
     _registrationsManager = registrationsManager;
     _idValidator          = idValidator;
     _webHookRegistrars    = webHookRegistrars;
     _logger = logger;
 }
        public void GetIdValidator_ReturnsDefaultInstance_IfNoneRegistered()
        {
            // Arrange
            _config.InitializeCustomWebHooks();

            // Act
            IWebHookIdValidator actual = _resolverMock.Object.GetIdValidator();

            // Assert
            Assert.IsType <DefaultWebHookIdValidator>(actual);
        }
Exemple #5
0
        public void SetIdValidator_GetIdValidator_Roundtrips()
        {
            // Arrange
            Mock <IWebHookIdValidator> idValidatorMock = new Mock <IWebHookIdValidator>();

            // Act
            CustomApiServices.SetIdValidator(idValidatorMock.Object);
            IWebHookIdValidator actual = CustomApiServices.GetIdValidator();

            // Assert
            Assert.Same(idValidatorMock.Object, actual);
        }
        public void GetIdValidator_ReturnsSameInstance_IfNoneRegistered()
        {
            // Arrange
            _config.InitializeCustomWebHooks();

            // Act
            IWebHookIdValidator actual1 = _resolverMock.Object.GetIdValidator();
            IWebHookIdValidator actual2 = _resolverMock.Object.GetIdValidator();

            // Assert
            Assert.Same(actual1, actual2);
        }
Exemple #7
0
        public async Task <IHttpActionResult> Post(WebHook webHook)
        {
            if (webHook == null)
            {
                return(BadRequest());
            }

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

                // Validate other parts of WebHook
                await _registrationsManager.VerifySecretAsync(webHook);

                await _registrationsManager.VerifyFiltersAsync(webHook);

                await _registrationsManager.VerifyAddressAsync(webHook);
            }
            catch (Exception ex)
            {
                string msg = string.Format(CultureInfo.CurrentCulture, BaladorResource.RegistrationController_RegistrationFailure, ex.Message);
                Configuration.DependencyResolver.GetLogger().Info(msg);
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.BadRequest, msg, ex);
                return(ResponseMessage(error));
            }

            try
            {
                // Add WebHook for this user.
                StoreResult result = await _registrationsManager.AddWebHookAsync(User, webHook, AddPrivateFilters);

                if (result == StoreResult.Success)
                {
                    return(CreatedAtRoute(RegistrationLookupAction, new { id = webHook.Id }, webHook));
                }
                return(CreateHttpResult(result));
            }
            catch (Exception ex)
            {
                string msg = string.Format(CultureInfo.CurrentCulture, BaladorResource.RegistrationController_RegistrationFailure, ex.Message);
                Configuration.DependencyResolver.GetLogger().Error(msg, ex);
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, msg, ex);
                return(ResponseMessage(error));
            }
        }
        public void GetIdValidator_ReturnsDependencyInstance_IfRegistered()
        {
            // Arrange
            Mock <IWebHookIdValidator> instanceMock = new Mock <IWebHookIdValidator>();

            _resolverMock.Setup(r => r.GetService(typeof(IWebHookIdValidator)))
            .Returns(instanceMock.Object)
            .Verifiable();

            // Act
            IWebHookIdValidator actual = _resolverMock.Object.GetIdValidator();

            // Assert
            Assert.Same(instanceMock.Object, actual);
            instanceMock.Verify();
        }
Exemple #9
0
        public async Task <IActionResult> Post([FromBody] WebHook webHook)
        {
            if (webHook == null)
            {
                throw new ArgumentNullException("webHook");
            }

            string userId = User.Identity.Name;

            await VerifyFilters(webHook);

            try
            {
                // Validate the WebHook is Active (using echo)
                await _manager.VerifyWebHookAsync(webHook);

                // Validate the provided WebHook ID (or force one to be created on server side)
                IWebHookIdValidator idValidator = (IWebHookIdValidator)HttpContext.RequestServices.GetService(typeof(IWebHookIdValidator));
                if (idValidator == null)
                {
                    idValidator = new DefaultWebHookIdValidator();
                }
                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(CreateResultFromStoreResult(result));
            }
            catch (Exception ex)
            {
                string msg = string.Format(CustomApiResource.RegistrationController_RegistrationFailure, ex.Message);
                _logger.LogError(msg);
                return(StatusCode(StatusCodes.Status500InternalServerError, msg));
            }
        }
Exemple #10
0
        public async Task <IActionResult> Put(string id, [FromBody] WebHook webHook)
        {
            if (webHook == null)
            {
                return(BadRequest());
            }
            if (!string.Equals(id, webHook.Id, StringComparison.OrdinalIgnoreCase))
            {
                return(BadRequest());
            }

            string userId = User.Identity.Name;

            await VerifyFilters(webHook);

            try
            {
                // Validate the WebHook is Active (using echo)
                await _manager.VerifyWebHookAsync(webHook);

                // Validate the provided WebHook ID (or force one to be created on server side)
                IWebHookIdValidator idValidator = (IWebHookIdValidator)HttpContext.RequestServices.GetService(typeof(IWebHookIdValidator));
                if (idValidator == null)
                {
                    idValidator = new DefaultWebHookIdValidator();
                }
                await idValidator.ValidateIdAsync(Request, webHook);

                StoreResult result = await _store.UpdateWebHookAsync(userId, webHook);

                return(CreateResultFromStoreResult(result));
            }
            catch (Exception ex)
            {
                string msg = string.Format(CustomApiResource.RegistrationController_UpdateFailure, ex.Message);
                _logger.LogError(msg);
                return(StatusCode(StatusCodes.Status500InternalServerError, msg));
            }
        }
Exemple #11
0
 public DefaultWebHookIdValidatorTests()
 {
     _request   = new HttpRequestMessage();
     _validator = new DefaultWebHookIdValidator();
 }
Exemple #12
0
 /// <summary>
 /// For testing purposes
 /// </summary>
 internal static void Reset()
 {
     _registrars = null;
     _idValidator = null;
 }
Exemple #13
0
 /// <summary>
 /// Sets a default <see cref="IWebHookIdValidator"/> implementation which is used if none are registered with the 
 /// Dependency Injection engine.
 /// </summary>
 /// <param name="instance">The <see cref="IWebHookIdValidator"/> to use. If <c>null</c> then a default implementation is used.</param>
 public static void SetIdValidator(IWebHookIdValidator instance)
 {
     _idValidator = instance;
 }
 /// <summary>
 /// For testing purposes
 /// </summary>
 internal static void Reset()
 {
     _registrars  = null;
     _idValidator = null;
 }
 public DefaultWebHookIdValidatorTests()
 {
     _request = new HttpRequestMessage();
     _validator = new DefaultWebHookIdValidator();
 }
Exemple #16
0
        /// <summary>
        /// Gets an <see cref="IWebHookIdValidator"/> implementation registered with the Dependency Injection engine
        /// or a default implementation if none are registered.
        /// </summary>
        /// <param name="services">The <see cref="IDependencyScope"/> implementation.</param>
        /// <returns>The registered <see cref="IWebHookIdValidator"/> instance or a default implementation if none are registered.</returns>
        public static IWebHookIdValidator GetIdValidator(this IDependencyScope services)
        {
            IWebHookIdValidator validator = services.GetService <IWebHookIdValidator>();

            return(validator ?? CustomApiServices.GetIdValidator());
        }
 /// <summary>
 /// Sets a default <see cref="IWebHookIdValidator"/> implementation which is used if none are registered with the
 /// Dependency Injection engine.
 /// </summary>
 /// <param name="instance">The <see cref="IWebHookIdValidator"/> to use. If <c>null</c> then a default implementation is used.</param>
 public static void SetIdValidator(IWebHookIdValidator instance)
 {
     _idValidator = instance;
 }