/// <summary>
        /// Validates the current request.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public async Task <ClientSecretValidationResult> ValidateAsync(HttpContext context)
        {
            _logger.LogDebug("Start client validation");

            var fail = new ClientSecretValidationResult
            {
                IsError = true
            };

            var parsedSecret = await _parser.ParseAsync(context);

            if (parsedSecret == null)
            {
                await RaiseFailureEvent("unknown", "No client id found");

                _logger.LogError("No client identifier found");
                return(fail);
            }

            // load client
            var client = await _clients.FindEnabledClientByIdAsync(parsedSecret.Id);

            if (client == null)
            {
                await RaiseFailureEvent(parsedSecret.Id, "Unknown client");

                _logger.LogError("No client with id '{clientId}' found. aborting", parsedSecret.Id);
                return(fail);
            }

            if (!client.RequireClientSecret || client.IsImplicitOnly())
            {
                _logger.LogDebug("Public Client - skipping secret validation success");
            }
            else
            {
                var result = await _validator.ValidateAsync(parsedSecret, client.ClientSecrets);

                if (result.Success == false)
                {
                    await RaiseFailureEvent(client.ClientId, "Invalid client secret");

                    _logger.LogError("Client secret validation failed for client: {clientId}.", client.ClientId);

                    return(fail);
                }
            }

            _logger.LogDebug("Client validation success");

            var success = new ClientSecretValidationResult
            {
                IsError = false,
                Client  = client
            };

            await RaiseSuccessEvent(client.ClientId, parsedSecret.Type);

            return(success);
        }
        public async Task <ClientSecretValidationResult> ValidateAsync(HttpContext context)
        {
            _logger.LogDebug("Start client validation");

            var fail = new ClientSecretValidationResult
            {
                IsError = true
            };

            var parsedSecret = await _parser.ParseAsync(context);

            if (parsedSecret == null)
            {
                await RaiseFailureEvent("unknown", "No client id or secret found");

                _logger.LogError("No client secret found");
                return(fail);
            }

            // load client
            var client = await _clients.FindClientByIdAsync(parsedSecret.Id);

            if (client == null)
            {
                await RaiseFailureEvent(parsedSecret.Id, "Unknown client");

                _logger.LogError("No client with id '{clientId}' found. aborting", parsedSecret.Id);
                return(fail);
            }

            var result = await _validator.ValidateAsync(parsedSecret, client.ClientSecrets);

            if (result.Success)
            {
                _logger.LogInformation("Client validation success");

                var success = new ClientSecretValidationResult
                {
                    IsError = false,
                    Client  = client
                };

                await RaiseSuccessEvent(client.ClientId);

                return(success);
            }

            await RaiseFailureEvent(client.ClientId, "Invalid client secret");

            _logger.LogError("Client validation failed for client: {clientId}.", client.ClientId);

            return(fail);
        }
        public async Task <ScopeSecretValidationResult> ValidateAsync(HttpContext context)
        {
            _logger.LogTrace("Start scope validation");

            var fail = new ScopeSecretValidationResult
            {
                IsError = true
            };

            var parsedSecret = await _parser.ParseAsync(context);

            if (parsedSecret == null)
            {
                await RaiseFailureEvent("unknown", "No scope id or secret found");

                _logger.LogError("No scope secret found");
                return(fail);
            }

            // load scope
            var scope = (await _scopes.FindEnabledScopesAsync(new[] { parsedSecret.Id })).FirstOrDefault();

            if (scope == null)
            {
                await RaiseFailureEvent(parsedSecret.Id, "Unknown scope");

                _logger.LogError("No scope with that name found. aborting");
                return(fail);
            }

            var result = await _validator.ValidateAsync(parsedSecret, scope.ScopeSecrets);

            if (result.Success)
            {
                _logger.LogDebug("Scope validation success");

                var success = new ScopeSecretValidationResult
                {
                    IsError = false,
                    Scope   = scope
                };

                await RaiseSuccessEvent(scope.Name);

                return(success);
            }

            await RaiseFailureEvent(scope.Name, "Invalid client secret");

            _logger.LogError("Scope validation failed.");

            return(fail);
        }
        /// <summary>
        /// Validates the secret on the current request.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public async Task <ApiSecretValidationResult> ValidateAsync(HttpContext context)
        {
            _logger.LogTrace("Start API validation");

            var fail = new ApiSecretValidationResult
            {
                IsError = true
            };

            var parsedSecret = await _parser.ParseAsync(context);

            if (parsedSecret == null)
            {
                await RaiseFailureEventAsync("unknown", "No API id or secret found");

                _logger.LogError("No API secret found");
                return(fail);
            }

            // load API resource
            var api = await _resources.FindApiResourceAsync(parsedSecret.Id);

            if (api == null)
            {
                await RaiseFailureEventAsync(parsedSecret.Id, "Unknown API resource");

                _logger.LogError("No API resource with that name found. aborting");
                return(fail);
            }

            if (api.Enabled == false)
            {
                await RaiseFailureEventAsync(parsedSecret.Id, "API resource not enabled");

                _logger.LogError("API resource not enabled. aborting.");
                return(fail);
            }

            var result = await _validator.ValidateAsync(parsedSecret, api.ApiSecrets);

            if (result.Success)
            {
                _logger.LogDebug("API resource validation success");

                var success = new ApiSecretValidationResult
                {
                    IsError  = false,
                    Resource = api
                };

                await RaiseSuccessEventAsync(api.Name, parsedSecret.Type);

                return(success);
            }

            await RaiseFailureEventAsync(api.Name, "Invalid API secret");

            _logger.LogError("API validation failed.");

            return(fail);
        }