Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use objects instead of tuples in the generated samples #203

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Check samples
run: |
cd biscuit-auth
cargo run --release --example testcases --features serde-error -- ./samples > ./samples/README.md
cargo run --release --example testcases --features serde-error -- ./samples --json > ./samples/samples.json
git diff --exit-code


Expand Down
103 changes: 85 additions & 18 deletions biscuit-auth/examples/testcases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,28 @@ impl TestResult {

#[derive(Debug, Serialize)]
struct AuthorizerWorld {
pub facts: BTreeSet<(String, BTreeSet<Option<usize>>)>,
pub rules: BTreeSet<(String, Option<usize>)>,
pub checks: BTreeSet<String>,
pub policies: BTreeSet<String>,
pub facts: Vec<AuthorizerFactSet>,
pub rules: Vec<AuthorizerRuleSet>,
pub checks: Vec<AuthorizerCheckSet>,
pub policies: Vec<String>,
}

#[derive(Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
struct AuthorizerFactSet {
origin: BTreeSet<Option<usize>>,
facts: Vec<String>,
}

#[derive(Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
struct AuthorizerRuleSet {
origin: Option<usize>,
rules: Vec<String>,
}

#[derive(Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
struct AuthorizerCheckSet {
origin: Option<usize>,
checks: Vec<String>,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -299,7 +317,7 @@ fn validate_token(root: &KeyPair, data: &[u8], authorizer_code: &str) -> Validat

let res = authorizer.authorize();
//println!("authorizer world:\n{}", authorizer.print_world());
let (_, _, mut checks, mut policies) = authorizer.dump();
let (_, _, _, policies) = authorizer.dump();
let snapshot = authorizer.snapshot().unwrap();

let symbols = SymbolTable::from_symbols_and_public_keys(
Expand All @@ -313,23 +331,65 @@ fn validate_token(root: &KeyPair, data: &[u8], authorizer_code: &str) -> Validat
)
.unwrap();

let mut facts = BTreeSet::new();
let mut rules = BTreeSet::new();
let mut authorizer_facts = Vec::new();
let mut authorizer_rules = Vec::new();
let mut authorizer_checks = Vec::new();
for (i, block) in snapshot.world.blocks.iter().enumerate() {
let mut origin = BTreeSet::new();
origin.insert(i);
let mut rules: Vec<String> = Vec::new();
for rule in block.rules_v2.iter() {
let r =
convert::proto_rule_to_token_rule(&rule, snapshot.world.version.unwrap()).unwrap();
rules.insert((symbols.print_rule(&r.0), Some(i)));
rules.push(symbols.print_rule(&r.0));
}
if !rules.is_empty() {
rules.sort();
authorizer_rules.push(AuthorizerRuleSet {
origin: Some(i),
rules,
});
}

let mut checks = Vec::new();
for check in block.checks_v2.iter() {
let c = convert::proto_check_to_token_check(&check, snapshot.world.version.unwrap())
.unwrap();
checks.push(symbols.print_check(&c));
}
if !checks.is_empty() {
checks.sort();
authorizer_checks.push(AuthorizerCheckSet {
origin: Some(i),
checks,
});
}
}

let mut authorizer_origin = BTreeSet::new();
authorizer_origin.insert(usize::MAX);
let mut rules: Vec<String> = Vec::new();
for rule in snapshot.world.authorizer_block.rules_v2 {
let r = convert::proto_rule_to_token_rule(&rule, snapshot.world.version.unwrap()).unwrap();
rules.insert((symbols.print_rule(&r.0), None));

rules.push(symbols.print_rule(&r.0));
}
if !rules.is_empty() {
rules.sort();
authorizer_rules.push(AuthorizerRuleSet {
origin: Some(usize::MAX),
rules,
});
}

let mut checks = Vec::new();
for check in snapshot.world.authorizer_block.checks_v2 {
let c =
convert::proto_check_to_token_check(&check, snapshot.world.version.unwrap()).unwrap();
checks.push(symbols.print_check(&c));
}
if !checks.is_empty() {
checks.sort();
authorizer_checks.push(AuthorizerCheckSet {
origin: Some(usize::MAX),
checks,
});
}

for factset in snapshot.world.generated_facts {
Expand All @@ -343,18 +403,25 @@ fn validate_token(root: &KeyPair, data: &[u8], authorizer_code: &str) -> Validat
};
}

let mut facts = Vec::new();

for fact in factset.facts {
let f = convert::proto_fact_to_token_fact(&fact).unwrap();
facts.insert((symbols.print_fact(&f), origin.clone()));
facts.push(symbols.print_fact(&f));
}
if !facts.is_empty() {
facts.sort();
authorizer_facts.push(AuthorizerFactSet { origin, facts });
}
}
authorizer_facts.sort();

Validation {
world: Some(AuthorizerWorld {
facts,
rules,
checks: checks.drain(..).map(|c| c.to_string()).collect(),
policies: policies.drain(..).map(|p| p.to_string()).collect(),
facts: authorizer_facts,
rules: authorizer_rules,
checks: authorizer_checks,
policies: policies.into_iter().map(|p| p.to_string()).collect(),
}),
result: match res {
Ok(i) => AuthorizerResult::Ok(i),
Expand Down
Loading
Loading