hessra_token/
attenuate.rs1extern crate biscuit_auth as biscuit;
2
3use biscuit::macros::block;
4use biscuit::{Biscuit, KeyPair, PublicKey};
5
6use crate::error::TokenError;
7
8pub fn add_service_node_attenuation(
26 token: Vec<u8>,
27 public_key: PublicKey,
28 service: &str,
29 node_key: &KeyPair,
30) -> Result<Vec<u8>, TokenError> {
31 let biscuit = Biscuit::from(&token, public_key).map_err(TokenError::biscuit_error)?;
32
33 let third_party_request = biscuit
35 .third_party_request()
36 .map_err(TokenError::biscuit_error)?;
37 let service_name = service.to_string();
38
39 let third_party_block = block!(
41 r#"
42 service({service_name});
43 "#
44 );
45
46 let third_party_block = third_party_request
48 .create_block(&node_key.private(), third_party_block)
49 .map_err(TokenError::biscuit_error)?;
50
51 let attenuated_biscuit = biscuit
53 .append_third_party(node_key.public(), third_party_block)
54 .map_err(TokenError::biscuit_error)?;
55
56 let attenuated_token = attenuated_biscuit
58 .to_vec()
59 .map_err(TokenError::biscuit_error)?;
60
61 Ok(attenuated_token)
62}