Ejemplo n.º 1
0
        private static void WriteMatch(XmlWriter writer, XacmlMatch xacmlMatch)
        {
            Guard.ArgumentNotNull(writer, nameof(writer));
            Guard.ArgumentNotNull(xacmlMatch, nameof(xacmlMatch));

            writer.WriteStartElement(XacmlConstants.Prefixes.Xacml, XacmlConstants.ElementNames.Match, Xacml30Constants.NameSpaces.Policy);

            writer.WriteAttributeString(XacmlConstants.AttributeNames.MatchId, xacmlMatch.MatchId.OriginalString);

            if (xacmlMatch.AttributeValue != null)
            {
                WriteAttributeValue(writer, xacmlMatch.AttributeValue);
            }

            if (xacmlMatch.AttributeDesignator != null)
            {
                WriteAttributeDesignator(writer, xacmlMatch.AttributeDesignator);
            }

            if (xacmlMatch.AttributeSelector != null)
            {
                throw new NotImplementedException($"XacmlSerializer: Serialization of type {xacmlMatch.AttributeSelector.GetType().Name} currently not supported");
            }

            writer.WriteEndElement();
        }
Ejemplo n.º 2
0
        private static void AddAttributeMatchToRule(XacmlMatch xacmlMatch, Rule rule)
        {
            if (xacmlMatch.AttributeDesignator.Category.Equals(XacmlConstants.MatchAttributeCategory.Action))
            {
                rule.Action = new AttributeMatch
                {
                    Id    = xacmlMatch.AttributeDesignator.AttributeId.OriginalString,
                    Value = xacmlMatch.AttributeValue.Value
                };
            }

            if (xacmlMatch.AttributeDesignator.Category.Equals(XacmlConstants.MatchAttributeCategory.Subject))
            {
                rule.CoveredBy.Add(new AttributeMatch
                {
                    Id    = xacmlMatch.AttributeDesignator.AttributeId.OriginalString,
                    Value = xacmlMatch.AttributeValue.Value
                });
            }

            if (xacmlMatch.AttributeDesignator.Category.Equals(XacmlConstants.MatchAttributeCategory.Resource))
            {
                rule.Resource.Add(new AttributeMatch
                {
                    Id    = xacmlMatch.AttributeDesignator.AttributeId.OriginalString,
                    Value = xacmlMatch.AttributeValue.Value
                });
            }
        }
Ejemplo n.º 3
0
        private static void AssertMatchEqual(XacmlMatch expected, XacmlMatch actual)
        {
            Assert.NotNull(actual);
            Assert.NotNull(expected);

            Assert.Equal(expected.MatchId, actual.MatchId);
            AssertAttributeDesignatorEqual(expected.AttributeDesignator, actual.AttributeDesignator);
            AssertAttributeValueEqual(expected.AttributeValue, actual.AttributeValue);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Writes the match.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="data">The data.</param>
        protected virtual void WriteMatch(XmlWriter writer, XacmlMatch data)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            Action <string, dynamic> action = (matchType, match) => {
                writer.WriteStartElement(XacmlConstants.Prefixes.Policy, matchType, this.Version.NamespacePolicy);
                writer.WriteAttributeString(XacmlConstants.AttributeNames.MatchId, match.MatchId.OriginalString);

                this.WriteAttributeValue(writer, match.AttributeValue);

                if (match.AttributeSelector == null && match.AttributeDesignator == null)
                {
                    throw new InvalidOperationException("AttributeSelector and AttributeDesignator is null.");
                }

                if (match.AttributeDesignator != null)
                {
                    this.WriteAttributeDesignator(writer, match.AttributeDesignator);
                }

                if (match.AttributeSelector != null)
                {
                    this.WriteAttributeSelector(writer, match.AttributeSelector);
                }

                writer.WriteEndElement();
            };

            var actionMatch = data as XacmlActionMatch;

            if (actionMatch != null)
            {
                action(XacmlConstants.ElementNames.ActionMatch, actionMatch);
            }

            var resourceMatch = data as XacmlResourceMatch;

            if (resourceMatch != null)
            {
                action(XacmlConstants.ElementNames.ResourceMatch, resourceMatch);
            }

            var subjectMatch = data as XacmlSubjectMatch;

            if (subjectMatch != null)
            {
                action(XacmlConstants.ElementNames.SubjectMatch, subjectMatch);
            }
        }
Ejemplo n.º 5
0
        private static AttributeMatch GetActionValueFromRule(XacmlRule rule)
        {
            foreach (XacmlAnyOf anyOf in rule.Target.AnyOf)
            {
                foreach (XacmlAllOf allOf in anyOf.AllOf)
                {
                    XacmlMatch action = allOf.Matches.FirstOrDefault(m => m.AttributeDesignator.Category.Equals(XacmlConstants.MatchAttributeCategory.Action));

                    if (action != null)
                    {
                        return(new AttributeMatch {
                            Id = action.AttributeDesignator.AttributeId.OriginalString, Value = action.AttributeValue.Value
                        });
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// Reads the match.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected virtual XacmlMatch ReadMatch(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var gaMatchId = reader.GetAttribute("MatchId");

            if (string.IsNullOrEmpty(gaMatchId))
            {
                throw ThrowHelperXml(reader, "MatchId IsNullOrEmpty");
            }

            Func <string, string, ReadElement <XacmlAttributeDesignator>, XacmlMatch> read = (matchType, designatorType, readFuncAttributeDesignator) => {
                XacmlMatch ret = null;
                if (!reader.IsStartElement(matchType, this.Version.NamespacePolicy))
                {
                    throw ThrowHelperXml(reader, string.Format("{0} NotStartElement", matchType));
                }

                reader.ReadStartElement(matchType, this.Version.NamespacePolicy);

                var attributeValue = ReadAttributeValue(reader);

                IDictionary <Tuple <string, string>, Action> dicts = null;
                XacmlAttributeSelector   sel = null;
                XacmlAttributeDesignator des = null;

                dicts = new Dictionary <Tuple <string, string>, Action>()
                {
                    { new Tuple <string, string>(designatorType, this.Version.NamespacePolicy), () => des = readFuncAttributeDesignator(reader) },
                    { new Tuple <string, string>(XacmlConstants.ElementNames.AttributeSelector, this.Version.NamespacePolicy), () => sel = ReadAttributeSelector(reader) },
                };

                this.ReadChoice(reader, dicts, isRequired: true);
                reader.ReadEndElement();

                switch (matchType)
                {
                case XacmlConstants.ElementNames.ActionMatch:
                    ret = des != null
                               ? new XacmlActionMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlActionAttributeDesignator)des)
                               : new XacmlActionMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel);
                    break;

                case XacmlConstants.ElementNames.SubjectMatch:
                    ret = des != null
                               ? new XacmlSubjectMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlSubjectAttributeDesignator)des)
                               : new XacmlSubjectMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel);
                    break;

                case XacmlConstants.ElementNames.ResourceMatch:
                    ret = des != null
                               ? new XacmlResourceMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlResourceAttributeDesignator)des)
                               : new XacmlResourceMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel);
                    break;

                default:
                    break;
                }

                return(ret);
            };

            if (reader.LocalName == XacmlConstants.ElementNames.ActionMatch)
            {
                return(read(reader.LocalName, XacmlConstants.ElementNames.ActionAttributeDesignator, ReadAttributeDesignator));
            }
            else if (reader.LocalName == XacmlConstants.ElementNames.SubjectMatch)
            {
                return(read(reader.LocalName, XacmlConstants.ElementNames.SubjectAttributeDesignator, ReadAttributeDesignator));
            }
            else if (reader.LocalName == XacmlConstants.ElementNames.ResourceMatch)
            {
                return(read(reader.LocalName, XacmlConstants.ElementNames.ResourceAttributeDesignator, ReadAttributeDesignator));
            }
            else
            {
                throw ThrowHelperXml(reader, string.Format("Incorrect start element. redaer.LocalName={0} ReadMatch", reader.LocalName));
            }
        }