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

refactor builders #249

Merged
merged 7 commits into from
Nov 27, 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,787 changes: 47 additions & 2,740 deletions biscuit-auth/src/token/builder.rs

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions biscuit-auth/src/token/builder/algorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::convert::TryFrom;

use crate::error;

#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub enum Algorithm {
Ed25519,
Secp256r1,
}

impl TryFrom<&str> for Algorithm {
type Error = error::Format;
fn try_from(value: &str) -> Result<Self, Self::Error> {

Check warning on line 13 in biscuit-auth/src/token/builder/algorithm.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/algorithm.rs#L13

Added line #L13 was not covered by tests
match value {
"ed25519" => Ok(Algorithm::Ed25519),
"secp256r1" => Ok(Algorithm::Secp256r1),
_ => Err(error::Format::DeserializationError(format!(

Check warning on line 17 in biscuit-auth/src/token/builder/algorithm.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/algorithm.rs#L15-L17

Added lines #L15 - L17 were not covered by tests
"deserialization error: unexpected key algorithm {}",
value
))),
}
}
}

impl From<biscuit_parser::builder::Algorithm> for Algorithm {
fn from(value: biscuit_parser::builder::Algorithm) -> Algorithm {
match value {
biscuit_parser::builder::Algorithm::Ed25519 => Algorithm::Ed25519,
biscuit_parser::builder::Algorithm::Secp256r1 => Algorithm::Secp256r1,

Check warning on line 29 in biscuit-auth/src/token/builder/algorithm.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/algorithm.rs#L29

Added line #L29 was not covered by tests
}
}
}

impl From<Algorithm> for biscuit_parser::builder::Algorithm {
fn from(value: Algorithm) -> biscuit_parser::builder::Algorithm {
match value {
Algorithm::Ed25519 => biscuit_parser::builder::Algorithm::Ed25519,
Algorithm::Secp256r1 => biscuit_parser::builder::Algorithm::Secp256r1,

Check warning on line 38 in biscuit-auth/src/token/builder/algorithm.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/algorithm.rs#L35-L38

Added lines #L35 - L38 were not covered by tests
}
}
}

impl From<crate::format::schema::public_key::Algorithm> for Algorithm {
fn from(value: crate::format::schema::public_key::Algorithm) -> Algorithm {
match value {
crate::format::schema::public_key::Algorithm::Ed25519 => Algorithm::Ed25519,
crate::format::schema::public_key::Algorithm::Secp256r1 => Algorithm::Secp256r1,

Check warning on line 47 in biscuit-auth/src/token/builder/algorithm.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/algorithm.rs#L44-L47

Added lines #L44 - L47 were not covered by tests
}
}
}

impl From<Algorithm> for crate::format::schema::public_key::Algorithm {
fn from(value: Algorithm) -> crate::format::schema::public_key::Algorithm {
match value {
Algorithm::Ed25519 => crate::format::schema::public_key::Algorithm::Ed25519,
Algorithm::Secp256r1 => crate::format::schema::public_key::Algorithm::Secp256r1,

Check warning on line 56 in biscuit-auth/src/token/builder/algorithm.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/algorithm.rs#L53-L56

Added lines #L53 - L56 were not covered by tests
}
}
}
178 changes: 178 additions & 0 deletions biscuit-auth/src/token/builder/biscuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
use super::{BlockBuilder, Check, Fact, Rule, Scope, Term};
use crate::builder_ext::BuilderExt;
use crate::crypto::PublicKey;
use crate::datalog::SymbolTable;
use crate::token::default_symbol_table;
use crate::{error, Biscuit, KeyPair};
use rand::{CryptoRng, RngCore};

use std::fmt;
use std::time::SystemTime;
use std::{collections::HashMap, convert::TryInto, fmt::Write};

/// creates a Biscuit
#[derive(Clone, Default)]
pub struct BiscuitBuilder {
inner: BlockBuilder,
root_key_id: Option<u32>,
}

impl BiscuitBuilder {
pub fn new() -> BiscuitBuilder {
BiscuitBuilder {
inner: BlockBuilder::new(),
root_key_id: None,
}
}

pub fn merge(&mut self, other: BlockBuilder) {
self.inner.merge(other)

Check warning on line 29 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L28-L29

Added lines #L28 - L29 were not covered by tests
}

pub fn add_fact<F: TryInto<Fact>>(&mut self, fact: F) -> Result<(), error::Token>
where
error::Token: From<<F as TryInto<Fact>>::Error>,
{
self.inner.add_fact(fact)
}

pub fn add_rule<Ru: TryInto<Rule>>(&mut self, rule: Ru) -> Result<(), error::Token>
where
error::Token: From<<Ru as TryInto<Rule>>::Error>,
{
self.inner.add_rule(rule)
}

pub fn add_check<C: TryInto<Check>>(&mut self, check: C) -> Result<(), error::Token>
where
error::Token: From<<C as TryInto<Check>>::Error>,
{
self.inner.add_check(check)
}

pub fn add_code<T: AsRef<str>>(&mut self, source: T) -> Result<(), error::Token> {
self.inner
.add_code_with_params(source, HashMap::new(), HashMap::new())
}

pub fn add_code_with_params<T: AsRef<str>>(
&mut self,
source: T,
params: HashMap<String, Term>,
scope_params: HashMap<String, PublicKey>,
) -> Result<(), error::Token> {
self.inner
.add_code_with_params(source, params, scope_params)

Check warning on line 65 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L65

Added line #L65 was not covered by tests
}

pub fn add_scope(&mut self, scope: Scope) {
self.inner.add_scope(scope);

Check warning on line 69 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L68-L69

Added lines #L68 - L69 were not covered by tests
}

#[cfg(test)]
pub(crate) fn add_right(&mut self, resource: &str, right: &str) {
use crate::builder::fact;

use super::string;

let _ = self.add_fact(fact("right", &[string(resource), string(right)]));
}

pub fn set_context(&mut self, context: String) {
self.inner.set_context(context);

Check warning on line 82 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L81-L82

Added lines #L81 - L82 were not covered by tests
}

pub fn set_root_key_id(&mut self, root_key_id: u32) {
self.root_key_id = Some(root_key_id);
}

/// returns all of the datalog loaded in the biscuit builder
pub fn dump(&self) -> (Vec<Fact>, Vec<Rule>, Vec<Check>) {
(
self.inner.facts.clone(),
self.inner.rules.clone(),
self.inner.checks.clone(),
)
}

pub fn dump_code(&self) -> String {
let (facts, rules, checks) = self.dump();
let mut f = String::new();
for fact in facts {
let _ = writeln!(f, "{};", fact);
}
for rule in rules {
let _ = writeln!(f, "{};", rule);
}
for check in checks {
let _ = writeln!(f, "{};", check);
}
f
}

pub fn build(self, root_key: &KeyPair) -> Result<Biscuit, error::Token> {
self.build_with_symbols(root_key, default_symbol_table())
}

pub fn build_with_symbols(
self,
root_key: &KeyPair,
symbols: SymbolTable,
) -> Result<Biscuit, error::Token> {
self.build_with_rng(root_key, symbols, &mut rand::rngs::OsRng)
}

pub fn build_with_rng<R: RngCore + CryptoRng>(
self,
root: &KeyPair,
symbols: SymbolTable,
rng: &mut R,
) -> Result<Biscuit, error::Token> {
let authority_block = self.inner.build(symbols.clone());
Biscuit::new_with_rng(rng, self.root_key_id, root, symbols, authority_block)
}

pub fn build_with_key_pair(
self,
root: &KeyPair,
symbols: SymbolTable,
next: &KeyPair,
) -> Result<Biscuit, error::Token> {
let authority_block = self.inner.build(symbols.clone());
Biscuit::new_with_key_pair(self.root_key_id, root, next, symbols, authority_block)
}
}

impl fmt::Display for BiscuitBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.root_key_id {
None => writeln!(f, "// no root key id set")?,

Check warning on line 149 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L149

Added line #L149 was not covered by tests
Some(id) => writeln!(f, "// root key id: {}", id)?,
}
self.inner.fmt(f)
}
}

impl BuilderExt for BiscuitBuilder {
fn add_resource(&mut self, name: &str) {
self.inner.add_resource(name);

Check warning on line 158 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L157-L158

Added lines #L157 - L158 were not covered by tests
}
fn check_resource(&mut self, name: &str) {
self.inner.check_resource(name);

Check warning on line 161 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L160-L161

Added lines #L160 - L161 were not covered by tests
}
fn check_resource_prefix(&mut self, prefix: &str) {
self.inner.check_resource_prefix(prefix);

Check warning on line 164 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L163-L164

Added lines #L163 - L164 were not covered by tests
}
fn check_resource_suffix(&mut self, suffix: &str) {
self.inner.check_resource_suffix(suffix);

Check warning on line 167 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L166-L167

Added lines #L166 - L167 were not covered by tests
}
fn add_operation(&mut self, name: &str) {
self.inner.add_operation(name);

Check warning on line 170 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L169-L170

Added lines #L169 - L170 were not covered by tests
}
fn check_operation(&mut self, name: &str) {
self.inner.check_operation(name);

Check warning on line 173 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L172-L173

Added lines #L172 - L173 were not covered by tests
}
fn check_expiration_date(&mut self, date: SystemTime) {
self.inner.check_expiration_date(date);

Check warning on line 176 in biscuit-auth/src/token/builder/biscuit.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/src/token/builder/biscuit.rs#L175-L176

Added lines #L175 - L176 were not covered by tests
}
}
Loading
Loading