public void WritePolicy_11() {
            var subject = new XacmlSubject(
                new XacmlSubjectMatch[] 
                {  
                    new XacmlSubjectMatch(
                        new Uri("http://www.MatchId.www"),
                        new XacmlAttributeValue(new Uri("http://www.DataType.www")), 
                        new XacmlSubjectAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false, Category = new Uri("http://www.subjectCategory.www") })
                });

            var target = new XacmlTarget(subject, null, null);
            XacmlPolicySet xacmlPolicySet = new XacmlPolicySet(new Uri("http://www.PolicySetId.www"), new Uri("http://www.PolicyCombiningAlgId.www"), target);
            xacmlPolicySet.Description = "description string";
            xacmlPolicySet.XPathVersion = Xacml10Constants.XPathVersions.Xpath10;
            
            XacmlPolicy xacmlPolicy = new XacmlPolicy(new Uri("http://www.PolicyId.www"), new Uri("http://www.RuleCombiningAlgId.www"), new XacmlTarget()) {
                Description = "description string",
                XPathVersion = Xacml10Constants.XPathVersions.Xpath10,
            };

            XacmlRule xacmlRule = new XacmlRule("http://www.RuleId.www", XacmlEffectType.Permit) {
                Description = "xacmlRule description"
            };

            xacmlPolicy.Rules.Add(xacmlRule);
            XacmlAttributeAssignment xacmlAttributeAssignment = new XacmlAttributeAssignment(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www"));
            XacmlObligation xacmlObligation = new XacmlObligation(new Uri("http://www.ObligationId.www"), XacmlEffectType.Permit, new XacmlAttributeAssignment[] { xacmlAttributeAssignment });
            xacmlPolicy.Obligations.Add(xacmlObligation);

            xacmlPolicySet.Policies.Add(xacmlPolicy);

            StringBuilder builder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(builder)) {
                var serializer = new Xacml10ProtocolSerializer();
                serializer.WritePolicySet(writer, xacmlPolicySet);
            }

            string xml = builder.ToString();
            ValidateMessage(xml, @"..\..\_Data\cs-xacml-schema-policy-01.xsd");
        }
        public void WritePolicy_20() {
            var subject = new XacmlSubject(
                new XacmlSubjectMatch[] 
                {  
                    new XacmlSubjectMatch(
                        new Uri("http://www.MatchId.www"),
                        new XacmlAttributeValue(new Uri("http://www.DataType.www")), 
                        new XacmlSubjectAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false, Category = new Uri("http://www.subjectCategory.www")}
                        )
                });

            var resource = new XacmlResource(
            new XacmlResourceMatch[] 
                { 
                    new XacmlResourceMatch(
                        new Uri("http://www.MatchId.www"), 
                        new XacmlAttributeValue(new Uri("http://www.DataType.www") /*, "xxxx" */), 
                        new XacmlResourceAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false} 
                        )
                });

            var action = new XacmlAction(
                new XacmlActionMatch[] 
                { 
                    new XacmlActionMatch(
                        new Uri("http://www.MatchId.www"), 
                        new XacmlAttributeValue(new Uri("http://www.DataType.www")), 
                        new XacmlActionAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")){ Issuer = "String", MustBePresent = false} 
                        )
                });

            var target = new XacmlTarget(subject, resource, action, null);

            // new Uri("http://www.PolicySetId.www")
            XacmlPolicySet xacmlPolicySet = new XacmlPolicySet(new Uri("http://www.PolicyCombiningAlgId.www"), target) {
                Description = "description string",
                XPathVersion = Xacml10Constants.XPathVersions.Xpath10,
            };
            
            ////#region Policy
            XacmlEnvironment env = new XacmlEnvironment(
                new XacmlEnvironmentMatch[]
                {
                    new XacmlEnvironmentMatch(
                        new Uri("http://www.EnvironmentMatchIdId.www"), 
                        new XacmlAttributeValue(new Uri("http://www.AttributValue.www")), 
                        new XacmlEnvironmentAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")){ Issuer = "String", MustBePresent = false} 
                        )
                });

            XacmlTarget targetWithEnvironment = new XacmlTarget(null, null, null, new XacmlEnvironment[] { env });

            XacmlPolicy xacmlPolicy = new XacmlPolicy(new Uri("http://www.PolicyId.www"), new Uri("http://www.RuleCombiningAlgId.www"), targetWithEnvironment) {
                Description = "description string",
                XPathVersion = Xacml10Constants.XPathVersions.Xpath10,
            };

            XacmlRule xacmlRule = new XacmlRule("http://www.RuleId.www", XacmlEffectType.Permit) {
                Description = "xacmlRule description"
            };

            xacmlPolicy.Rules.Add(xacmlRule);

            XacmlAttributeAssignment xacmlAttributeAssignment = new XacmlAttributeAssignment(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www"));
            XacmlObligation xacmlObligation = new XacmlObligation(new Uri("http://www.ObligationId.www"), XacmlEffectType.Permit, new XacmlAttributeAssignment[] { xacmlAttributeAssignment });
            xacmlPolicy.Obligations.Add(xacmlObligation);

            xacmlPolicySet.Policies.Add(xacmlPolicy);

            StringBuilder builder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(builder)) {
                var serializer = new Xacml20ProtocolSerializer();
                serializer.WritePolicySet(writer, xacmlPolicySet);
            }

            string xml = builder.ToString();
            ValidateMessage(xml, @"..\..\_Data\access_control-xacml-2.0-policy-schema-os.xsd");
        }
        public void Add() {
            var target = new XacmlTarget();

            var subject1 = new XacmlSubject(
                new XacmlSubjectMatch[] 
                {  
                    new XacmlSubjectMatch(
                        new Uri("http://subject1"),
                        new XacmlAttributeValue(new Uri("http://www.DataType.www")), 
                        new XacmlSubjectAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false, Category = new Uri("http://www.subjectCategory.www") })
                });

            var subject2 = new XacmlSubject(
                new XacmlSubjectMatch[] 
                {  
                    new XacmlSubjectMatch(
                        new Uri("http://subject2"),
                        new XacmlAttributeValue(new Uri("http://www.DataType.www")), 
                        new XacmlSubjectAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false, Category = new Uri("http://www.subjectCategory.www") })
                });

            var resource1 = new XacmlResource(
            new XacmlResourceMatch[] 
                { 
                    new XacmlResourceMatch(
                        new Uri("http://resource1"), 
                        new XacmlAttributeValue(new Uri("http://www.DataType.www") /*, "xxxx" */), 
                        new XacmlResourceAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false} 
                        )
                });

            var resource2 = new XacmlResource(
            new XacmlResourceMatch[] 
                { 
                    new XacmlResourceMatch(
                        new Uri("http://resource1"), 
                        new XacmlAttributeValue(new Uri("http://www.DataType.www") /*, "xxxx" */), 
                        new XacmlResourceAttributeDesignator(new Uri("http://www.AttributeId.www"), new Uri("http://www.DataType.www")) { Issuer = "String", MustBePresent = false} 
                        )
                });

            {
                target.AnyOf.Add(new XacmlAnyOf(new XacmlAllOf[] { subject1 }));

                Assert.AreEqual(1, target.AnyOf.Count);
                Assert.AreEqual(1, target.AnyOf.SelectMany(x => x.AllOf).Count());
                Assert.AreEqual(1, target.Subjects.Count);
                Assert.AreEqual(0, target.Resources.Count);
                Assert.AreEqual(new Uri("http://subject1"), target.Subjects.First().Matches.First().MatchId);
            }

            {
                target.Subjects.Add(subject2);

                Assert.AreEqual(1, target.AnyOf.Count);
                Assert.AreEqual(2, target.AnyOf.SelectMany(x => x.AllOf).Count());
                Assert.AreEqual(2, target.Subjects.Count);
                Assert.AreEqual(0, target.Resources.Count);
            }

            {
                target.AnyOf.Add(new XacmlAnyOf(new XacmlAllOf[] { resource1 }));

                Assert.AreEqual(2, target.AnyOf.Count);
                Assert.AreEqual(3, target.AnyOf.SelectMany(x => x.AllOf).Count());
                Assert.AreEqual(2, target.Subjects.Count);
                Assert.AreEqual(1, target.Resources.Count);
            }

            {
                target.Resources.Add(resource2);

                Assert.AreEqual(2, target.AnyOf.Count);
                Assert.AreEqual(4, target.AnyOf.SelectMany(x => x.AllOf).Count());
                Assert.AreEqual(2, target.Subjects.Count);
                Assert.AreEqual(2, target.Resources.Count);

                foreach (var res in target.Resources) {
                }

                Assert.AreEqual(true, target.Resources.Contains(resource1));
            }

            {
                target.Subjects.Remove(subject1);

                Assert.AreEqual(2, target.AnyOf.Count);
                Assert.AreEqual(3, target.AnyOf.SelectMany(x => x.AllOf).Count());
                Assert.AreEqual(1, target.Subjects.Count);
                Assert.AreEqual(2, target.Resources.Count);
            }

            {
                target.Resources.Clear();

                Assert.AreEqual(1, target.AnyOf.Count);
                Assert.AreEqual(1, target.AnyOf.SelectMany(x => x.AllOf).Count());
                Assert.AreEqual(1, target.Subjects.Count);
                Assert.AreEqual(0, target.Resources.Count);
            }
        }
        /// <summary>
        /// Reads the subject.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        /// <exception cref="System.Xml.XmlException">Subject NotStartElement</exception>
        protected virtual XacmlSubject ReadSubject(XmlReader reader) {
            Contract.Requires<ArgumentNullException>(reader != null, "reader");

            if (!reader.IsStartElement(XacmlConstants.ElementNames.Subject, this.version.NamespacePolicy)) {
                throw Diagnostic.DiagnosticTools.ExceptionUtil.ThrowHelperError(new XmlException("Subject NotStartElement"));
            }

            reader.ReadStartElement(XacmlConstants.ElementNames.Subject, this.version.NamespacePolicy);
            List<XacmlSubjectMatch> matches = new List<XacmlSubjectMatch>();
            this.ReadListAbstract(matches, XacmlConstants.ElementNames.SubjectMatch, this.version.NamespacePolicy, this.ReadMatch, reader, true);
            XacmlSubject subj = new XacmlSubject(matches);

            reader.ReadEndElement();
            return subj;
        }
Exemple #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XacmlTarget" /> class.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="resource">The resource.</param>
 /// <param name="action">The action.</param>
 /// <param name="environment">The environment.</param>
 /// <remarks>
 /// Used only from XACML 2.0
 /// </remarks>
 public XacmlTarget(XacmlSubject subject, XacmlResource resource, XacmlAction action, XacmlEnvironment environment)
     : this(subject != null ? new XacmlSubject[] { subject } : null, resource != null ? new XacmlResource[] { resource } : null, action != null ? new XacmlAction[] { action } : null, environment != null ? new XacmlEnvironment[] { environment } : null)
 {
 }
Exemple #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XacmlTarget"/> class.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="resource">The resource.</param>
 /// <param name="action">The action.</param>
 /// <param name="environment">The environment.</param>
 /// <remarks>
 /// Used only from XACML 1.0/1.1/2.0
 /// </remarks>
 public XacmlTarget(XacmlSubject subject, XacmlResource resource, XacmlAction action)
     : this(subject != null ? new XacmlSubject[] { subject } : null, resource != null ? new XacmlResource[] { resource } : null, action != null ? new XacmlAction[] { action } : null, null)
 {
 }