public void AddService_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.AddService(typeof(NonFilter)); });

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 4
0
 public static void AddValidateModelFilter(this FilterCollection filters)
 {
     filters.Add(new ValidateModelFilter());
 }
Exemplo n.º 5
0
 public static void AddValidateModelFilter(this FilterCollection filters, int errorCode)
 {
     filters.Add(new ValidateModelFilter {
         ErrorCode = errorCode
     });
 }
Exemplo n.º 6
0
        /// <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>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="filters"/> is <c>null</c>.</exception>
        public static FilterCollection AddJwtTokenAuthorization(this FilterCollection filters)
        {
            Guard.NotNull(filters, nameof(filters), "Requires a filter collection to add the JWT token authorization filter");

            return(filters.AddJwtTokenAuthorization(configureOptions: null));
        }
Exemplo n.º 7
0
 public static void AddApiExceptionFilter(this FilterCollection filters, params Action <Exception>[] exceptionListeners)
 {
     filters.Add(new ApiExceptionFilter(exceptionListeners));
 }
        /// <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>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="filters"/> is <c>null</c>.</exception>
        public static FilterCollection AddCertificateAuthentication(this FilterCollection filters)
        {
            Guard.NotNull(filters, nameof(filters), "Requires a set of MVC filters to add the certificate authentication MVC filter");

            return(AddCertificateAuthentication(filters, configureOptions: null));
        }
        /// <summary>
        /// Adds JWT token authorization
        /// </summary>
        /// <param name="filters">All filters that are being applied to the request pipeline</param>
        public static FilterCollection AddJwtTokenAuthorization(this FilterCollection filters)
        {
            Guard.NotNull(filters, nameof(filters));

            return(filters.AddJwtTokenAuthorization(options => { }));
        }