Exemple #1
0
        /// <inheritdoc />
        public Task Authorize(AuthorizationContext context)
        {
            bool found = false;

            if (context.User != null)
            {
                if (AllowedValues == null || !AllowedValues.Any())
                {
                    found = context.User.Claims.Any(
                        claim => string.Equals(claim.Type, ClaimType, StringComparison.OrdinalIgnoreCase));
                }
                else
                {
                    found = context.User.Claims.Any(
                        claim => string.Equals(claim.Type, ClaimType, StringComparison.OrdinalIgnoreCase) &&
                        AllowedValues.Contains(claim.Value, StringComparer.Ordinal));
                }
            }

            if (!found)
            {
                if (AllowedValues != null && AllowedValues.Any())
                {
                    string values = string.Join(", ", DisplayValues ?? AllowedValues);
                    context.ReportError($"Required claim '{ClaimType}' with any value of '{values}' is not present.");
                }
                else
                {
                    context.ReportError($"Required claim '{ClaimType}' is not present.");
                }
            }

            return(Task.CompletedTask);
        }
        /// <inheritdoc />
        public override string ToString()
        {
            var value = (AllowedValues == null || !AllowedValues.Any())
                ? string.Empty
                : $" and `{ClaimConstants.Scp}` or `{ClaimConstants.Scope}` is one of the following values: ({string.Join("|", AllowedValues)})";

            return($"{nameof(ScopeAuthorizationRequirement)}:Scope={value}");
        }
Exemple #3
0
        public void Sanitize(Value value)
        {
            value.FieldName = Name ?? string.Empty;
            string sanitizedValue = TrimField ? value.OriginalValue.Trim()
                                              : value.OriginalValue;

            //If it wasn't in the original file, handle it in advance
            if (value.Missing || sanitizedValue == string.Empty)
            {
                if (!AllowedBlank)
                {
                    value.ErrorMsg = ValueIfBlank;
                }
                return;
            }

            //Removes any disallowed characters and retains only allowed characters
            var chars = sanitizedValue.ToCharArray();

            if (AllowedCharacters.Any())
            {
                chars = chars.Where(c => AllowedCharacters.Any(a => a == c)).ToArray();
            }
            if (DisallowedCharacters.Any())
            {
                chars = chars.Where(c => !DisallowedCharacters.Any(d => d == c)).ToArray();
            }

            //validate the length
            if (chars.Length > MaxLength ||
                chars.Length < MinLength)
            {
                value.SanitizedValue = new string(chars);
                value.ErrorMsg       = ValueIfWrongLength;
                return;
            }

            //run regex
            sanitizedValue = new string(chars);
            if (RegEx != string.Empty && !Regex.IsMatch(sanitizedValue, RegEx))
            {
                value.ErrorMsg = "Value failed regex check on value.";
                return;
            }

            //run any custom checks
            value.SanitizedValue = RemoveDoubleSpaces(sanitizedValue); //remove any double spaces
            CustomChecks.ForEach(c => c.Execute(value));

            //If this there are a fixed number of options, check for them
            if ((AllowedValues?.Count() ?? 0) != 0 &&
                !AllowedValues.Any(v => v == value.SanitizedValue))
            {
                value.ErrorMsg = ValueIfNotInAllowedOptions;
            }
        }