Exemple #1
0
        public async Task <ValidationResult> ValidateClientAsync()
        {
            Logger.Info("Start client validation");

            if (_validatedRequest.ClientId.IsMissing())
            {
                throw new InvalidOperationException("ClientId is empty. Validate protocol first.");
            }

            //////////////////////////////////////////////////////////
            // check for valid client
            //////////////////////////////////////////////////////////
            var client = await _clients.FindClientByIdAsync(_validatedRequest.ClientId);

            if (client == null || client.Enabled == false)
            {
                Logger.ErrorFormat("Unknown client or not enabled: {0}", _validatedRequest.ClientId);
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            Logger.InfoFormat("Client found in registry: {0} / {1}", client.ClientId, client.ClientName);
            _validatedRequest.Client = client;

            //////////////////////////////////////////////////////////
            // check if redirect_uri is valid
            //////////////////////////////////////////////////////////
            if (!_validatedRequest.Client.RedirectUris.Contains(_validatedRequest.RedirectUri))
            {
                Logger.ErrorFormat("Invalid redirect_uri: {0}", _validatedRequest.RedirectUri);
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            //////////////////////////////////////////////////////////
            // check if flow is allowed for client
            //////////////////////////////////////////////////////////
            if (_validatedRequest.Flow != _validatedRequest.Client.Flow)
            {
                Logger.ErrorFormat("Invalid flow for client: {0}", _validatedRequest.Flow);
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            var scopeValidator = new ScopeValidator();

            //////////////////////////////////////////////////////////
            // check if scopes are valid/supported and check for resource scopes
            //////////////////////////////////////////////////////////
            if (!scopeValidator.AreScopesValid(_validatedRequest.RequestedScopes, await _scopes.GetScopesAsync()))
            {
                return(Invalid(ErrorTypes.Client, Constants.AuthorizeErrors.InvalidScope));
            }

            if (scopeValidator.ContainsOpenIdScopes && !_validatedRequest.IsOpenIdRequest)
            {
                Logger.Error("Identity related scope requests, but no openid scope");
                return(Invalid(ErrorTypes.Client, Constants.AuthorizeErrors.InvalidScope));
            }

            if (scopeValidator.ContainsResourceScopes)
            {
                _validatedRequest.IsResourceRequest = true;
            }

            //////////////////////////////////////////////////////////
            // check scopes and scope restrictions
            //////////////////////////////////////////////////////////
            if (!scopeValidator.AreScopesAllowed(_validatedRequest.Client, _validatedRequest.RequestedScopes))
            {
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            _validatedRequest.ValidatedScopes = scopeValidator;

            //////////////////////////////////////////////////////////
            // check id vs resource scopes and response types plausability
            //////////////////////////////////////////////////////////
            if (!scopeValidator.IsResponseTypeValid(_validatedRequest.ResponseType))
            {
                return(Invalid(ErrorTypes.Client, Constants.AuthorizeErrors.InvalidScope));
            }

            var customResult = await _customValidator.ValidateAuthorizeRequestAsync(_validatedRequest);

            if (customResult.IsError)
            {
                Logger.Error("Error in custom validation: " + customResult.Error);
            }

            Logger.Info("Client validation successful");
            return(customResult);
        }
        public async Task <AuthorizeRequestValidationResult> ValidateAsync(NameValueCollection parameters, ClaimsPrincipal subject = null)
        {
            Logger.Info("Start authorize request protocol validation");

            var request = new ValidatedAuthorizeRequest
            {
                Options = _options,
                Subject = subject ?? Principal.Anonymous
            };

            if (parameters == null)
            {
                Logger.Error("Parameters are null.");
                throw new ArgumentNullException("parameters");
            }

            request.Raw = parameters;

            // validate client_id and redirect_uri
            var clientResult = await ValidateClientAsync(request);

            if (clientResult.IsError)
            {
                return(clientResult);
            }

            // state, response_type, response_mode
            var mandatoryResult = ValidateCoreParameters(request);

            if (mandatoryResult.IsError)
            {
                return(mandatoryResult);
            }

            // scope, scope restrictions and plausability
            var scopeResult = await ValidateScopeAsync(request);

            if (scopeResult.IsError)
            {
                return(scopeResult);
            }

            // nonce, prompt, acr_values, login_hint etc.
            var optionalResult = ValidateOptionalParameters(request);

            if (optionalResult.IsError)
            {
                return(optionalResult);
            }

            // custom validator
            var customResult = await _customValidator.ValidateAuthorizeRequestAsync(request);

            if (customResult.IsError)
            {
                LogError("Error in custom validation: " + customResult.Error, request);
                return(Invalid(request, customResult.ErrorType, customResult.Error));
            }

            LogSuccess(request);
            return(Valid(request));
        }
        public async Task <ValidationResult> ValidateClientAsync()
        {
            Logger.Info("Start authorize request client validation");

            if (_validatedRequest.ClientId.IsMissing())
            {
                throw new InvalidOperationException("ClientId is empty. Validate protocol first.");
            }

            //////////////////////////////////////////////////////////
            // check for valid client
            //////////////////////////////////////////////////////////
            var client = await _clients.FindClientByIdAsync(_validatedRequest.ClientId);

            if (client == null || client.Enabled == false)
            {
                LogError("Unknown client or not enabled: " + _validatedRequest.ClientId);
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            _validatedRequest.Client = client;

            //////////////////////////////////////////////////////////
            // check if redirect_uri is valid
            //////////////////////////////////////////////////////////
            if (await _uriValidator.IsRedirectUriValidAsync(_validatedRequest.RedirectUri, _validatedRequest.Client) == false)
            {
                LogError("Invalid redirect_uri: " + _validatedRequest.RedirectUri);
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            //////////////////////////////////////////////////////////
            // check if flow is allowed for client
            //////////////////////////////////////////////////////////
            if (_validatedRequest.Flow != _validatedRequest.Client.Flow)
            {
                LogError("Invalid flow for client: " + _validatedRequest.Flow);
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            //////////////////////////////////////////////////////////
            // check if scopes are valid/supported and check for resource scopes
            //////////////////////////////////////////////////////////
            if (await _scopeValidator.AreScopesValidAsync(_validatedRequest.RequestedScopes) == false)
            {
                return(Invalid(ErrorTypes.Client, Constants.AuthorizeErrors.InvalidScope));
            }

            if (_scopeValidator.ContainsOpenIdScopes && !_validatedRequest.IsOpenIdRequest)
            {
                LogError("Identity related scope requests, but no openid scope");
                return(Invalid(ErrorTypes.Client, Constants.AuthorizeErrors.InvalidScope));
            }

            if (_scopeValidator.ContainsResourceScopes)
            {
                _validatedRequest.IsResourceRequest = true;
            }

            //////////////////////////////////////////////////////////
            // check scopes and scope restrictions
            //////////////////////////////////////////////////////////
            if (!_scopeValidator.AreScopesAllowed(_validatedRequest.Client, _validatedRequest.RequestedScopes))
            {
                return(Invalid(ErrorTypes.User, Constants.AuthorizeErrors.UnauthorizedClient));
            }

            _validatedRequest.ValidatedScopes = _scopeValidator;

            //////////////////////////////////////////////////////////
            // check id vs resource scopes and response types plausability
            //////////////////////////////////////////////////////////
            if (!_scopeValidator.IsResponseTypeValid(_validatedRequest.ResponseType))
            {
                return(Invalid(ErrorTypes.Client, Constants.AuthorizeErrors.InvalidScope));
            }

            //////////////////////////////////////////////////////////
            // check if sessionId is available and if session management is enabled
            //////////////////////////////////////////////////////////
            if (_options.Endpoints.EnableCheckSessionEndpoint)
            {
                if (_validatedRequest.SessionId.IsMissing())
                {
                    Logger.Warn("Session management is enabled, but session id cookie is missing.");
                }
            }

            var customResult = await _customValidator.ValidateAuthorizeRequestAsync(_validatedRequest);

            if (customResult.IsError)
            {
                LogError("Error in custom validation: " + customResult.Error);
            }
            else
            {
                LogSuccess();
            }

            return(customResult);
        }