diff --git a/Cargo.toml b/Cargo.toml index f5a746f..55e932f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "paseto_maker" authors = ["itsscb "] license = "GPL-3.0" -version = "0.1.0" +version = "0.2.0" edition = "2021" repository = "https://github.com/itsscb/paseto_maker" description = "This library provides high-level functionality for creating, handling, and managing PASETO tokens." diff --git a/src/lib.rs b/src/lib.rs index f9f9411..02c0666 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,21 +6,13 @@ //! This library includes modules for defining claims, handling errors, and creating/verifying PASETO tokens. //! It leverages the `rusty_paseto` crate and currently supports PASETO Tokens V4.public. //! -//! # Modules -//! - `claims`: Defines the structure and behavior of the claims that can be embedded in a PASETO token. -//! - `errors`: Provides error types and handling mechanisms for the library. -//! - `maker`: Contains the logic for creating and verifying PASETO tokens. -//! -//! # Re-exports -//! - `Claims`: The struct representing the claims in a PASETO token. -//! - `Maker`: The struct used for creating and verifying PASETO tokens. -//! //! # Usage Example //! ```rust //! use paseto_maker::{Maker, Claims, version::V4, purpose::Public}; //! //! fn main() -> Result<(), Box> { -//! let maker = Maker::new_with_keypair().unwrap(); +//! let (priv_key, _) = Maker::new_keypair(); +//! let maker = Maker::new(&priv_key).expect("failed to create maker"); //! let claims = Claims::new().with_subject("example"); //! let token = maker.create_token(&claims).unwrap(); //! println!("Token: {}", token); @@ -31,16 +23,10 @@ //! } //! ``` //! -//! The `claims` module defines the structure and behavior of the claims that can be embedded in a PASETO token. -//! The `errors` module provides error types and handling mechanisms for the library. -//! The `maker` module contains the logic for creating and verifying PASETO tokens. -//! -//! The `Claims` struct and `Maker` struct are re-exported for ease of use. -//! //! This library uses the `rusty_paseto` crate underneath and currently only supports PASETO Tokens V4.public. -pub(crate) mod claims; -pub mod errors; -pub(crate) mod maker; +mod claims; +mod errors; +mod maker; pub use claims::Claims; pub use maker::Maker; diff --git a/src/maker/mod.rs b/src/maker/mod.rs index 7e190a2..da947d6 100644 --- a/src/maker/mod.rs +++ b/src/maker/mod.rs @@ -35,22 +35,19 @@ pub struct Maker { /// /// - `new(private_key: &[u8; 64]) -> Self` /// - Creates a new `Maker` instance with the given private and public keys. -/// - `new_with_keypair() -> Self` -/// - Generates a new keypair and creates a new `Maker` instance with the generated keys. /// - `new_keypair() -> ([u8; 64], [u8; 32])` /// - Generates a new Ed25519 keypair and returns the private and public keys. -/// - `private_key(&self) -> PasetoAsymmetricPrivateKey` -/// - Returns the private key as a `PasetoAsymmetricPrivateKey`. -/// - `public_key(&self) -> PasetoAsymmetricPublicKey` -/// - Returns the public key as a `PasetoAsymmetricPublicKey`. /// - `create_token(&self, claims: &Claims) -> Result` /// - Creates a new PASETO token with the given claims. Returns the token as a `String` or an error if the token creation fails. +/// - `verify_token(&self, token: &str) -> Result` +/// - Verifies a PASETO token. Returns the containing Claims or an error if the token verification fails. /// /// # Example /// /// ```rust /// use paseto_maker::{Maker, Claims, version::V4, purpose::Public}; -/// let maker = Maker::new_with_keypair().unwrap(); +/// let (priv_key, _) = Maker::new_keypair(); +/// let maker = Maker::new(&priv_key).expect("failed to create maker"); /// let claims = Claims::new(); /// let token = maker.create_token(&claims).unwrap(); /// ``` @@ -73,22 +70,6 @@ impl Maker { }) } - /// # Errors - /// - /// This function will return an error if the key generation or Maker creation fails. - pub fn new_with_keypair() -> Result { - // let (private_key, public_key) = Self::new_keypair(); - let private_key = Self::new_private_key(); - Self::new(&private_key) - } - - #[must_use] - pub fn new_private_key() -> [u8; 64] { - let mut csprng = rand::rngs::OsRng; - let priv_key: ed25519_dalek::SigningKey = ed25519_dalek::SigningKey::generate(&mut csprng); - priv_key.to_keypair_bytes() - } - #[must_use] pub fn new_keypair() -> ([u8; 64], [u8; 32]) { let mut csprng = rand::rngs::OsRng; @@ -101,8 +82,7 @@ impl Maker { PasetoAsymmetricPrivateKey::::from(&self.private_key) } - #[must_use] - pub fn public_key(&self) -> PasetoAsymmetricPublicKey { + fn public_key(&self) -> PasetoAsymmetricPublicKey { PasetoAsymmetricPublicKey::::from(&self.public_key) } @@ -251,7 +231,8 @@ mod test { #[test] fn test_invalid_claims() { - let maker = Maker::new_with_keypair().expect("failed to create maker"); + let (priv_key, _) = Maker::new_keypair(); + let maker = Maker::new(&priv_key).expect("failed to create maker"); let claims = Claims::new().with_issued_at("invalid RF3339 date"); let token = maker.create_token(&claims); @@ -260,7 +241,9 @@ mod test { #[test] fn test_create_token() { - let maker = Maker::new_with_keypair().expect("failed to create maker"); + let (priv_key, _) = Maker::new_keypair(); + let maker = Maker::new(&priv_key).expect("failed to create maker"); + let public_key = maker.public_key(); let mut claims = Claims::new().with_issued_at("2027-09-18T03:42:15+02:00"); claims.set_claim("sub", "this is the subject").unwrap();