Ejemplo n.º 1
0
        public override async Task Invoke(IOwinContext context)
        {
            var openIdConnectRequest = await context.ToOpenIdConnectRequest();

            var isExcludedFromRecaptcha = _options.IsExcluded(openIdConnectRequest);

            var loginStatistics  = context.Get <ILoginStatistics>();
            var recaptchaContext = context.Get <IRecaptchaContext>();

            if (recaptchaContext != null)
            {
                _logger.ExtendedInfo("Recaptcha completed", new { username = openIdConnectRequest.GetUsername(), ipAddress = openIdConnectRequest.GetRemoteIpAddress(), userAgent = openIdConnectRequest.GetUserAgent(), RecaptchaState = recaptchaContext.State, RecaptchaHostname = recaptchaContext.Hostname });

                var recaptchaMonitor = context.Get <IRecaptchaMonitor>();

                recaptchaMonitor?.ChallengeCompleted(openIdConnectRequest.ToRecaptchaUserContext(), recaptchaContext.ToRecaptchaResponseContext());

                switch (recaptchaContext.State)
                {
                case RecaptchaState.Failed:
                {
                    await Challenge(context, openIdConnectRequest, loginStatistics);

                    return;
                }

                case RecaptchaState.ChallengeSucceeded:
                    await context.CleanupAcrValues();

                    await Next.Invoke(context);

                    return;
                }
            }

            if (_options.Matches(openIdConnectRequest) && !isExcludedFromRecaptcha)
            {
                var result = await DoInvoke(context, openIdConnectRequest, loginStatistics);

                switch (result)
                {
                case PipelineState.Challenge:
                {
                    var numberOfFailedLogins = await loginStatistics.GetNumberOfFailedLoginsForIpAddress(openIdConnectRequest.GetRemoteIpAddress());

                    await ChallengeWithRequestForRecaptcha(context, openIdConnectRequest, numberOfFailedLogins);

                    _logger.ExtendedInfo("Issuing Recaptcha Challenge", new { username = openIdConnectRequest.GetUsername(), ipAddress = openIdConnectRequest.GetRemoteIpAddress(), userAgent = openIdConnectRequest.GetUserAgent(), RecaptchaState = recaptchaContext?.State, numberOfFailedLogins });

                    return;
                }

                case PipelineState.Continue:
                    break;
                }
            }

            await Next.Invoke(context);
        }
        public override async Task Invoke(IOwinContext context)
        {
            var openIdConnectRequest = await context.ToOpenIdConnectRequest();

            var isExcludedFromRecaptcha = Options.IsExcluded(openIdConnectRequest);

            var loginStatistics  = context.Get <ILoginStatistics>();
            var recaptchaContext = context.Get <IRecaptchaContext>();
            var recaptchaTracker = context.Get <IRecaptchaTracker>();

            if (recaptchaContext != null)
            {
                if (!recaptchaTracker.IsCompleted)
                {
                    var recaptchaMonitor = context.Get <IRecaptchaMonitor>();

                    if (recaptchaMonitor != null)
                    {
                        await recaptchaMonitor.ChallengeCompleted(openIdConnectRequest.ToRecaptchaUserContext(), recaptchaContext.ToRecaptchaResponseContext());
                    }

                    recaptchaTracker.IsCompleted = true;
                }

                switch (recaptchaContext.State)
                {
                case RecaptchaState.Failed:
                {
                    await Challenge(context, openIdConnectRequest, loginStatistics);

                    return;
                }

                case RecaptchaState.ChallengeSucceeded:
                    await context.CleanupAcrValues();

                    await Next.Invoke(context);

                    return;
                }
            }

            if (Options.Matches(openIdConnectRequest) && !isExcludedFromRecaptcha)
            {
                var result = await DoInvoke(context, openIdConnectRequest, loginStatistics);

                switch (result)
                {
                case PipelineState.Challenge:
                {
                    var numberOfFailedLogins = await loginStatistics.GetNumberOfFailedLoginsForIpAddress(openIdConnectRequest.GetRemoteIpAddress());

                    await ChallengeWithRequestForRecaptcha(context, openIdConnectRequest, numberOfFailedLogins);

                    return;
                }

                case PipelineState.Continue:
                    break;
                }
            }

            await Next.Invoke(context);
        }