Skip to content

Commit

Permalink
Merge pull request #254 from biscuit-auth/top-level-exports
Browse files Browse the repository at this point in the history
API improvements
  • Loading branch information
divarvel authored Dec 1, 2024
2 parents 0dab828 + 3624478 commit 3bd7a7b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
6 changes: 3 additions & 3 deletions biscuit-auth/src/bwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests {

#[test]
fn roundtrips() {
let keypair = KeyPair::new(Algorithm::Ed25519);
let keypair = KeyPair::new();
let bwk = BiscuitWebKey {
public_key: keypair.public(),
key_id: 12,
Expand All @@ -74,7 +74,7 @@ mod tests {
let parsed: BiscuitWebKey = serde_json::from_str(&serialized).unwrap();
assert_eq!(parsed, bwk);

let keypair = KeyPair::new(Algorithm::Secp256r1);
let keypair = KeyPair::new_with_algorithm(Algorithm::Secp256r1);
let bwk = BiscuitWebKey {
public_key: keypair.public(),
key_id: 0,
Expand All @@ -86,7 +86,7 @@ mod tests {
let parsed: BiscuitWebKey = serde_json::from_str(&serialized).unwrap();
assert_eq!(parsed, bwk);

let keypair = KeyPair::new(Algorithm::Ed25519);
let keypair = KeyPair::new();
let bwk = BiscuitWebKey {
public_key: keypair.public(),
key_id: 0,
Expand Down
10 changes: 8 additions & 2 deletions biscuit-auth/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ pub enum KeyPair {
}

impl KeyPair {
pub fn new(algorithm: Algorithm) -> Self {
/// Create a new ed25519 keypair with the default OS RNG
pub fn new() -> Self {
Self::new_with_rng(Algorithm::Ed25519, &mut rand::rngs::OsRng)
}

/// Create a new keypair with a chosen algorithm and the default OS RNG
pub fn new_with_algorithm(algorithm: Algorithm) -> Self {
Self::new_with_rng(algorithm, &mut rand::rngs::OsRng)
}

Expand Down Expand Up @@ -107,7 +113,7 @@ impl KeyPair {

impl std::default::Default for KeyPair {
fn default() -> Self {
Self::new(Algorithm::Ed25519)
Self::new()
}
}

Expand Down
30 changes: 15 additions & 15 deletions biscuit-auth/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ mod tests {
fn test_block_signature_version() {
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(),
&KeyPair::new(),
&None,
&Some(DATALOG_3_1),
std::iter::empty()
Expand All @@ -629,8 +629,8 @@ mod tests {
);
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Secp256r1),
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new_with_algorithm(Algorithm::Secp256r1),
&KeyPair::new_with_algorithm(Algorithm::Ed25519),
&None,
&Some(DATALOG_3_1),
std::iter::empty()
Expand All @@ -640,8 +640,8 @@ mod tests {
);
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(Algorithm::Secp256r1),
&KeyPair::new_with_algorithm(Algorithm::Ed25519),
&KeyPair::new_with_algorithm(Algorithm::Secp256r1),
&None,
&Some(DATALOG_3_1),
std::iter::empty()
Expand All @@ -651,8 +651,8 @@ mod tests {
);
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Secp256r1),
&KeyPair::new(Algorithm::Secp256r1),
&KeyPair::new_with_algorithm(Algorithm::Secp256r1),
&KeyPair::new_with_algorithm(Algorithm::Secp256r1),
&None,
&Some(DATALOG_3_1),
std::iter::empty()
Expand All @@ -662,10 +662,10 @@ mod tests {
);
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(),
&KeyPair::new(),
&Some(ExternalSignature {
public_key: KeyPair::new(Algorithm::Ed25519).public(),
public_key: KeyPair::new().public(),
signature: Signature::from_vec(Vec::new())
}),
&Some(DATALOG_3_1),
Expand All @@ -676,8 +676,8 @@ mod tests {
);
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(),
&KeyPair::new(),
&None,
&Some(DATALOG_3_3),
std::iter::empty()
Expand All @@ -687,8 +687,8 @@ mod tests {
);
assert_eq!(
block_signature_version(
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(Algorithm::Ed25519),
&KeyPair::new(),
&KeyPair::new(),
&None,
&Some(DATALOG_3_1),
std::iter::once(1)
Expand Down
3 changes: 2 additions & 1 deletion biscuit-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! fn main() -> Result<(), error::Token> {
//! // let's generate the root key pair. The root public key will be necessary
//! // to verify the token
//! let root = KeyPair::new(Algorithm::Ed25519);
//! let root = KeyPair::new();
//! let public_key = root.public();
//!
//! // creating a first token
Expand Down Expand Up @@ -232,6 +232,7 @@ mod token;
pub use crypto::{KeyPair, PrivateKey, PublicKey};
pub use token::authorizer::{Authorizer, AuthorizerLimits};
pub use token::builder;
pub use token::builder::{Algorithm, AuthorizerBuilder, BiscuitBuilder, BlockBuilder};
pub use token::builder_ext;
pub use token::unverified::UnverifiedBiscuit;
pub use token::Biscuit;
Expand Down
9 changes: 3 additions & 6 deletions biscuit-auth/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
//!
//! ```rust
//! use biscuit_auth::KeyPair;
//! use biscuit_auth::builder::Algorithm;
//! use biscuit_auth::macros::{authorizer, biscuit, block};
//! use std::time::{Duration, SystemTime};
//!
//! let root = KeyPair::new(Algorithm::Ed25519);
//! let root = KeyPair::new();
//!
//! let user_id = "1234";
//! let biscuit = biscuit!(
Expand Down Expand Up @@ -97,11 +96,10 @@ pub use biscuit_quote::authorizer_merge;
///
/// ```rust
/// use biscuit_auth::{Biscuit, KeyPair};
/// use biscuit_auth::builder::Algorithm;
/// use biscuit_auth::macros::biscuit;
/// use std::time::{SystemTime, Duration};
///
/// let root = KeyPair::new(Algorithm::Ed25519);
/// let root = KeyPair::new();
/// let biscuit = biscuit!(
/// r#"
/// user({user_id});
Expand All @@ -119,11 +117,10 @@ pub use biscuit_quote::biscuit;
///
/// ```rust
/// use biscuit_auth::{Biscuit, KeyPair};
/// use biscuit_auth::builder::Algorithm;
/// use biscuit_auth::macros::{biscuit, biscuit_merge};
/// use std::time::{SystemTime, Duration};
///
/// let root = KeyPair::new(Algorithm::Ed25519);
/// let root = KeyPair::new();
///
/// let mut b = biscuit!(
/// r#"
Expand Down
20 changes: 9 additions & 11 deletions biscuit-auth/src/token/authorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ impl Authorizer {
/// ```rust
/// # use biscuit_auth::KeyPair;
/// # use biscuit_auth::Biscuit;
/// # use biscuit_auth::builder::Algorithm;
/// let keypair = KeyPair::new(Algorithm::Ed25519);
/// let keypair = KeyPair::new();
/// let biscuit = Biscuit::builder()
/// .fact("user(\"John Doe\", 42)")
/// .expect("parse error")
Expand Down Expand Up @@ -199,8 +198,7 @@ impl Authorizer {
/// ```rust
/// # use biscuit_auth::KeyPair;
/// # use biscuit_auth::Biscuit;
/// # use biscuit_auth::builder::Algorithm;
/// let keypair = KeyPair::new(Algorithm::Ed25519,);
/// let keypair = KeyPair::new();
/// let biscuit = Biscuit::builder()
/// .fact("user(\"John Doe\", 42)")
/// .expect("parse error")
Expand Down Expand Up @@ -861,7 +859,7 @@ mod tests {

use crate::PublicKey;
use crate::{
builder::{Algorithm, BiscuitBuilder, BlockBuilder},
builder::{BiscuitBuilder, BlockBuilder},
KeyPair,
};

Expand Down Expand Up @@ -995,7 +993,7 @@ mod tests {
fn query_authorizer_from_token_tuple() {
use crate::Biscuit;
use crate::KeyPair;
let keypair = KeyPair::new(Algorithm::Ed25519);
let keypair = KeyPair::new();
let biscuit = Biscuit::builder()
.fact("user(\"John Doe\", 42)")
.unwrap()
Expand All @@ -1016,7 +1014,7 @@ mod tests {
fn query_authorizer_from_token_string() {
use crate::Biscuit;
use crate::KeyPair;
let keypair = KeyPair::new(Algorithm::Ed25519);
let keypair = KeyPair::new();
let biscuit = Biscuit::builder()
.fact("user(\"John Doe\")")
.unwrap()
Expand All @@ -1032,8 +1030,8 @@ mod tests {

#[test]
fn authorizer_with_scopes() {
let root = KeyPair::new(Algorithm::Ed25519);
let external = KeyPair::new(Algorithm::Ed25519);
let root = KeyPair::new();
let external = KeyPair::new();

let mut scope_params = HashMap::new();
scope_params.insert("external_pub".to_string(), external.public());
Expand Down Expand Up @@ -1065,7 +1063,7 @@ mod tests {
let biscuit2 = Biscuit::from(serialized, root.public()).unwrap();

let builder = AuthorizerBuilder::new();
let external2 = KeyPair::new(Algorithm::Ed25519);
let external2 = KeyPair::new();

let mut scope_params = HashMap::new();
scope_params.insert("external".to_string(), external.public());
Expand Down Expand Up @@ -1213,7 +1211,7 @@ mod tests {

#[test]
fn authorizer_display_before_and_after_authorization() {
let root = KeyPair::new(Algorithm::Ed25519);
let root = KeyPair::new();

let token = BiscuitBuilder::new()
.code(
Expand Down
2 changes: 1 addition & 1 deletion biscuit-auth/src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn default_symbol_table() -> SymbolTable {
/// use biscuit::{KeyPair, Biscuit, builder::*, builder_ext::*};
///
/// fn main() -> Result<(), biscuit::error::Token> {
/// let root = KeyPair::new(Algorithm::Ed25519);
/// let root = KeyPair::new();
///
/// // first we define the authority block for global data,
/// // like access rights
Expand Down
2 changes: 1 addition & 1 deletion biscuit-auth/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ fn policy_macro() {

#[test]
fn json() {
let key_pair = KeyPair::new(biscuit_auth::builder::Algorithm::Ed25519);
let key_pair = KeyPair::new();
let biscuit = biscuit!(r#"user(123)"#).build(&key_pair).unwrap();

let value: serde_json::Value = json!(
Expand Down

0 comments on commit 3bd7a7b

Please sign in to comment.