Skip to content

Commit

Permalink
remove block() usage and C-API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ptondereau committed Nov 3, 2024
1 parent 4bb076c commit ca5d65c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 112 deletions.
2 changes: 1 addition & 1 deletion biscuit-auth/src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl Biscuit {
1 + self.blocks.len()
}

pub fn block(&self, index: usize) -> Result<Block, error::Token> {
pub(crate) fn block(&self, index: usize) -> Result<Block, error::Token> {
let block = if index == 0 {
proto_block_to_token_block(
&self.authority,
Expand Down
111 changes: 0 additions & 111 deletions biscuit-capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,117 +699,6 @@ pub unsafe extern "C" fn biscuit_block_count(biscuit: Option<&Biscuit>) -> usize
biscuit.0.block_count()
}

#[no_mangle]
pub unsafe extern "C" fn biscuit_block_fact_count(
biscuit: Option<&Biscuit>,
block_index: u32,
) -> usize {
if biscuit.is_none() {
update_last_error(Error::InvalidArgument);
return 0;
}

let biscuit = biscuit.unwrap();

let block = match biscuit.0.block(block_index as usize) {
Ok(block) => block,
Err(e) => {
update_last_error(e.into());
return 0;
}
};

block.facts.len()
}

#[no_mangle]
pub unsafe extern "C" fn biscuit_block_rule_count(
biscuit: Option<&Biscuit>,
block_index: u32,
) -> usize {
if biscuit.is_none() {
update_last_error(Error::InvalidArgument);
return 0;
}

let biscuit = biscuit.unwrap();

let block = match biscuit.0.block(block_index as usize) {
Ok(block) => block,
Err(e) => {
update_last_error(e.into());
return 0;
}
};

block.rules.len()
}

#[no_mangle]
pub unsafe extern "C" fn biscuit_block_check_count(
biscuit: Option<&Biscuit>,
block_index: u32,
) -> usize {
if biscuit.is_none() {
update_last_error(Error::InvalidArgument);
return 0;
}

let biscuit = biscuit.unwrap();

let block = match biscuit.0.block(block_index as usize) {
Ok(block) => block,
Err(e) => {
update_last_error(e.into());
return 0;
}
};

block.checks.len()
}

#[no_mangle]
pub unsafe extern "C" fn biscuit_block_context(
biscuit: Option<&Biscuit>,
block_index: u32,
) -> *mut c_char {
if biscuit.is_none() {
update_last_error(Error::InvalidArgument);
return std::ptr::null_mut();
}

let biscuit = biscuit.unwrap();

let block = if block_index == 0 {
match biscuit.0.block(0) {
Ok(b) => b,
Err(err) => {
update_last_error(Error::Biscuit(err));
return std::ptr::null_mut();
}
}
} else {
match biscuit.0.block(block_index as usize - 1) {
Ok(b) => b,
Err(err) => {
update_last_error(Error::Biscuit(err));
return std::ptr::null_mut();
}
}
};

match &block.context {
None => std::ptr::null_mut(),
Some(context) => match CString::new(context.clone()) {
Ok(s) => s.into_raw(),
Err(_) => {
update_last_error(Error::InvalidArgument);
std::ptr::null_mut()
}
},
}
}

#[no_mangle]
pub unsafe extern "C" fn create_block() -> Box<BlockBuilder> {
Box::new(BlockBuilder(biscuit_auth::builder::BlockBuilder::new()))
Expand Down
4 changes: 4 additions & 0 deletions biscuit-capi/tests/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ mod capi {
const char *biscuit_source = biscuit_print_block_source(b2, 0);
printf("biscuit block 0 source: %s\n", biscuit_source);

uintptr_t count = biscuit_block_count(b2);
printf("biscuit block count: %" PRIuPTR "\n", count);

free(buffer);
authorizer_free(authorizer);
block_builder_free(bb);
Expand Down Expand Up @@ -132,6 +135,7 @@ serialized size: 322
wrote 322 bytes
biscuit block 0 source: right("file1", "read");
biscuit block count: 2
"#,
);
}
Expand Down

0 comments on commit ca5d65c

Please sign in to comment.