biscuit-rust
biscuit-rust copied to clipboard
Querying not functioning
I create a token like so:
builder
.add_authority_fact(&format!("user(\"{}\")", user.user_uuid) as &str)
.unwrap();
and when I later try to query it:
#[derive(Debug)]
pub struct StringWrap(String);
impl From<biscuit_auth::builder::Fact> for StringWrap {
fn from(b: biscuit_auth::builder::Fact) -> Self {
StringWrap(format!("{}", b))
}
}
let users: Vec<StringWrap> = b
.authorizer()
.unwrap()
.query("data($id) <- user($id)")
.unwrap();
dbg!(&users);
I always receive an empty array:
[src/lib.rs:70] &users = []
```,
but in the Symbols of the `biscuit`, it seems that it does properly contain it:
```bash
symbols: [
"authority",
"ambient",
"resource",
"operation",
"right",
"current_time",
"revocation_id",
"user",
"5f85453c-227c-4219-bbbe-f2a27895e7cc",
],
Also it seems that there is nothing that implements TryFrom<Fact>, which is why I added the StringWrap struct.
After a bit of investigating it seems to be that when the authorizer is constructed from the token, it doesn't populate the world with the information from the token, which it seems that it should do that?
pub(crate) fn from_token(token: &'t Biscuit) -> Result<Self, error::Token> {
let mut v = Authorizer::new()?;
v.token = Some(token);
Ok(v)
}
Or the querying must be extended in the same way that authorize_with_limits extends the world with information from the token if it's present:
if let Some(token) = self.token.as_ref() {
for fact in token.authority.facts.iter().cloned() {
let fact = Fact::convert_from(&fact, &token.symbols).convert(&mut self.symbols);
self.world.facts.insert(fact);
}
let mut revocation_ids = token.revocation_identifiers();
let revocation_id_sym = self.symbols.get("revocation_id").unwrap();
for (i, id) in revocation_ids.drain(..).enumerate() {
self.world.facts.insert(datalog::Fact::new(
revocation_id_sym,
&[datalog::Term::Integer(i as i64), datalog::Term::Bytes(id)],
));
}
for rule in token.authority.rules.iter().cloned() {
let r = Rule::convert_from(&rule, &token.symbols);
let rule = r.convert(&mut self.symbols);
if let Err(_message) = r.validate_variables() {
return Err(
error::Logic::InvalidBlockRule(0, token.symbols.print_rule(&rule)).into(),
);
}
}
}
however re-adding information to the world each time one of these functions is called seems off, so I think the best way forward would be to extract the above snippet into the functions that set the token, to ensure the world is populated once.
If you agree I'll create a PR for this approach.
I've also discovered in the meantime that querying is only possible for tuples out of the box, and if you'd expect a singular string, you have to use (String, ), so I can also try to add TryFrom<Fact> for some commonly used "base" types
Did this ever go anywhere? I'm having a similar issue of pulling data from a Biscuit. It'd be great to have easy access to user/expiration facts inside the token.
@johnhainline , the issues I had were resolved in https://github.com/biscuit-auth/biscuit-rust/pull/52