Skip to main content

paseto_v4_sodium/
lib.rs

1//! PASETO v4 (libsodium)
2//!
3//! ```
4//! use paseto_v4_sodium::{SignedToken, UnsignedToken, SecretKey, PublicKey};
5//! use paseto_json::{RegisteredClaims, Time, HasExpiry, FromIssuer, ForSubject, Validate};
6//! use std::time::Duration;
7//!
8//! // init libsodium
9//! paseto_v4_sodium::ensure_init().expect("libsodium should initialise successfully");
10//!
11//! // create a new keypair
12//! let secret_key = SecretKey::random().unwrap();
13//! let public_key = secret_key.public_key();
14//!
15//! // create a set of token claims
16//! let claims = RegisteredClaims::now(Duration::from_secs(3600))
17//!     .from_issuer("https://paseto.conrad.cafe/".to_string())
18//!     .for_subject("conradludgate".to_string());
19//!
20//! // create and sign a new token
21//! let signed_token = UnsignedToken::new(claims).sign(&secret_key).unwrap();
22//!
23//! // serialize the token.
24//! let token = signed_token.to_string();
25//! // "v4.public.eyJpc3MiOiJodHRwczovL3Bhc2V0by5jb25yYWQuY2FmZS8iLCJzdWIiOiJjb25yYWRsdWRnYXRlIiwiYXVkIjpudWxsLCJleHAiOiIyMDI1LTA5LTIwVDEyOjAxOjEzLjcyMjQ3OVoiLCJuYmYiOiIyMDI1LTA5LTIwVDExOjAxOjEzLjcyMjQ3OVoiLCJpYXQiOiIyMDI1LTA5LTIwVDExOjAxOjEzLjcyMjQ3OVoiLCJqdGkiOm51bGx9N7O1CAXQpQ3rpxhq6xFZt32z27VSL8suiek38-5W4LRGr1tDmKcP0_xrlp5-kdE6o7B_K8KU-6Fwmu0hzrkiDQ"
26//!
27//! // serialize the public key.
28//! let key = public_key.to_string();
29//! // "k4.public.xRPdFzRvXY-H-6L3S2I3_TmdMKu6XwLKLSR10lZ-yfk"
30//!
31//! // ...
32//!
33//! // parse the token
34//! let signed_token: SignedToken<RegisteredClaims> = token.parse().unwrap();
35//!
36//! // parse the key
37//! let public_key: PublicKey = key.parse().unwrap();
38//!
39//! // verify the token signature and validate the claims.
40//! let validation = Time::valid_now()
41//!     .and_then(HasExpiry)
42//!     .and_then(FromIssuer("https://paseto.conrad.cafe/"))
43//!     .and_then(ForSubject("conradludgate"));
44//! let verified_token = signed_token.verify(&public_key, &validation).unwrap();
45//! ```
46#![forbid(unsafe_code)]
47
48#[cfg(feature = "zeroize")]
49macro_rules! secret {
50    ($value:expr) => {
51        zeroize::Zeroizing::new($value)
52    };
53}
54#[cfg(not(feature = "zeroize"))]
55macro_rules! secret {
56    ($value:expr) => {
57        $value
58    };
59}
60
61/// Low level implementation primitives.
62pub mod core;
63
64pub use paseto_core::PasetoError;
65
66/// A token with publically readable data, but not yet verified
67pub type SignedToken<M, F = ()> = paseto_core::SignedToken<core::V4, M, F>;
68/// A token with secret data
69pub type EncryptedToken<M, F = ()> = paseto_core::EncryptedToken<core::V4, M, F>;
70/// A [`SignedToken`] that has been verified
71pub type UnsignedToken<M, F = ()> = paseto_core::UnsignedToken<core::V4, M, F>;
72/// An [`EncryptedToken`] that has been decrypted
73pub type UnencryptedToken<M, F = ()> = paseto_core::UnencryptedToken<core::V4, M, F>;
74
75/// Private key used for [`encryption`](UnencryptedToken::encrypt) and [`decryption`](EncryptedToken::decrypt)
76pub type LocalKey = paseto_core::LocalKey<core::V4>;
77/// Public key used for signature [`verification`](SignedToken::verify)
78pub type PublicKey = paseto_core::PublicKey<core::V4>;
79/// Private key used for token [`signing`](UnsignedToken::sign)
80pub type SecretKey = paseto_core::SecretKey<core::V4>;
81
82/// A short ID for a key.
83pub type KeyId<K> = paseto_core::paserk::KeyId<core::V4, K>;
84/// A plaintext encoding of a key.
85pub type KeyText<K> = paseto_core::paserk::KeyText<core::V4, K>;
86
87/// Public key used for [`sealing`](LocalKey::seal) a [`LocalKey`] for a recipient.
88pub type PkePublicKey = paseto_core::PkePublicKey<core::V4>;
89/// Private key used for [`unsealing`](SealedKey::unseal) a sealed [`LocalKey`].
90pub type PkeSecretKey = paseto_core::PkeSecretKey<core::V4>;
91
92/// An asymmetrically encrypted [`LocalKey`].
93pub type SealedKey = paseto_core::paserk::SealedKey<core::V4>;
94
95pub use libsodium_rs::ensure_init;