Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature fix rust version #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "biscuit-python"
version = "0.2.1"
edition = "2021"
rust-version = "1.79"

[lib]
name = "biscuit_auth"
Expand All @@ -13,3 +14,4 @@ pyo3 = { version = "0.18.3", features = ["extension-module", "chrono"]}
hex = "0.4"
base64 = "0.13.0"
chrono = "0.4"
time = "0.3.36"
15 changes: 14 additions & 1 deletion biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,17 @@ def test_keypair_from_private_key_pem():
private_key_pem = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIASZaU0NoF3KxABSZj5x1QwVOUZfiSbf6SAzz3qq1T1l\n-----END PRIVATE KEY-----"
private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
kp = KeyPair.from_private_key_pem(pem=private_key_pem)
assert kp.private_key.to_hex() == private_key_hex
assert kp.private_key.to_hex() == private_key_hex


def test_append_third_party_block():
root_kp = KeyPair()
external_kp = KeyPair()

builder = BiscuitBuilder("user({user});", {'user': "123"})
biscuit = builder.build(root_kp.private_key)

third_party_block = BlockBuilder("external_fact({fact});", {'fact': "456"})
new_biscuit = biscuit.append_third_party_block(external_kp, third_party_block)

assert new_biscuit.block_external_key(1).to_hex() == external_kp.public_key.to_hex()
47 changes: 47 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ create_exception!(
pyo3::exceptions::PyException
);

impl BiscuitBuildError {
/// Helper function to build `BiscuitBuildError` from other errors.
pub fn from_err(error: impl ToString) -> PyErr {
Self::new_err(error.to_string())
}
}

struct PyKeyProvider {
py_value: PyObject,
}
Expand Down Expand Up @@ -315,6 +322,31 @@ impl PyBiscuit {
.map(PyBiscuit)
}

/// Create a new `Biscuit` by appending a third party attenuation block
///
/// :param kp: keypair used to sign the block
/// :type kp: KeyPair
/// :param block: a builder for the new block
/// :type block: BlockBuilder
/// :return: the attenuated biscuit
/// :rtype: Biscuit
pub fn append_third_party_block(
&self,
kp: &PyKeyPair,
block: &PyBlockBuilder,
) -> PyResult<PyBiscuit> {
let third_party_block = self
.0
.third_party_request()
.and_then(|req| req.create_block(&kp.private_key().0, block.0.clone()))
.map_err(BiscuitBuildError::from_err)?;

self.0
.append_third_party(kp.public_key().0, third_party_block)
.map_err(BiscuitBuildError::from_err)
.map(PyBiscuit)
}

/// The revocation ids of the token, encoded as hexadecimal strings
#[getter]
pub fn revocation_ids(&self) -> Vec<String> {
Expand All @@ -325,6 +357,21 @@ impl PyBiscuit {
.collect()
}

/// Get the external key of a block if it exists
///
/// :param index: the block index
/// :type index: int
/// :return: the public key if it exists
/// :rtype: str | None
pub fn block_external_key(&self, index: usize) -> PyResult<Option<PyPublicKey>> {
let opt_key = self
.0
.block_external_key(index)
.map_err(|e| BiscuitBlockError::new_err(e.to_string()))?;

Ok(opt_key.map(PyPublicKey))
}

fn __repr__(&self) -> String {
self.0.print()
}
Expand Down