Exemple #1
0
        public Either <Error, HashSet <FactBuilder> > Query(RuleBuilder query, RunLimits limits)
        {
            Either <Error, Void> runRes = world.Run(limits, new HashSet <ulong>());

            if (runRes.IsLeft)
            {
                Error e = runRes.Left;
                return(new Left(e));
            }

            HashSet <Fact>        facts = world.QueryRule(query.Convert(symbols));
            HashSet <FactBuilder> s     = new HashSet <FactBuilder>();

            foreach (Fact f in facts)
            {
                s.Add(FactBuilder.ConvertFrom(f, symbols));
            }

            return(new Right(s));
        }
Exemple #2
0
        public Either <Error, Void> AddToken(Biscuit token, Option <PublicKey> root)
        {
            if (!token.IsSealed())
            {
                Either <Error, Void> res = token.CheckRootKey(root.Get());
                if (res.IsLeft)
                {
                    return(res.Left);
                }
            }

            if (this.token != null)
            {
                return(new FailedLogic(new LogicError.VerifierNotEmpty()));
            }

            ulong authority_index = symbols.Get("authority").Get();
            ulong ambient_index   = symbols.Get("ambient").Get();

            foreach (Fact fact in token.Authority.Facts)
            {
                if (fact.Predicate.Ids[0].Equals(new ID.Symbol(ambient_index)))
                {
                    return(new FailedLogic(new LogicError.InvalidAuthorityFact(symbols.PrintFact(fact))));
                }

                Fact converted_fact = FactBuilder.ConvertFrom(fact, token.Symbols).Convert(this.symbols);
                world.AddFact(converted_fact);
            }

            foreach (Rule rule in token.Authority.Rules)
            {
                Rule converted_rule = RuleBuilder.ConvertFrom(rule, token.Symbols).Convert(this.symbols);
                world.AddPrivilegedRule(converted_rule);
            }

            List <Check> authority_checks = new List <Check>();

            foreach (Check check in token.Authority.Checks)
            {
                Datalog.Check converted_check = CheckBuilder.ConvertFrom(check, token.Symbols).Convert(this.symbols);
                authority_checks.Add(converted_check);
            }
            token_checks.Add(authority_checks);

            for (int i = 0; i < token.Blocks.Count; i++)
            {
                Block b = token.Blocks[i];
                if (b.Index != i + 1)
                {
                    return(new InvalidBlockIndex(1 + token.Blocks.Count, token.Blocks[i].Index));
                }

                foreach (Fact fact in b.Facts)
                {
                    if (fact.Predicate.Ids[0].Equals(new ID.Symbol(authority_index)) ||
                        fact.Predicate.Ids[0].Equals(new ID.Symbol(ambient_index)))
                    {
                        return(new FailedLogic(new LogicError.InvalidBlockFact(i, symbols.PrintFact(fact))));
                    }

                    Fact converted_fact = FactBuilder.ConvertFrom(fact, token.Symbols).Convert(this.symbols);
                    world.AddFact(converted_fact);
                }

                foreach (Rule rule in b.Rules)
                {
                    Rule converted_rule = RuleBuilder.ConvertFrom(rule, token.Symbols).Convert(this.symbols);
                    world.AddRule(converted_rule);
                }

                List <Check> block_checks = new List <Check>();
                foreach (Check check in b.Checks)
                {
                    Check converted_check = CheckBuilder.ConvertFrom(check, token.Symbols).Convert(this.symbols);
                    block_checks.Add(converted_check);
                }
                token_checks.Add(block_checks);
            }

            List <RevocationIdentifier> revocation_ids = token.RevocationIdentifiers;
            ulong rev = symbols.Get("revocation_id").Get();

            for (int i = 0; i < revocation_ids.Count; i++)
            {
                byte[] id = revocation_ids[i].Bytes;
                world.AddFact(new Fact(new Predicate(rev, Arrays.AsList <ID>(new ID.Integer(i), new ID.Bytes(id)))));
            }

            return(new Right(null));
        }
Exemple #3
0
 public void AddFact(FactBuilder fact)
 {
     world.AddFact(fact.Convert(symbols));
 }