Skip to content
Draft
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
7 changes: 0 additions & 7 deletions biscuit-auth/src/token/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,6 @@ pub trait ToAnyParam {
fn to_any_param(&self) -> AnyParam;
}

#[cfg(feature = "datalog-macro")]
impl ToAnyParam for PublicKey {
fn to_any_param(&self) -> AnyParam {
AnyParam::PublicKey(*self)
}
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, convert::TryFrom};
Expand Down
19 changes: 11 additions & 8 deletions biscuit-auth/src/token/builder/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::{
error, PublicKey,
};

#[cfg(feature = "datalog-macro")]
use super::ToAnyParam;
use super::{display_rule_body, Convert, Rule, Term};

/// Builder for a Biscuit check
Expand Down Expand Up @@ -98,17 +96,22 @@ impl Check {
}

#[cfg(feature = "datalog-macro")]
pub fn set_macro_param<T: ToAnyParam>(
pub fn set_macro_param<T: Into<Term>>(
&mut self,
name: &str,
param: T,
) -> Result<(), error::Token> {
use super::AnyParam;
self.set_lenient(name, param.into())
}

match param.to_any_param() {
AnyParam::Term(t) => self.set_lenient(name, t),
AnyParam::PublicKey(p) => self.set_scope_lenient(name, p),
}
// TODO maybe introduce a conversion trait to support refs, multiple values, non-pk scopes
#[cfg(feature = "datalog-macro")]
pub fn set_macro_scope_param(
&mut self,
name: &str,
param: PublicKey,
) -> Result<(), error::Token> {
self.set_scope_lenient(name, param)
}

pub fn validate_parameters(&self) -> Result<(), error::Token> {
Expand Down
11 changes: 2 additions & 9 deletions biscuit-auth/src/token/builder/fact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::{
error,
};

#[cfg(feature = "datalog-macro")]
use super::ToAnyParam;
use super::{Convert, Predicate, Term};

/// Builder for a Datalog fact
Expand Down Expand Up @@ -115,17 +113,12 @@ impl Fact {
}

#[cfg(feature = "datalog-macro")]
pub fn set_macro_param<T: ToAnyParam>(
pub fn set_macro_param<T: Into<Term>>(
&mut self,
name: &str,
param: T,
) -> Result<(), error::Token> {
use super::AnyParam;

match param.to_any_param() {
AnyParam::Term(t) => self.set_lenient(name, t),
AnyParam::PublicKey(_) => Ok(()),
}
self.set_lenient(name, param.into())
}

pub(super) fn apply_parameters(&mut self) {
Expand Down
19 changes: 11 additions & 8 deletions biscuit-auth/src/token/builder/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use nom::Finish;

use crate::{error, PublicKey};

#[cfg(feature = "datalog-macro")]
use super::ToAnyParam;
use super::{display_rule_body, Rule, Term};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -91,17 +89,22 @@ impl Policy {
}

#[cfg(feature = "datalog-macro")]
pub fn set_macro_param<T: ToAnyParam>(
pub fn set_macro_param<T: Into<Term>>(
&mut self,
name: &str,
param: T,
) -> Result<(), error::Token> {
use super::AnyParam;
self.set_lenient(name, param.into())
}

match param.to_any_param() {
AnyParam::Term(t) => self.set_lenient(name, t),
AnyParam::PublicKey(p) => self.set_scope_lenient(name, p),
}
// TODO maybe introduce a conversion trait to support refs, multiple values, non-pk scopes
#[cfg(feature = "datalog-macro")]
pub fn set_macro_scope_param(
&mut self,
name: &str,
param: PublicKey,
) -> Result<(), error::Token> {
self.set_scope_lenient(name, param)
}

pub fn validate_parameters(&self) -> Result<(), error::Token> {
Expand Down
19 changes: 11 additions & 8 deletions biscuit-auth/src/token/builder/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::{
error, PublicKey,
};

#[cfg(feature = "datalog-macro")]
use super::ToAnyParam;
use super::{Convert, Expression, Predicate, Scope, Term};

/// Builder for a Datalog rule
Expand Down Expand Up @@ -245,17 +243,22 @@ impl Rule {
}

#[cfg(feature = "datalog-macro")]
pub fn set_macro_param<T: ToAnyParam>(
pub fn set_macro_param<T: Into<Term>>(
&mut self,
name: &str,
param: T,
) -> Result<(), error::Token> {
use super::AnyParam;
self.set_lenient(name, param.into())
}

match param.to_any_param() {
AnyParam::Term(t) => self.set_lenient(name, t),
AnyParam::PublicKey(pubkey) => self.set_scope_lenient(name, pubkey),
}
// TODO maybe introduce a conversion trait to support refs, multiple values, non-pk scopes
#[cfg(feature = "datalog-macro")]
pub fn set_macro_scope_param(
&mut self,
name: &str,
param: PublicKey,
) -> Result<(), error::Token> {
self.set_scope_lenient(name, param)
}

pub(super) fn apply_parameters(&mut self) {
Expand Down
25 changes: 24 additions & 1 deletion biscuit-auth/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2019 Geoffroy Couprie <[email protected]> and Contributors to the Eclipse Foundation.
* SPDX-License-Identifier: Apache-2.0
*/
use biscuit_auth::{builder, datalog::RunLimits, KeyPair};
use biscuit_auth::{builder, datalog::RunLimits, KeyPair, PublicKey};
use biscuit_quote::{
authorizer, authorizer_merge, biscuit, biscuit_merge, block, block_merge, check, fact, policy,
rule,
Expand Down Expand Up @@ -314,3 +314,26 @@ fn ecdsa() {
r#"rule($0, true) <- fact($0, $1, $2, "my_value", {0}) trusting secp256r1/0245dd01132962da3812911b746b080aed714873c1812e7cefacf13e3880712da0"#,
);
}

#[test]
fn trusting() {
// this should only work with a proper `PublicKey` value, and fail when trying to provide a string instead
let pubkey: PublicKey =
"secp256r1/0245dd01132962da3812911b746b080aed714873c1812e7cefacf13e3880712da0"
.parse()
.unwrap();
let _ = authorizer!(
r#"
nonce("a"); operation("o"); pathname("p");
d($x) <- nonce($x) trusting {pubkey}
"#
);
let _ = rule!(
r#"
data($nonce, $operation, $pathname)
<- nonce($nonce), operation($operation), pathname($pathname)

trusting {pubkey}
"#,
);
}
Loading
Loading