public void GenericAdd_WithOrder_SetsOrder() { // Arrange var collection = new FilterCollection(); // Act var added = collection.Add <MyFilter>(17); // Assert Assert.Equal(17, Assert.IsAssignableFrom <IOrderedFilter>(added).Order); }
public void Add_ThrowsOnNonIFilter() { // Arrange var collection = new FilterCollection(); var expectedMessage = $"The type '{typeof(NonFilter).FullName}' must derive from " + $"'{typeof(IFilterMetadata).FullName}'."; // Act & Assert ExceptionAssert.ThrowsArgument( () => collection.Add(typeof(NonFilter)), "filterType", expectedMessage); }
/// <summary> /// Adds JWT token authorization to the MVC <see cref="FilterCollection"/>. /// </summary> /// <param name="filters">All filters that are being applied to the request pipeline</param> /// <param name="configureOptions">Configuration options for using JWT token authorization</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="filters"/> is <c>null</c>.</exception> public static FilterCollection AddJwtTokenAuthorization( this FilterCollection filters, Action <JwtTokenAuthorizationOptions> configureOptions) { Guard.NotNull(filters, nameof(filters), "Requires a filter collection to add the JWT token authorization filter"); var options = new JwtTokenAuthorizationOptions(); configureOptions?.Invoke(options); filters.Add(new JwtTokenAuthorizationFilter(options)); return(filters); }
/// <summary> /// Adds an certificate authentication MVC filter to the given <paramref name="filters"/> that authenticates the incoming HTTP request. /// </summary> /// <param name="filters">The current MVC filters of the application.</param> /// <param name="configureOptions"> /// The optional function to configure the set of additional consumer-configurable options to change the behavior of the certificate authentication. /// </param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="filters"/> is <c>null</c>.</exception> public static FilterCollection AddCertificateAuthentication( this FilterCollection filters, Action <CertificateAuthenticationOptions> configureOptions) { Guard.NotNull(filters, nameof(filters), "Requires a set of MVC filters to add the certificate authentication MVC filter"); var options = new CertificateAuthenticationOptions(); configureOptions?.Invoke(options); filters.Add(new CertificateAuthenticationFilter(options)); return(filters); }
public void GenericAdd_UsesTypeFilterAttribute() { // Arrange var collection = new FilterCollection(); // Act var added = collection.Add <MyFilter>(); // Assert var typeFilter = Assert.IsType <TypeFilterAttribute>(added); Assert.Equal(typeof(MyFilter), typeFilter.ImplementationType); Assert.Same(typeFilter, Assert.Single(collection)); }
public void Add_ThrowsOnNonIFilter() { // Arrange var collection = new FilterCollection(); var expectedMessage = $"The type '{typeof(NonFilter).FullName}' must derive from " + $"'{typeof(IFilterMetadata).FullName}'." + Environment.NewLine + "Parameter name: filterType"; // Act & Assert var ex = Assert.Throws <ArgumentException>(() => { collection.Add(typeof(NonFilter)); }); // Assert Assert.Equal(expectedMessage, ex.Message); }
/// <summary> /// Adds JWT token authorization /// </summary> /// <param name="filters">All filters that are being applied to the request pipeline</param> /// <param name="configureOptions">Configuration options for using JWT token authorization</param> /// <param name="claimCheck">Custom claims key-value pair to validate against</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="claimCheck"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="claimCheck"/> doesn't have any entries or one of the entries has blank key/value inputs.</exception> public static FilterCollection AddJwtTokenAuthorization( this FilterCollection filters, Action <JwtTokenAuthorizationOptions> configureOptions, IDictionary <string, string> claimCheck) { Guard.NotNull(filters, nameof(filters), "Requires a filter collection to add the JWT token authorization filter"); Guard.NotNull(claimCheck, nameof(claimCheck), "Requires a set of claim checks to verify the claims request JWT"); Guard.NotAny(claimCheck, nameof(claimCheck), "Requires at least one entry in the set of claim checks to verify the claims in the request JWT"); Guard.For <ArgumentException>(() => claimCheck.Any(item => String.IsNullOrWhiteSpace(item.Key) || String.IsNullOrWhiteSpace(item.Value)), "Requires all entries in the set of claim checks to be non-blank to correctly verify the claims in the request JWT"); var options = new JwtTokenAuthorizationOptions(claimCheck); configureOptions?.Invoke(options); filters.Add(new JwtTokenAuthorizationFilter(options)); return(filters); }
/// <summary> /// Adds an shared access key authentication MVC filter to the given <paramref name="filters"/> that authenticates the incoming HTTP request on its query. /// </summary> /// <param name="filters">The current MVC filters of the application.</param> /// <param name="parameterName">The name of the request query parameter name which value must match the stored secret.</param> /// <param name="secretName">The name of the secret that's being retrieved using the <see cref="ISecretProvider.GetRawSecretAsync"/> call.</param> /// <param name="configureOptions"> /// The optional function to configure the set of additional consumer-configurable options to change the behavior of the shared access authentication. /// </param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="filters"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="parameterName"/> or <paramref name="secretName"/> is blank.</exception> public static FilterCollection AddSharedAccessAuthenticationOnQuery( this FilterCollection filters, string parameterName, string secretName, Action <SharedAccessKeyAuthenticationOptions> configureOptions) { Guard.NotNull(filters, nameof(filters), "Requires a set of MVC filters to add the shared access authentication MVC filter"); Guard.NotNullOrWhitespace(parameterName, nameof(parameterName), "Requires a non-blank HTTP request query parameter name name to match the stored secret during the shared access key authentication"); Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve the stored access key in the secret store during the shared access key authentication"); var options = new SharedAccessKeyAuthenticationOptions(); configureOptions?.Invoke(options); filters.Add(new SharedAccessKeyAuthenticationFilter(headerName: null, queryParameterName: parameterName, secretName, options)); return(filters); }
public static void AddValidateModelFilter(this FilterCollection filters) { filters.Add(new ValidateModelFilter()); }
public static void AddValidateModelFilter(this FilterCollection filters, int errorCode) { filters.Add(new ValidateModelFilter { ErrorCode = errorCode }); }
public static void AddApiExceptionFilter(this FilterCollection filters, params Action <Exception>[] exceptionListeners) { filters.Add(new ApiExceptionFilter(exceptionListeners)); }