Skip to content
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
54 changes: 14 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ def test_fact():
assert fact.name == "fact"
assert fact.terms == [1, True, "", "Test", b'\xaa\xbb\xcc', datetime(2023, 4, 29, 1, 0, 0, tzinfo = timezone.utc)]

def test_fact_collection_terms():
assert Fact('fact({1, 2, 3})').terms == [{1, 2, 3}]
assert Fact('fact([1, 2, 3])').terms == [[1, 2, 3]]
assert Fact('fact({"a": 1, "b": 2})').terms == [{"a": 1, "b": 2}]
assert Fact('fact({1: "x", 2: "y"})').terms == [{1: "x", 2: "y"}]
assert Fact('fact(null)').terms == [None]
assert Fact('fact([1, {2, 3}, {"k": [4, 5]}, null])').terms == [[1, {2, 3}, {"k": [4, 5]}, None]]

def test_query_collection_terms():
# collection terms also convert cleanly when returned from an authorizer query
builder = AuthorizerBuilder()
builder.add_fact(Fact('data({1, 2, 3}, [4, 5], {"k": 6}, null)'))
authorizer = builder.build_unauthenticated()

facts = authorizer.query(Rule("res($s, $a, $m, $n) <- data($s, $a, $m, $n)"))
assert len(facts) == 1
assert facts[0].terms == [{1, 2, 3}, [4, 5], {"k": 6}, None]

def test_biscuit_builder():
kp = KeyPair()
pubkey = PublicKey("ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
Expand Down
9 changes: 3 additions & 6 deletions docs/datalog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ Terms of a fact can be extracted to python values.o
>>> fact.terms
['abc', 123, b'\xaa', datetime.datetime(2023, 6, 9, 0, 0, tzinfo=datetime.timezone.utc), True]

.. warning::
Extracting sets is not supported yet.

>>> Fact("fact({123})").terms
Traceback (most recent call last):
...
pyo3_runtime.PanicException: not yet implemented
>>> fact = Fact("""fact({123}, [123], {"a": 123}, null)""")
>>> fact.terms
[{123}, [123], {'a': 123}, None]

43 changes: 33 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use ::biscuit_auth::RootKeyProvider;
use ::biscuit_auth::ThirdPartyBlock;
use ::biscuit_auth::ThirdPartyRequest;
use ::biscuit_auth::UnverifiedBiscuit;
use chrono::DateTime;
use chrono::Duration;
use chrono::TimeZone;
use chrono::Utc;
Expand Down Expand Up @@ -1326,19 +1325,43 @@ fn inner_term_to_py(t: &builder::Term, py: Python<'_>) -> PyResult<Py<PyAny>> {
}
builder::Term::Bytes(bs) => bs.clone().into_py_any(py),
builder::Term::Bool(b) => (*b).into_py_any(py),
_ => Err(DataLogError::new_err("Invalid term value".to_string())),
builder::Term::Null => py.None().into_py_any(py),
builder::Term::Set(vs) => {
let set = PySet::empty(py)?;
for v in vs {
set.add(inner_term_to_py(v, py)?)?;
}
set.into_py_any(py)
}
builder::Term::Array(vs) => {
let list = PyList::empty(py);
for v in vs {
list.append(inner_term_to_py(v, py)?)?;
}
list.into_py_any(py)
}
builder::Term::Map(m) => {
let dict = PyDict::new(py);
for (k, v) in m {
let key = match k {
MapKey::Integer(i) => (*i).into_py_any(py)?,
MapKey::Str(s) => s.into_py_any(py)?,
MapKey::Parameter(_) => {
return Err(DataLogError::new_err("Invalid map key".to_string()))
}
};
dict.set_item(key, inner_term_to_py(v, py)?)?;
}
dict.into_py_any(py)
}
builder::Term::Parameter(_) | builder::Term::Variable(_) => {
Err(DataLogError::new_err("Invalid term value".to_string()))
}
}
}

fn term_to_py(t: &builder::Term) -> PyResult<Py<PyAny>> {
Python::attach(|py| match t {
builder::Term::Parameter(_) => Err(DataLogError::new_err("Invalid term value".to_string())),
builder::Term::Variable(_) => Err(DataLogError::new_err("Invalid term value".to_string())),
builder::Term::Set(_vs) => todo!(),
builder::Term::Array(_vs) => todo!(),
builder::Term::Map(_vs) => todo!(),
term => inner_term_to_py(term, py),
})
Python::attach(|py| inner_term_to_py(t, py))
}

/// Wrapper for a non-naïve python date
Expand Down
Loading