public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var baseValue = base.TryCast(bindingData);

            if (baseValue.valid)
            {
                return(baseValue);
            }

            baseValue.value     = GetValue();
            baseValue.valid     = true;
            baseValue.fromQuery = true;
            return(baseValue);

            object GetValue()
            {
                if (parameterRequiringValidation.ParameterType.IsSubClassOfGeneric(typeof(IRefOptional <>)))
                {
                    var instance = RefOptionalHelper.CreateEmpty(
                        parameterRequiringValidation.ParameterType.GenericTypeArguments.First());
                    return(instance);
                }

                return(parameterRequiringValidation.ParameterType.GetDefault());
            }
        }
        public static SelectParameterResult TryCast(BindingData bindingData,
                                                    string key, bool checkFileName)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var queryResult = bindingData.fetchQueryParam(parameterRequiringValidation,
                                                          (v) =>
            {
                return(SelectParameterResult.Query(v, key, parameterRequiringValidation));
            },
                                                          (whyQuery) => SelectParameterResult.FailureQuery(whyQuery, key, parameterRequiringValidation));

            if (queryResult.valid)
            {
                return(queryResult);
            }

            if (!checkFileName)
            {
                return(queryResult);
            }

            return(bindingData.fetchDefaultParam(parameterRequiringValidation,
                                                 (v) =>
            {
                return SelectParameterResult.File(v, key, parameterRequiringValidation);
            },
                                                 (whyQuery) =>
            {
                return queryResult;
            }));
        }
        public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var key = this.GetKey(parameterRequiringValidation);

            return(TryCast(bindingData, key, this.CheckFileName));
        }
Beispiel #4
0
        public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var key = this.GetKey(parameterRequiringValidation);

            return(bindingData.fetchBodyParam(parameterRequiringValidation,
                                              vCasted => SelectParameterResult.Body(vCasted, key, parameterRequiringValidation),
                                              why => SelectParameterResult.FailureBody(why, key, parameterRequiringValidation)));
        }
        public virtual SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var key        = GetKey(parameterRequiringValidation);
            var fileResult = bindingData.fetchDefaultParam(parameterRequiringValidation,
                                                           (v) =>
            {
                return(SelectParameterResult.File(v, key, parameterRequiringValidation));
            },
                                                           (whyQuery) => SelectParameterResult.FailureFile(
                                                               $"Could not create value in file.",
                                                               key, parameterRequiringValidation));

            if (fileResult.valid)
            {
                return(fileResult);
            }

            var queryResult = bindingData.fetchQueryParam(parameterRequiringValidation,
                                                          (v) =>
            {
                if (fileResult.valid)
                {
                    fileResult.fromQuery = true;
                    return(fileResult);
                }
                return(SelectParameterResult.Query(v, key, parameterRequiringValidation));
            },
                                                          (whyQuery) =>
            {
                if (fileResult.valid)
                {
                    return(fileResult);
                }
                return(SelectParameterResult.FailureQuery(whyQuery, key, parameterRequiringValidation));
            });

            if (queryResult.valid)
            {
                return(queryResult);
            }

            return(bindingData.fetchBodyParam(parameterRequiringValidation,
                                              (v) =>
            {
                return SelectParameterResult.Body(v, key, parameterRequiringValidation);
            },
                                              (whyQuery) =>
            {
                if (queryResult.valid)
                {
                    return queryResult;
                }
                return SelectParameterResult.FailureBody(whyQuery, key, parameterRequiringValidation);
            }));
        }
        public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var baseValue = base.TryCast(bindingData);

            if (baseValue.valid)
            {
                return(baseValue);
            }

            baseValue.valid    = true;
            baseValue.fromBody = true;
            baseValue.value    = GetValue();
            return(baseValue);

            object GetValue()
            {
                var parameterType = parameterRequiringValidation.ParameterType;

                if (parameterType.IsSubClassOfGeneric(typeof(IRefOptional <>)))
                {
                    var refType = parameterType.GenericTypeArguments.First();
                    var parameterTypeGeneric = RefOptionalHelper.CreateEmpty(refType);
                    return(parameterTypeGeneric);
                }

                if (parameterType.IsSubClassOfGeneric(typeof(IRefs <>)))
                {
                    var refType = parameterType.GenericTypeArguments.First();
                    var parameterTypeGeneric = typeof(Refs <>).MakeGenericType(new Type[] { refType });
                    var refIds           = new Guid[] { };
                    var refIdsLookupType = typeof(Func <,>).MakeGenericType(new Type[] { typeof(Guid), refType });
                    var refIdsLookup     = refIdsLookupType.GetDefault();
                    return(Activator.CreateInstance(
                               parameterTypeGeneric, new object[] { refIds }));
                }

                if (parameterType.IsSubClassOfGeneric(typeof(IDictionary <,>)))
                {
                    var parameterTypeGeneric = typeof(Dictionary <,>).MakeGenericType(parameterType.GenericTypeArguments);
                    return(Activator.CreateInstance(parameterTypeGeneric));
                }

                return(parameterType.GetDefault());
            }
        }
Beispiel #7
0
        public virtual SelectParameterResult TryCast(BindingData bindingData)
        {
            var request = bindingData.request;
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;

            return(request.GetClaims(
                       (claimsEnumerable) =>
            {
                var claims = claimsEnumerable.ToArray();
                return AppSettings.ActorIdClaimType.ConfigurationString(
                    (accountIdClaimType) =>
                {
                    return claims
                    .First <Claim, SelectParameterResult>(
                        (claim, next) =>
                    {
                        if (String.Compare(claim.Type, accountIdClaimType) == 0)
                        {
                            var accountId = Guid.Parse(claim.Value);
                            if (parameterRequiringValidation.ParameterType.IsSubClassOfGeneric(typeof(IRef <>)))
                            {
                                var instantiatableRefType = typeof(Ref <>)
                                                            .MakeGenericType(parameterRequiringValidation.ParameterType.GenericTypeArguments);
                                var refInstance = Activator.CreateInstance(instantiatableRefType,
                                                                           new object[] { accountId });
                                return SelectParameterResult.Header(refInstance, "Authentication", parameterRequiringValidation);
                            }
                            return SelectParameterResult.FailureHeader(
                                $"Inform server developer type `{parameterRequiringValidation.ParameterType.FullName}` is not a valid Authorization result.",
                                "Authentication", parameterRequiringValidation);
                        }
                        return next();
                    },
                        () =>
                    {
                        return SelectParameterResult.FailureHeader("Account is not set in token",
                                                                   "Authentication", parameterRequiringValidation);
                    });
                },
                    (why) => SelectParameterResult.FailureHeader(why, "Authentication", parameterRequiringValidation));
            },
                       () => SelectParameterResult.FailureHeader("Authentication header not set.",
                                                                 "Authentication", parameterRequiringValidation),
                       (why) => SelectParameterResult.FailureHeader(why, "Authentication", parameterRequiringValidation)));
        }
Beispiel #8
0
        public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var request = bindingData.request;
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var bindType = parameterRequiringValidation.ParameterType;

            request.GetAbsoluteUri();
            if (typeof(MediaTypeHeaderValue) == bindType)
            {
                if (Content.IsNullOrWhiteSpace())
                {
                    return new SelectParameterResult
                           {
                               valid         = true,
                               fromBody      = false,
                               fromQuery     = false,
                               fromFile      = false,
                               key           = Content,
                               parameterInfo = parameterRequiringValidation,
                               value         = request.GetMediaType(),
                           }
                }
                ;

                return(bindingData.fetchBodyParam(
                           parameterRequiringValidation,
                           (value) =>
                {
                    return new SelectParameterResult
                    {
                        valid = true,
                        fromBody = false,
                        fromQuery = false,
                        fromFile = false,
                        key = Content,
                        parameterInfo = parameterRequiringValidation,
                        value = value,
                    };
                },
                           (why) =>
                {
                    return new SelectParameterResult
                    {
                        fromBody = false,
                        key = "",
                        fromQuery = false,
                        fromFile = false,
                        parameterInfo = parameterRequiringValidation,
                        valid = false,
                        failure = why,     // $"Cannot extract MediaTypeHeaderValue from non-multipart request.",
                    };
                }));
            }
            if (bindType.IsSubClassOfGeneric(typeof(IHttpHeaderValueCollection <>)))
            {
                if (bindType.GenericTypeArguments.First() == typeof(StringWithQualityHeaderValue))
                {
                    if (Content.HasBlackSpace())
                    {
                        return new SelectParameterResult
                               {
                                   key           = "",
                                   fromQuery     = false,
                                   fromBody      = false,
                                   fromFile      = false,
                                   parameterInfo = parameterRequiringValidation,
                                   valid         = false,
                                   failure       = "AcceptLanguage is not a content header.",
                               }
                    }
                    ;

                    var accepts   = request.GetAcceptLanguage().ToArray();
                    var valueCast = new HeaderValues <StringWithQualityHeaderValue>(accepts);
                    return(new SelectParameterResult
                    {
                        valid = true,
                        fromBody = false,
                        fromQuery = false,
                        fromFile = false,
                        key = default,
Beispiel #9
0
        public RouteMatch IsRouteMatch(
            MethodInfo method, string[] componentsMatched,
            IInvokeResource resourceInvoker, IHttpRequest request, IApplication httpApp,
            IEnumerable <string> bodyKeys, CastDelegate fetchBodyParam)
        {
            var fileNameCastDelegate  = GetFileNameCastDelegate(request, httpApp, componentsMatched, out string[] pathKeys);
            var fetchQueryParam       = GetQueryCastDelegate(request, httpApp, out string[] queryKeys);
            var parametersCastResults = method
                                        .GetParameters()
                                        .Where(param => param.ContainsAttributeInterface <IBindApiValue>())
                                        .Select(
                (param) =>
            {
                var castValue   = param.GetAttributeInterface <IBindApiValue>();
                var bindingData = new BindingData
                {
                    httpApp                      = httpApp,
                    fetchDefaultParam            = fileNameCastDelegate,
                    fetchQueryParam              = fetchQueryParam,
                    fetchBodyParam               = fetchBodyParam,
                    method                       = method,
                    parameterRequiringValidation = param,
                    request                      = request,
                    resourceInvoker              = resourceInvoker,
                };
                return(castValue.TryCast(bindingData));
            })
                                        .ToArray();

            var failedValidations = parametersCastResults
                                    .Where(pcr => !pcr.valid)
                                    .ToArray();

            return(HasExtraParameters(method,
                                      pathKeys, queryKeys, bodyKeys,
                                      parametersCastResults,
                                      () =>
            {
                if (failedValidations.Any())
                {
                    return new RouteMatch
                    {
                        isValid = false,
                        failedValidations = failedValidations,
                        method = method,
                    };
                }

                //var parametersWithValues = parametersCastResults
                //    .Select(parametersCastResult =>
                //        parametersCastResult.parameterInfo.PairWithValue(parametersCastResult.value))
                //    .ToArray();

                return new RouteMatch
                {
                    isValid = true,
                    method = method,
                    parametersWithValues = parametersCastResults,
                };
            },
                                      (extraFileParams, extraQueryParams, extraBodyParams) =>
            {
                return new RouteMatch
                {
                    isValid = false,
                    failedValidations = failedValidations,
                    method = method,
                    extraFileParams = extraFileParams,
                    extraQueryParams = extraQueryParams,
                    extraBodyParams = extraBodyParams,
                };
            }));
        }