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

UnverifiedBiscuit.external_public_keys() now returns PublicKeys #263

Merged
merged 2 commits into from
Jan 27, 2025
Merged
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
1 change: 1 addition & 0 deletions biscuit-auth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Support for P256 signatures (#108)
- `query_exactly_once()` (#260) (Baran Yildirim)
- include algorithm prefix in public/private key strings (#261)
- `UnverifiedBiscuit.external_public_keys()` now returns `PublicKey`s, not byte vecs (#263)

# `5.0.0`

Expand Down
50 changes: 43 additions & 7 deletions biscuit-auth/src/token/unverified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,11 @@ impl UnverifiedBiscuit {
/// Blocks carrying an external public key are _third-party blocks_
/// and their contents can be trusted as coming from the holder of
/// the corresponding private key
pub fn external_public_keys(&self) -> Vec<Option<Vec<u8>>> {
pub fn external_public_keys(&self) -> Vec<Option<PublicKey>> {
let mut res = vec![None];

for block in self.container.blocks.iter() {
res.push(
block
.external_signature
.as_ref()
.map(|sig| sig.public_key.to_bytes().to_vec()),
);
res.push(block.external_signature.as_ref().map(|sig| sig.public_key));
}

res
Expand Down Expand Up @@ -379,3 +374,44 @@ impl UnverifiedBiscuit {
self.append_third_party(&decoded)
}
}

#[cfg(test)]
mod tests {
use crate::{BiscuitBuilder, BlockBuilder, KeyPair};

use super::UnverifiedBiscuit;

#[test]
fn consistent_with_biscuit() {
let root_key = KeyPair::new();
let external_key = KeyPair::new();
let biscuit = BiscuitBuilder::new()
.fact("test(true)")
.unwrap()
.build(&root_key)
.unwrap()
.append(BlockBuilder::new().fact("test(false)").unwrap())
.unwrap();
let req = biscuit.third_party_request().unwrap();
let res = req
.create_block(
&external_key.private(),
BlockBuilder::new().fact("third_party(true)").unwrap(),
)
.unwrap();
let biscuit = biscuit
.append_third_party(external_key.public(), res)
.unwrap();

let unverified = UnverifiedBiscuit::from_base64(biscuit.to_base64().unwrap()).unwrap();

unverified.clone().verify(root_key.public()).unwrap();

assert_eq!(unverified.blocks, biscuit.blocks);

assert_eq!(
unverified.external_public_keys(),
biscuit.external_public_keys()
);
}
}
Loading