Skip to content

Commit 6d55705

Browse files
authored
refactor builder modules (#249)
This splits the builders to their own files
1 parent 255abf2 commit 6d55705

File tree

12 files changed

+2878
-2740
lines changed

12 files changed

+2878
-2740
lines changed

biscuit-auth/src/token/builder.rs

Lines changed: 47 additions & 2740 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::convert::TryFrom;
2+
3+
use crate::error;
4+
5+
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
6+
pub enum Algorithm {
7+
Ed25519,
8+
Secp256r1,
9+
}
10+
11+
impl TryFrom<&str> for Algorithm {
12+
type Error = error::Format;
13+
fn try_from(value: &str) -> Result<Self, Self::Error> {
14+
match value {
15+
"ed25519" => Ok(Algorithm::Ed25519),
16+
"secp256r1" => Ok(Algorithm::Secp256r1),
17+
_ => Err(error::Format::DeserializationError(format!(
18+
"deserialization error: unexpected key algorithm {}",
19+
value
20+
))),
21+
}
22+
}
23+
}
24+
25+
impl From<biscuit_parser::builder::Algorithm> for Algorithm {
26+
fn from(value: biscuit_parser::builder::Algorithm) -> Algorithm {
27+
match value {
28+
biscuit_parser::builder::Algorithm::Ed25519 => Algorithm::Ed25519,
29+
biscuit_parser::builder::Algorithm::Secp256r1 => Algorithm::Secp256r1,
30+
}
31+
}
32+
}
33+
34+
impl From<Algorithm> for biscuit_parser::builder::Algorithm {
35+
fn from(value: Algorithm) -> biscuit_parser::builder::Algorithm {
36+
match value {
37+
Algorithm::Ed25519 => biscuit_parser::builder::Algorithm::Ed25519,
38+
Algorithm::Secp256r1 => biscuit_parser::builder::Algorithm::Secp256r1,
39+
}
40+
}
41+
}
42+
43+
impl From<crate::format::schema::public_key::Algorithm> for Algorithm {
44+
fn from(value: crate::format::schema::public_key::Algorithm) -> Algorithm {
45+
match value {
46+
crate::format::schema::public_key::Algorithm::Ed25519 => Algorithm::Ed25519,
47+
crate::format::schema::public_key::Algorithm::Secp256r1 => Algorithm::Secp256r1,
48+
}
49+
}
50+
}
51+
52+
impl From<Algorithm> for crate::format::schema::public_key::Algorithm {
53+
fn from(value: Algorithm) -> crate::format::schema::public_key::Algorithm {
54+
match value {
55+
Algorithm::Ed25519 => crate::format::schema::public_key::Algorithm::Ed25519,
56+
Algorithm::Secp256r1 => crate::format::schema::public_key::Algorithm::Secp256r1,
57+
}
58+
}
59+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
use super::{BlockBuilder, Check, Fact, Rule, Scope, Term};
2+
use crate::builder_ext::BuilderExt;
3+
use crate::crypto::PublicKey;
4+
use crate::datalog::SymbolTable;
5+
use crate::token::default_symbol_table;
6+
use crate::{error, Biscuit, KeyPair};
7+
use rand::{CryptoRng, RngCore};
8+
9+
use std::fmt;
10+
use std::time::SystemTime;
11+
use std::{collections::HashMap, convert::TryInto, fmt::Write};
12+
13+
/// creates a Biscuit
14+
#[derive(Clone, Default)]
15+
pub struct BiscuitBuilder {
16+
inner: BlockBuilder,
17+
root_key_id: Option<u32>,
18+
}
19+
20+
impl BiscuitBuilder {
21+
pub fn new() -> BiscuitBuilder {
22+
BiscuitBuilder {
23+
inner: BlockBuilder::new(),
24+
root_key_id: None,
25+
}
26+
}
27+
28+
pub fn merge(&mut self, other: BlockBuilder) {
29+
self.inner.merge(other)
30+
}
31+
32+
pub fn add_fact<F: TryInto<Fact>>(&mut self, fact: F) -> Result<(), error::Token>
33+
where
34+
error::Token: From<<F as TryInto<Fact>>::Error>,
35+
{
36+
self.inner.add_fact(fact)
37+
}
38+
39+
pub fn add_rule<Ru: TryInto<Rule>>(&mut self, rule: Ru) -> Result<(), error::Token>
40+
where
41+
error::Token: From<<Ru as TryInto<Rule>>::Error>,
42+
{
43+
self.inner.add_rule(rule)
44+
}
45+
46+
pub fn add_check<C: TryInto<Check>>(&mut self, check: C) -> Result<(), error::Token>
47+
where
48+
error::Token: From<<C as TryInto<Check>>::Error>,
49+
{
50+
self.inner.add_check(check)
51+
}
52+
53+
pub fn add_code<T: AsRef<str>>(&mut self, source: T) -> Result<(), error::Token> {
54+
self.inner
55+
.add_code_with_params(source, HashMap::new(), HashMap::new())
56+
}
57+
58+
pub fn add_code_with_params<T: AsRef<str>>(
59+
&mut self,
60+
source: T,
61+
params: HashMap<String, Term>,
62+
scope_params: HashMap<String, PublicKey>,
63+
) -> Result<(), error::Token> {
64+
self.inner
65+
.add_code_with_params(source, params, scope_params)
66+
}
67+
68+
pub fn add_scope(&mut self, scope: Scope) {
69+
self.inner.add_scope(scope);
70+
}
71+
72+
#[cfg(test)]
73+
pub(crate) fn add_right(&mut self, resource: &str, right: &str) {
74+
use crate::builder::fact;
75+
76+
use super::string;
77+
78+
let _ = self.add_fact(fact("right", &[string(resource), string(right)]));
79+
}
80+
81+
pub fn set_context(&mut self, context: String) {
82+
self.inner.set_context(context);
83+
}
84+
85+
pub fn set_root_key_id(&mut self, root_key_id: u32) {
86+
self.root_key_id = Some(root_key_id);
87+
}
88+
89+
/// returns all of the datalog loaded in the biscuit builder
90+
pub fn dump(&self) -> (Vec<Fact>, Vec<Rule>, Vec<Check>) {
91+
(
92+
self.inner.facts.clone(),
93+
self.inner.rules.clone(),
94+
self.inner.checks.clone(),
95+
)
96+
}
97+
98+
pub fn dump_code(&self) -> String {
99+
let (facts, rules, checks) = self.dump();
100+
let mut f = String::new();
101+
for fact in facts {
102+
let _ = writeln!(f, "{};", fact);
103+
}
104+
for rule in rules {
105+
let _ = writeln!(f, "{};", rule);
106+
}
107+
for check in checks {
108+
let _ = writeln!(f, "{};", check);
109+
}
110+
f
111+
}
112+
113+
pub fn build(self, root_key: &KeyPair) -> Result<Biscuit, error::Token> {
114+
self.build_with_symbols(root_key, default_symbol_table())
115+
}
116+
117+
pub fn build_with_symbols(
118+
self,
119+
root_key: &KeyPair,
120+
symbols: SymbolTable,
121+
) -> Result<Biscuit, error::Token> {
122+
self.build_with_rng(root_key, symbols, &mut rand::rngs::OsRng)
123+
}
124+
125+
pub fn build_with_rng<R: RngCore + CryptoRng>(
126+
self,
127+
root: &KeyPair,
128+
symbols: SymbolTable,
129+
rng: &mut R,
130+
) -> Result<Biscuit, error::Token> {
131+
let authority_block = self.inner.build(symbols.clone());
132+
Biscuit::new_with_rng(rng, self.root_key_id, root, symbols, authority_block)
133+
}
134+
135+
pub fn build_with_key_pair(
136+
self,
137+
root: &KeyPair,
138+
symbols: SymbolTable,
139+
next: &KeyPair,
140+
) -> Result<Biscuit, error::Token> {
141+
let authority_block = self.inner.build(symbols.clone());
142+
Biscuit::new_with_key_pair(self.root_key_id, root, next, symbols, authority_block)
143+
}
144+
}
145+
146+
impl fmt::Display for BiscuitBuilder {
147+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148+
match self.root_key_id {
149+
None => writeln!(f, "// no root key id set")?,
150+
Some(id) => writeln!(f, "// root key id: {}", id)?,
151+
}
152+
self.inner.fmt(f)
153+
}
154+
}
155+
156+
impl BuilderExt for BiscuitBuilder {
157+
fn add_resource(&mut self, name: &str) {
158+
self.inner.add_resource(name);
159+
}
160+
fn check_resource(&mut self, name: &str) {
161+
self.inner.check_resource(name);
162+
}
163+
fn check_resource_prefix(&mut self, prefix: &str) {
164+
self.inner.check_resource_prefix(prefix);
165+
}
166+
fn check_resource_suffix(&mut self, suffix: &str) {
167+
self.inner.check_resource_suffix(suffix);
168+
}
169+
fn add_operation(&mut self, name: &str) {
170+
self.inner.add_operation(name);
171+
}
172+
fn check_operation(&mut self, name: &str) {
173+
self.inner.check_operation(name);
174+
}
175+
fn check_expiration_date(&mut self, date: SystemTime) {
176+
self.inner.check_expiration_date(date);
177+
}
178+
}

0 commit comments

Comments
 (0)