Example #1
0
        public void AddRule(PolicyRule rule)
        {
            foreach (var claim in rule.InputClaims)
            {
                claim.ClaimType.DisplayName = this.GenerateClaimTypeDisplayName(claim.ClaimType);
                if (!this.ClaimTypes.Contains(claim.ClaimType))
                {
                    this.ClaimTypes.Add(claim.ClaimType);
                }

                if (!this.Issuers.Contains(claim.Issuer))
                {
                    if (claim.Issuer == null)
                    {
                        throw new ArgumentException(Resources.IssuerNotNull);
                    }

                    throw new PolicyScopeException(
                        String.Format(CultureInfo.CurrentCulture, Resources.IssuerNotDefined, claim.Issuer.DisplayName));
                }
            }

            rule.OutputClaim.ClaimType.DisplayName = this.GenerateClaimTypeDisplayName(rule.OutputClaim.ClaimType);
            if (!this.ClaimTypes.Contains(rule.OutputClaim.ClaimType))
            {
                this.ClaimTypes.Add(rule.OutputClaim.ClaimType);
            }

            if (!this.Rules.Contains(rule))
            {
                this.Rules.Add(rule);
            }
        }
Example #2
0
        public void AddPolicyRule(Uri scopeUri, PolicyRule rule)
        {
            var scopes = this.RetrieveScopes() as IList<PolicyScope>;
            var policyScope = (from s in scopes
                               where s.Uri.ToString() == scopeUri.ToString()
                               select s).FirstOrDefault();

            if (policyScope == null)
            {
                throw new PolicyScopeException(Resources.ScopeNotFound);
            }

            policyScope.AddRule(rule);

            this.SaveScopes(scopes);
        }
        public void ShoudMatchInputClaimWithAssertionMatchAll()
        {
            var store = new MockPolicyStore();
            ClaimsPolicyEvaluator evaluator = new ClaimsPolicyEvaluator(store);

            InputPolicyClaim inputClaim = new InputPolicyClaim(this.issuer, this.inputClaimType, "myInputClaim");
            OutputPolicyClaim outputClaim = new OutputPolicyClaim(this.outputClaimType, "myOutputClaimValue");
            PolicyRule rule = new PolicyRule(AssertionsMatch.All, new[] { inputClaim }, outputClaim);
            store.RetrieveScopesReturnValue = new List<PolicyScope>() { new PolicyScope(new Uri("http://myScope"), new[] { rule }) };

            IEnumerable<Claim> evaluatedOutputClaims = evaluator.Evaluate(new Uri("http://myScope"), new[] { new Claim("http://myInputClaimType", "myInputClaim", string.Empty, "http://myInputClaimIssuer") });

            Assert.IsNotNull(evaluatedOutputClaims);
            Assert.AreEqual(1, evaluatedOutputClaims.Count());
            Assert.AreEqual("http://myOutputClaimType", evaluatedOutputClaims.ElementAt(0).ClaimType);
            Assert.AreEqual("myOutputClaimValue", evaluatedOutputClaims.ElementAt(0).Value);
        }
Example #4
0
        public void ShouldAddRuleViaWCF()
        {
            ChannelFactory<IPolicyStore> factory = new ChannelFactory<IPolicyStore>(new BasicHttpBinding(), new EndpointAddress("http://localhost:3333/policystore"));
            IPolicyStore store = factory.CreateChannel();
            var scope = store.RetrieveScopes().ElementAt(0);

            InputPolicyClaim inputClaim = new InputPolicyClaim(scope.Issuers.ElementAt(0), scope.ClaimTypes.ElementAt(0), "thevalue");
            OutputPolicyClaim outputClaim = new OutputPolicyClaim(scope.ClaimTypes.ElementAt(0), CopyFromConstants.InputValue);

            PolicyRule rule = new PolicyRule(AssertionsMatch.All, new[] { inputClaim }, outputClaim);

            store.AddPolicyRule(scope.Uri, rule);

            var updatedScope = store.RetrieveScopes().ElementAt(0);

            Assert.AreEqual(3, updatedScope.Rules.Count());
        }
Example #5
0
        public void AddPolicyRuleShouldPassIfExistingScope()
        {
            XmlPolicyStore store = new XmlPolicyStore("My Xml Store Path", new MockXmlRepository(@".\content\claimMappings-PassingTest2.xml"));

            int initialScopeCount = store.RetrieveScopes().Count();

            IList<InputPolicyClaim> inputClaims = new List<InputPolicyClaim>();
            Issuer issuer = new Issuer("http://myIssuer1");
            ClaimType claimType = new ClaimType("http://myClaimType", "myClaimType");
            inputClaims.Add(new InputPolicyClaim(issuer, claimType, "nicolas"));
            PolicyRule newRule = new PolicyRule(AssertionsMatch.Any, inputClaims, new OutputPolicyClaim(claimType, string.Empty, CopyFromConstants.InputValue));

            store.AddPolicyRule(new Uri("http://localhost/1"), newRule);

            int expectedScopeCount = initialScopeCount;
            Assert.AreEqual(expectedScopeCount, store.RetrieveScopes().Count());
            Assert.AreEqual(2, store.RetrieveScopes().ElementAt(0).Rules.Count());
        }
Example #6
0
        public void AddRuleShouldAddClaimTypeIfDoesNotExists()
        {
            var scope = RetrievePolicyScope();
            var claimFullName = "http://tests/newsampleclaimtype/";
            var inputClaim = new InputPolicyClaim(sampleIssuer, new ClaimType(claimFullName, string.Empty), "new sample value");
            var rule = new PolicyRule(AssertionsMatch.Any, new List<InputPolicyClaim> { inputClaim }, GetSampleOutputClaim());

            Assert.AreEqual(1, scope.ClaimTypes.Count);

            scope.AddRule(rule);

            Assert.AreEqual(2, scope.ClaimTypes.Count);

            var result = scope.ClaimTypes.ElementAt(1);

            Assert.AreEqual(claimFullName, result.FullName);
            Assert.AreEqual("newsampleclaimtype", result.DisplayName);
        }
Example #7
0
        public void AddRuleDoesNotAddPolicyRuleIfAlreadyExists()
        {
            var scope = RetrievePolicyScope();
            var orginalRule = new PolicyRule(AssertionsMatch.Any, GetSampleInputClaims(), GetSampleOutputClaim());

            Assert.AreEqual(0, scope.Rules.Count);

            scope.AddRule(orginalRule);

            Assert.AreEqual(1, scope.Rules.Count);
            Assert.AreSame(orginalRule, scope.Rules[0]);

            var copyRule = new PolicyRule(AssertionsMatch.Any, GetSampleInputClaims(), GetSampleOutputClaim());

            scope.AddRule(orginalRule);

            Assert.AreEqual(1, scope.Rules.Count);
            Assert.AreSame(orginalRule, scope.Rules[0]);
        }
Example #8
0
        public void ShouldPassEvaluateRuleIfCopyOutputValueFromInputIssuer()
        {
            var store = new XmlPolicyStore("My Xml Store Path", new MockXmlRepository(@"content\integrationTest2.xml"));

            PolicyScope scope = store.RetrieveScope(new Uri("http://localhost/1"));
            var issuer = scope.Issuers.ElementAt(0);
            IList<InputPolicyClaim> inputClaims = new List<InputPolicyClaim>();
            ClaimType claimType = new ClaimType("http://myClaimType", "myClaimType");
            inputClaims.Add(new InputPolicyClaim(issuer, claimType, "*"));
            PolicyRule newRule = new PolicyRule(AssertionsMatch.Any, inputClaims, new OutputPolicyClaim(claimType, string.Empty, CopyFromConstants.InputIssuer));

            store.AddPolicyRule(new Uri("http://localhost/1"), newRule);
            string claimValue = "myInputClaimValue33";

            ClaimsPolicyEvaluator evaluator = new ClaimsPolicyEvaluator(store);
            Claim inputClaim = new Claim("http://myClaimType", claimValue, string.Empty, "http://myIssuer1");
            IEnumerable<Claim> evaluatedOutputClaims = evaluator.Evaluate(new Uri("http://localhost/1"), new[] { inputClaim });

            Assert.IsNotNull(evaluatedOutputClaims);
            Assert.AreEqual("http://myClaimType", evaluatedOutputClaims.ElementAt(0).ClaimType);
        }
Example #9
0
        public void AddPolicyRuleShouldAddNewOutputClaimTypeIfDoesNotExists()
        {
            XmlPolicyStore store = new XmlPolicyStore("My Xml Store Path", new MockXmlRepository(@".\content\claimMappings-PassingTest3.xml"));
            var scopeUri = new Uri("http://localhost/1");

            IList<InputPolicyClaim> inputClaims = new List<InputPolicyClaim>();
            Issuer issuer = new Issuer("http://myIssuer1", "6f7051ece706096ac5a05ecb1860e2151c11b491", "myIssuer1");
            ClaimType claimType = new ClaimType("http://myClaimType", "myClaimType");

            inputClaims.Add(new InputPolicyClaim(issuer, claimType, "nicolas"));

            ClaimType newClaimType = new ClaimType("http://newClaimType", "myNewClaimType");

            PolicyRule newRule = new PolicyRule(AssertionsMatch.Any, inputClaims, new OutputPolicyClaim(newClaimType, string.Empty, CopyFromConstants.InputValue));

            store.AddPolicyRule(scopeUri, newRule);
            var scope = store.RetrieveScope(scopeUri);

            Assert.AreEqual(2, scope.ClaimTypes.Count);
            Assert.AreEqual(newClaimType.FullName, scope.ClaimTypes.ElementAt(1).FullName);
            Assert.AreEqual(newClaimType.DisplayName, scope.ClaimTypes.ElementAt(1).DisplayName);
        }
Example #10
0
        public void AddPolicyRuleShouldThrowIfNotExistingScope()
        {
            XmlPolicyStore store = new XmlPolicyStore("My Xml Store Path", new MockXmlRepository(@".\content\claimMappings-PassingTest2.xml"));

            IList<InputPolicyClaim> inputClaims = new List<InputPolicyClaim>();
            Issuer issuer = new Issuer("http://myIssuer1");
            ClaimType claimType = new ClaimType("http://myClaimType", "myClaimType");
            inputClaims.Add(new InputPolicyClaim(issuer, claimType, "nicolas"));
            PolicyRule newRule = new PolicyRule(AssertionsMatch.Any, inputClaims, new OutputPolicyClaim(claimType, string.Empty, CopyFromConstants.InputValue));

            store.AddPolicyRule(new Uri("http://notExistingScope/1"), newRule);
        }
        public void ShouldMatchInputClaimAndCopyInputIssuerToOutputValue()
        {
            var store = new MockPolicyStore();
            ClaimsPolicyEvaluator evaluator = new ClaimsPolicyEvaluator(store);

            ClaimType inputClaimType = new ClaimType("http://myInputClaimType");
            ClaimType outputClaimType = new ClaimType("http://myOutputClaimType");
            Issuer issuer = new Issuer("http://myInputClaimIssuer");

            InputPolicyClaim inputClaim = new InputPolicyClaim(issuer, inputClaimType, "myInputClaim");
            OutputPolicyClaim outputClaim = new OutputPolicyClaim(outputClaimType, string.Empty, CopyFromConstants.InputIssuer);
            PolicyRule rule = new PolicyRule(AssertionsMatch.Any, new[] { inputClaim }, outputClaim);
            store.RetrieveScopesReturnValue = new List<PolicyScope>() { new PolicyScope(new Uri("http://myScope"), new[] { rule }) };

            IEnumerable<Claim> evaluatedOutputClaims = evaluator.Evaluate(new Uri("http://myScope"), new[] { new Claim("http://myInputClaimType", "myInputClaim", string.Empty, "http://myInputClaimIssuer") });

            Assert.IsNotNull(evaluatedOutputClaims);
            Assert.AreEqual(1, evaluatedOutputClaims.Count());
            Assert.AreEqual("http://myOutputClaimType", evaluatedOutputClaims.ElementAt(0).ClaimType);
            Assert.AreEqual("http://myInputClaimIssuer", evaluatedOutputClaims.ElementAt(0).Value);
        }
Example #12
0
        private static IEnumerable<Claim> MatchesRule(PolicyRule rule, IEnumerable<Claim> inputClaims)
        {
            List<Claim> matchingClaims = new List<Claim>();
            foreach (InputPolicyClaim inputPolicyClaim in rule.InputClaims)
            {
                var claimsMatched = inputClaims.Where(c => (c.Issuer == inputPolicyClaim.Issuer.Uri || c.OriginalIssuer == inputPolicyClaim.Issuer.Uri)
                                                        && c.ClaimType.Equals(inputPolicyClaim.ClaimType.FullName, StringComparison.OrdinalIgnoreCase)
                                                        && ((inputPolicyClaim.Value == Wildcard) || (c.Value.ToUpperInvariant() == inputPolicyClaim.Value.ToUpperInvariant())));
                if (claimsMatched == null)
                {
                    break;
                }

                matchingClaims.AddRange(claimsMatched);
            }

            return matchingClaims;
        }
 public void AddPolicyRule(Uri scope, PolicyRule rule)
 {
     throw new NotImplementedException();
 }
 public void RemovePolicyRule(Uri scopeUri, PolicyRule rule)
 {
     throw new NotImplementedException();
 }
        public void ShouldOutputCorrectInputValue()
        {
            var store = new MockPolicyStore();
            ClaimsPolicyEvaluator evaluator = new ClaimsPolicyEvaluator(store);
            InputPolicyClaim inputPolicyClaim1 = new InputPolicyClaim(this.issuer, this.inputClaimType, "*");

            ClaimType outputClaimType1 = new ClaimType("http://myOutputClaimType1");
            OutputPolicyClaim outputPolicyClaim1 = new OutputPolicyClaim(outputClaimType1, "myOutputClaimValue");

            PolicyRule policyRule1 = new PolicyRule(AssertionsMatch.Any, new[] { inputPolicyClaim1 }, outputPolicyClaim1);

            InputPolicyClaim inputPolicyClaim2 = new InputPolicyClaim(this.issuer, this.inputClaimType, "inputClaimValue");

            ClaimType outputClaimType2 = new ClaimType("http://myOutputClaimType2");
            OutputPolicyClaim outputPolicyClaim2 = new OutputPolicyClaim(outputClaimType2, string.Empty, CopyFromConstants.InputValue);
            PolicyRule policyRule2 = new PolicyRule(AssertionsMatch.Any, new[] { inputPolicyClaim2 }, outputPolicyClaim2);
            store.RetrieveScopesReturnValue = new List<PolicyScope>() { new PolicyScope(new Uri("http://myScope"), new[] { policyRule1, policyRule2 }) };

            IEnumerable<Claim> evaluatedOutputClaims = evaluator.Evaluate(new Uri("http://myScope"), new[] { new Claim("http://myInputClaimType", "inputClaimValue", string.Empty, "http://myInputClaimIssuer") });

            Assert.IsNotNull(evaluatedOutputClaims);
            Assert.AreEqual(2, evaluatedOutputClaims.Count());
            var outputClaim1 = evaluatedOutputClaims.FirstOrDefault(c => c.ClaimType == "http://myOutputClaimType1");
            Assert.IsNotNull(outputClaim1);
            Assert.AreEqual("myOutputClaimValue", outputClaim1.Value);
            var outputClaim2 = evaluatedOutputClaims.FirstOrDefault(c => c.ClaimType == "http://myOutputClaimType2");
            Assert.IsNotNull(outputClaim2);
            Assert.AreEqual("inputClaimValue", outputClaim2.Value);
        }
Example #16
0
        public void AddRuleShouldSetTheRightClaimTypeDisplayName()
        {
            var scope = RetrievePolicyScope();
            var inputClaimType = new ClaimType("http://tests/sampleclaimtype/", string.Empty);

            var inputClaim = new InputPolicyClaim(sampleIssuer, inputClaimType, "new sample value");
            var rule = new PolicyRule(AssertionsMatch.Any, new List<InputPolicyClaim> { inputClaim }, GetSampleOutputClaim());

            Assert.AreEqual(string.Empty, inputClaimType.DisplayName);
            Assert.AreEqual(1, scope.ClaimTypes.Count);

            scope.AddRule(rule);

            Assert.AreEqual(sampleClaimType.DisplayName, inputClaimType.DisplayName);
            Assert.AreEqual(1, scope.ClaimTypes.Count);
        }
Example #17
0
        public void AddRuleThrowsIfIssuerOfInputClaimDoesNotExists()
        {
            var scope = RetrievePolicyScope();
            var newIssuer = new Issuer("http://newsampleissuer");
            var inputClaim = new InputPolicyClaim(newIssuer, sampleClaimType, "sample value");
            var rule = new PolicyRule(AssertionsMatch.Any, new List<InputPolicyClaim> { inputClaim }, GetSampleOutputClaim());

            scope.AddRule(rule);
        }
        public void ShouldMatchInputClaimValueInCaseInsensitiveFashion()
        {
            var store = new MockPolicyStore();
            var scopeUri = new Uri("http://myScope");
            var inputClaimValue = "myInputClaimValue";
            var outputClaimValue = "myOutputClaimValue";

            InputPolicyClaim inputClaim = new InputPolicyClaim(
                new Issuer("http://myInputClaimIssuer", "myInputClaimIssuer"),
                new ClaimType("http://myInputClaimType", "myInputClaimType"),
                inputClaimValue);
            OutputPolicyClaim outputClaim = new OutputPolicyClaim(
                new ClaimType("http://myOutputClaimType", "myOutputClaimType"),
                outputClaimValue);
            PolicyRule rule = new PolicyRule(AssertionsMatch.Any, new[] { inputClaim }, outputClaim);
            store.RetrieveScopesReturnValue = new List<PolicyScope> { new PolicyScope(scopeUri, new[] { rule }) };

            var evaluator = new ClaimsPolicyEvaluator(store);
            var evaluatedOutputClaims = evaluator.Evaluate(scopeUri, new[] { new Claim("http://myInputClaimType", inputClaimValue.ToUpperInvariant(), string.Empty, "http://myInputClaimIssuer") });

            Assert.IsNotNull(evaluatedOutputClaims);
            Assert.AreEqual(1, evaluatedOutputClaims.Count());
            Assert.AreEqual(outputClaimValue, evaluatedOutputClaims.ElementAt(0).Value);
        }
Example #19
0
        private static XElement SerializaRule(PolicyRule rule)
        {
            XElement ruleElement = new XElement("rule");
            ruleElement.SetAttributeValue("assertionsMatch", rule.AssertionsMatch.ToString());
            XElement inputElement = new XElement("input");
            ruleElement.Add(inputElement);
            foreach (var claim in rule.InputClaims)
            {
                inputElement.Add(SerializeInputClaim(claim));
            }

            ruleElement.Add(SerializeOutputClaim(rule.OutputClaim));
            return ruleElement;
        }
Example #20
0
        public void AddRuleThrowsIfIssuerOfInputClaimIsNull()
        {
            var scope = RetrievePolicyScope();
            var inputClaim = new InputPolicyClaim(null, sampleClaimType, "sample value");
            var rule = new PolicyRule(AssertionsMatch.Any, new List<InputPolicyClaim> { inputClaim }, GetSampleOutputClaim());

            scope.AddRule(rule);
        }
Example #21
0
 public void RemoveRule(PolicyRule rule)
 {
     this.Rules.Remove(rule);
 }
Example #22
0
 public void RemoveRule(PolicyRule rule)
 {
     this.Rules.Remove(rule);
 }
Example #23
0
        public void AddRuleShouldAddNewPolicyRuleToTheScope()
        {
            var scope = RetrievePolicyScope();
            var rule = new PolicyRule(AssertionsMatch.Any, GetSampleInputClaims(), GetSampleOutputClaim());

            Assert.AreEqual(0, scope.Rules.Count);

            scope.AddRule(rule);

            Assert.AreEqual(1, scope.Rules.Count);
            Assert.AreSame(rule, scope.Rules[0]);
        }