예제 #1
0
        /// <inheritdoc />
        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var routeData          = context.RouteData;
            var verifyCodeMetadata = _verifyCodeMetadata;

            if (verifyCodeMetadata == null)
            {
                if (!routeData.TryGetWebHookReceiverName(out var requestReceiverName))
                {
                    return;
                }

                verifyCodeMetadata = _metadataProvider.GetVerifyCodeMetadata(requestReceiverName);
                if (verifyCodeMetadata == null)
                {
                    return;
                }
            }

            var result = EnsureValidCode(context.HttpContext.Request, routeData, verifyCodeMetadata.ReceiverName);

            if (result != null)
            {
                context.Result = result;
            }
        }
예제 #2
0
    /// <summary>
    /// <para>
    /// Confirms the <see cref="Routing.WebHookReceiverNameConstraint"/> is configured and ran successfully for
    /// this request. Also confirms at least one <see cref="IWebHookReceiver"/> filter is configured to handle this
    /// request.
    /// </para>
    /// <para>
    /// Logs an informational message when both confirmations succeed. If either confirmation fails, sets
    /// <see cref="ResourceExecutingContext.Result"/> to a <see cref="StatusCodeResult"/> with
    /// <see cref="StatusCodeResult.StatusCode"/> set to <see cref="StatusCodes.Status500InternalServerError"/>.
    /// </para>
    /// </summary>
    /// <param name="context">The <see cref="ResourceExecutingContext"/>.</param>
    public void OnResourceExecuting(ResourceExecutingContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (context.RouteData.TryGetWebHookReceiverName(out var receiverName))
        {
            if (!context.RouteData.GetWebHookReceiverExists())
            {
                _logger.LogError(
                    0,
                    "Unable to find WebHook routing constraints for the '{ReceiverName}' receiver. Add the " +
                    $"required configuration by calling a receiver-specific method that calls " +
                    $"'{typeof(IMvcBuilder)}.{nameof(WebHookMvcBuilderExtensions.AddWebHooks)}' or " +
                    $"'{nameof(IMvcCoreBuilder)}.{nameof(WebHookMvcCoreBuilderExtensions.AddWebHooks)}' in the " +
                    $"application startup code. For example, call '{nameof(IMvcCoreBuilder)}.AddGitHubWebHooks' " +
                    "to configure a minimal GitHub receiver.",
                    receiverName);

                context.Result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
                return;
            }

            // Check for receiver-specific filters only for receivers that do _not_ use code verification.
            if (_metadataProvider.GetVerifyCodeMetadata(receiverName) == null)
            {
                var found = false;
                for (var i = 0; i < context.Filters.Count; i++)
                {
                    var filter = context.Filters[i];
                    if (filter is IWebHookReceiver receiver && receiver.IsApplicable(receiverName))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    // This case is actually more likely a gap in the receiver-specific configuration method.
                    _logger.LogError(
                        1,
                        "Unable to find WebHook filters for the '{ReceiverName}' receiver. Add the required " +
                        "configuration by calling a receiver-specific method that calls " +
                        $"'{typeof(IMvcBuilder)}.{nameof(WebHookMvcBuilderExtensions.AddWebHooks)}' or " +
                        $"'{nameof(IMvcCoreBuilder)}.{nameof(WebHookMvcCoreBuilderExtensions.AddWebHooks)}' in " +
                        "the application startup code. For example, call " +
                        $"'{nameof(IMvcCoreBuilder)}.AddGitHubWebHooks' to configure a minimal GitHub receiver.",
                        receiverName);

                    context.Result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
                    return;
                }
            }
        }
        else
        {
            // Routing not configured at all (wrong template) but the request reached this action.
            _logger.LogError(
                2,
                "Unable to find WebHook routing information in the request. Add the required " +
                "configuration by calling a receiver-specific method that calls " +
                $"'{typeof(IMvcBuilder)}.{nameof(WebHookMvcBuilderExtensions.AddWebHooks)}' or " +
                $"'{nameof(IMvcCoreBuilder)}.{nameof(WebHookMvcCoreBuilderExtensions.AddWebHooks)}' in the " +
                $"application startup code. For example, call '{nameof(IMvcCoreBuilder)}.AddGitHubWebHooks' to " +
                "configure a minimal GitHub receiver.");

            context.Result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
            return;
        }

        context.RouteData.TryGetWebHookReceiverId(out var id);
        _logger.LogInformation(
            3,
            "Processing incoming WebHook request with receiver '{ReceiverName}' and id '{Id}'.",
            receiverName,
            id);
    }