Пример #1
0
        public async Task <CustomGrantValidationResult> ValidateAsync(ValidatedTokenRequest request)
        {
            var validator = _validators.FirstOrDefault(v => v.GrantType.Equals(request.GrantType, StringComparison.Ordinal));

            if (validator == null)
            {
                return(new CustomGrantValidationResult
                {
                    IsError = true,
                    Error = "No validator found for grant type"
                });
            }

            try
            {
                return(await validator.ValidateAsync(request));
            }
            catch (Exception e)
            {
                Logger.Error("Grant validation error:" + e.Message);
                return(new CustomGrantValidationResult
                {
                    IsError = true,
                    Error = "Grant validation error",
                });
            }
        }
Пример #2
0
        public async Task <TokenRequestValidationResult> ValidateRequestAsync(NameValueCollection parameters, Client client)
        {
            Logger.Info("Start token request validation");

            _validatedRequest = new ValidatedTokenRequest();

            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            _validatedRequest.Raw     = parameters;
            _validatedRequest.Client  = client;
            _validatedRequest.Options = _options;

            /////////////////////////////////////////////
            // check grant type
            /////////////////////////////////////////////
            var grantType = parameters.Get(Constants.TokenRequest.GrantType);

            if (grantType.IsMissing())
            {
                LogError("Grant type is missing.");
                return(Invalid(Constants.TokenErrors.UnsupportedGrantType));
            }

            if (grantType.Length > _options.InputLengthRestrictions.GrantType)
            {
                LogError("Grant type is too long.");
                return(Invalid(Constants.TokenErrors.UnsupportedGrantType));
            }

            _validatedRequest.GrantType = grantType;

            // standard grant types
            switch (grantType)
            {
            case Constants.GrantTypes.AuthorizationCode:
                return(await RunValidationAsync(ValidateAuthorizationCodeRequestAsync, parameters));

            case Constants.GrantTypes.ClientCredentials:
                return(await RunValidationAsync(ValidateClientCredentialsRequestAsync, parameters));

            case Constants.GrantTypes.Password:
                return(await RunValidationAsync(ValidateResourceOwnerCredentialRequestAsync, parameters));

            case Constants.GrantTypes.RefreshToken:
                return(await RunValidationAsync(ValidateRefreshTokenRequestAsync, parameters));
            }

            // custom grant type
            var result = await RunValidationAsync(ValidateCustomGrantRequestAsync, parameters);

            if (result.IsError)
            {
                if (result.Error.IsPresent())
                {
                    return(result);
                }

                LogError("Unsupported grant_type: " + grantType);
                return(Invalid(Constants.TokenErrors.UnsupportedGrantType));
            }

            return(result);
        }