diff --git a/biscuit_auth.pyi b/biscuit_auth.pyi index da07790..ff46464 100644 --- a/biscuit_auth.pyi +++ b/biscuit_auth.pyi @@ -642,6 +642,11 @@ class ThirdPartyRequest: block: BlockBuilder ) -> ThirdPartyBlock: ... + def __new__ ( + self, + base64=None + ) -> ThirdPartyRequest: ... + # Third party block contents class ThirdPartyBlock: pass diff --git a/biscuit_test.py b/biscuit_test.py index 9ec6d06..fd2b685 100644 --- a/biscuit_test.py +++ b/biscuit_test.py @@ -4,7 +4,10 @@ import pytest -from biscuit_auth import Algorithm, KeyPair, Authorizer, AuthorizerBuilder, Biscuit, BiscuitBuilder, BlockBuilder, Check, Fact, KeyPair, Policy, PrivateKey, PublicKey, Rule, UnverifiedBiscuit +from biscuit_auth import Algorithm, KeyPair, Authorizer, AuthorizerBuilder, Biscuit, BiscuitBuilder, BlockBuilder, \ + Check, Fact, KeyPair, Policy, PrivateKey, PublicKey, Rule, UnverifiedBiscuit, ThirdPartyRequest, \ + BiscuitSerializationError, BiscuitValidationError + def test_fact(): fact = Fact('fact(1, true, "", "Test", hex:aabbcc, 2023-04-29T01:00:00Z)') @@ -511,3 +514,40 @@ def test_read_third_party_facts(): facts = authorizer.query(rule) assert facts[0].terms[0] == "56" + +def test_third_party_serialize(): + root_kp = KeyPair() + biscuit_builder = BiscuitBuilder("user({id})", { 'id': "1234" }) + biscuit = biscuit_builder.build(root_kp.private_key) + + third_party_kp = KeyPair() + new_block = BlockBuilder("external_fact({fact})", { 'fact': "56" }) + third_party_request = biscuit.third_party_request() + + b64 = third_party_request.to_base64() + assert type(b64) == str + + recovered_request = ThirdPartyRequest(base64=b64) + third_party_block = recovered_request.create_block(third_party_kp.private_key, new_block) + biscuit_with_third_party_block = biscuit.append_third_party(third_party_kp.public_key, third_party_block) + + assert repr(biscuit_with_third_party_block.block_external_key(1)) == repr(third_party_kp.public_key) + +def test_third_party_serialize_fail(): + root_kp = KeyPair() + biscuit_builder = BiscuitBuilder("user({id})", { 'id': "1234" }) + biscuit = biscuit_builder.build(root_kp.private_key) + + third_party_kp = KeyPair() + new_block = BlockBuilder("external_fact({fact})", { 'fact': "56" }) + third_party_request = biscuit.third_party_request() + + b64 = third_party_request.to_base64() + assert type(b64) == str + + # reverse the string to make it invalid + b64 = b64[::-1] + + # expect an error when trying to decode the invalid b64 string + with pytest.raises(BiscuitValidationError): + recovered_request = ThirdPartyRequest(base64=b64) diff --git a/src/lib.rs b/src/lib.rs index ab08894..ec97673 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1064,12 +1064,42 @@ impl PyBlockBuilder { } } -/// Third party block request #[pyclass(name = "ThirdPartyRequest")] pub struct PyThirdPartyRequest(Option); #[pymethods] impl PyThirdPartyRequest { + /// Build a third-party request from a base64-encoded serialized request. + /// + /// :param base64: a base64-encoded third-party request + /// :type base64: str, optional + /// :return: the third-party request + /// :rtype: ThirdPartyRequest + #[new] + #[pyo3(signature = (base64=None))] + pub fn new(base64: Option<&str>) -> PyResult { + match base64 { + Some(base64) => ThirdPartyRequest::deserialize_base64(base64) + .map(|request| PyThirdPartyRequest(Some(request))) + .map_err(|error| BiscuitValidationError::new_err(error.to_string())), + None => Err(PyValueError::new_err( + "ThirdPartyRequest requires a base64 parameter", + )), + } + } + + /// Serializes the third-party request to URL safe base64 data. + /// + /// :return: the serialized third-party request + /// :rtype: str + pub fn to_base64(&self) -> PyResult { + self.0 + .as_ref() + .ok_or_else(|| PyValueError::new_err("third party request already consumed"))? + .serialize_base64() + .map_err(|error| BiscuitSerializationError::new_err(error.to_string())) + } + /// Create a third-party block /// /// :param private_key: the third-party's private key used to sign the block