/// <summary> /// Reads the resource. /// </summary> /// <param name="reader">The reader.</param> /// <returns></returns> /// <exception cref="System.Xml.XmlException">Resource NotStartElement</exception> protected virtual XacmlResource ReadResource(XmlReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (!reader.IsStartElement(XacmlConstants.ElementNames.Resource, this.Version.NamespacePolicy)) { throw ThrowHelperXml(reader, "Resource NotStartElement"); } reader.ReadStartElement(XacmlConstants.ElementNames.Resource, this.Version.NamespacePolicy); List <XacmlResourceMatch> matches = new List <XacmlResourceMatch>(); this.ReadListAbstract(matches, XacmlConstants.ElementNames.ResourceMatch, this.Version.NamespacePolicy, this.ReadMatch, reader, true); XacmlResource res = new XacmlResource(matches); reader.ReadEndElement(); return(res); }
/// <summary> /// protected virtual void WriteResource /// </summary> /// <param name="writer">XmlWriter writer</param> /// <param name="data">XacmlResource data</param> protected virtual void WriteResource(XmlWriter writer, XacmlResource data) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (data == null) { throw new ArgumentNullException(nameof(data)); } writer.WriteStartElement(XacmlConstants.Prefixes.Policy, XacmlConstants.ElementNames.Resource, this.Version.NamespacePolicy); foreach (var resourceMatch in data.Matches) { writer.WriteAttributeString(XacmlConstants.AttributeNames.MatchId, resourceMatch.MatchId.OriginalString); writer.WriteStartElement(XacmlConstants.Prefixes.Policy, XacmlConstants.ElementNames.AttributeValue, this.Version.NamespacePolicy); writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, resourceMatch.AttributeValue.DataType.OriginalString); //// writer.WriteString("Text"); writer.WriteEndElement(); } }
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, Path.Combine(TestCasePath, "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); } }