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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "biscuit_auth"
crate-type = ["cdylib"]

[dependencies]
biscuit-auth = { version = "7.0.0-alpha.1", features = ["pem"] }
biscuit-auth = { version = "7.0.0-alpha.2", features = ["pem"] }
pyo3 = { version = "0.28.2", features = ["extension-module", "chrono"] }
hex = "0.4"
base64 = "0.13.0"
Expand Down
8 changes: 6 additions & 2 deletions biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_biscuit_builder():
'bool': True,
'bytes': b'\xaa\xbb',
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
'set': {2, True, "Test", datetime(2023, 4, 29, 1, 0, 0, tzinfo = timezone.utc) },
'set': {2, 3},
'array': [2, True, "Test", ["inner"]],
'map': { 'key': [{12: "value"}]}
},
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_biscuit_builder():
bool(true);
bytes(hex:aabb);
datetime(2023-04-03T10:00:00Z);
set({2, "Test", 2023-04-29T01:00:00Z, true});
set({2, 3});
array([2, true, "Test", ["inner"]]);
map({"key": [{12: "value"}]});
fact(false);
Expand All @@ -98,6 +98,10 @@ def test_biscuit_builder():
builder.build(private_key)
assert True

def test_heterogeneous_set():
with pytest.raises(ValueError, match="set elements must have the same type"):
Fact("set({set})", { 'set': {2, "Test"} })

def test_block_builder():
pubkey = PublicKey("ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = BlockBuilder(
Expand Down
4 changes: 2 additions & 2 deletions docs/datalog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ time(2023-06-09T00:00:00Z)
bytes(hex:aabbff)
>>> Fact("bytes({bytes})", {'bytes': [0xaa, 0xbb, 255]})
bytes(hex:aabbff)
>>> Fact("set({set})", {'set': {0, True, "ab", b'\xaa'}})
set({0, "ab", hex:aa, true})
>>> Fact("set({set})", {'set': {0, 1, 2}})
set({0, 1, 2})

Inspecting datalog values
-------------------------
Expand Down
32 changes: 23 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ pub enum PyAlgorithm {
Secp256r1,
}

impl From<builder::Algorithm> for PyAlgorithm {
fn from(value: builder::Algorithm) -> Self {
impl TryFrom<builder::Algorithm> for PyAlgorithm {
type Error = PyErr;

fn try_from(value: builder::Algorithm) -> PyResult<Self> {
match value {
builder::Algorithm::Ed25519 => Self::Ed25519,
builder::Algorithm::Secp256r1 => Self::Secp256r1,
builder::Algorithm::Ed25519 => Ok(Self::Ed25519),
builder::Algorithm::Secp256r1 => Ok(Self::Secp256r1),
_ => Err(PyValueError::new_err(format!(
"unsupported algorithm: {value:?}"
))),
}
}
}
Expand Down Expand Up @@ -1401,11 +1406,20 @@ impl PyTerm {
pub fn to_term(&self) -> PyResult<builder::Term> {
match self {
PyTerm::Simple(s) => s.to_term(),
PyTerm::Set(vs) => vs
.iter()
.map(|s| s.to_term())
.collect::<PyResult<_>>()
.map(builder::Term::Set),
PyTerm::Set(vs) => {
let mut kinds = vs.iter().map(std::mem::discriminant);
if let Some(first) = kinds.next() {
if kinds.any(|k| k != first) {
return Err(PyValueError::new_err(
"set elements must have the same type",
));
}
}
vs.iter()
.map(|s| s.to_term())
.collect::<PyResult<_>>()
.map(builder::Term::Set)
}
PyTerm::Array(vs) => vs
.iter()
.map(|s| s.to_term())
Expand Down
Loading