Skip to content
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
5 changes: 5 additions & 0 deletions biscuit_auth.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ class ThirdPartyRequest:
block: BlockBuilder
) -> ThirdPartyBlock: ...

def __new__ (
self,
base64=None
) -> ThirdPartyRequest: ...

# Third party block contents
class ThirdPartyBlock:
pass
42 changes: 41 additions & 1 deletion biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand Down Expand Up @@ -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)
32 changes: 31 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,12 +1064,42 @@ impl PyBlockBuilder {
}
}

/// Third party block request
#[pyclass(name = "ThirdPartyRequest")]
pub struct PyThirdPartyRequest(Option<ThirdPartyRequest>);

#[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<Self> {
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<String> {
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
Expand Down