Commit d7f87902436fe8f84edfa705c62b65407f93b9dc

Cleo Rebert 2020-07-03T14:34:58

Optionnal serde support for smaller builds

diff --git a/Cargo.toml b/Cargo.toml
index 60973da..60a4d2a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "totp-rs"
-version = "0.4.1"
+version = "0.5.0"
 authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
 edition = "2018"
 readme = "README.md"
@@ -12,14 +12,15 @@ keywords = ["authentication", "2fa", "totp", "hmac", "otp"]
 categories = ["authentication", "web-programming"]
 
 [package.metadata.docs.rs]
-features = [ "qr" ]
+features = [ "qr", "serde_support" ]
 
 [features]
 default = []
 qr = ["qrcode", "image", "base64"]
+serde_support = ["serde"]
 
 [dependencies]
-serde = { version = "1.0", features = ["derive"] }
+serde = { version = "1.0", features = ["derive"], optional = true }
 sha2 = "0.9.0"
 sha-1 = "0.9.0"
 hmac = "0.8.0"
diff --git a/README.md b/README.md
index 226b101..74f9bbd 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,21 @@
 # totp-rs
 ![Build Status](https://github.com/constantoine/totp-rs/workflows/Rust/badge.svg) ![docs](https://docs.rs/totp-rs/badge.svg)
 
-This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! With additional feature "qr", you can use it to generate a base64 png qrcode.
+This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as low-dependency as possible to ensure small binaries and short compilation time
 
-## How to use
+## Features
+---
+### qr
+With optionnal feature "qr", you can use it to generate a base64 png qrcode
+### serde_support
+With optionnal feature "serde_support", library-defined types will be Deserialize-able and Serialize-able
 
+## How to use
+---
 Add it to your `Cargo.toml`:
 ```toml
 [dependencies]
-totp-rs = "~0.4"
+totp-rs = "~0.5"
 ```
 You can then do something like:
 ```Rust
@@ -36,7 +43,7 @@ println!("{}", token);
 Add it to your `Cargo.toml`:
 ```toml
 [dependencies.totp-rs]
-version = "~0.4"
+version = "~0.5"
 features = ["qr"]
 ```
 You can then do something like:
@@ -53,3 +60,11 @@ let totp = TOTP::new(
 let code = totp.get_qr("user@example.com", "my-org.com")?;
 println!("{}", code);
 ```
+
+### With serde support
+Add it to your `Cargo.toml`:
+```toml
+[dependencies.totp-rs]
+version = "~0.5"
+features = ["serde_support"]
+```
diff --git a/src/lib.rs b/src/lib.rs
index 1c7b54f..b0334dc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,11 +1,11 @@
-//! This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! With additional feature "qr", you can use it to generate a base64 png qrcode.
+//! This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as low-dependency as possible to ensure small binaries and short compilation time
 //!
 //! # Examples
 //!
 //! ```rust
 //! use std::time::SystemTime;
 //! use totp_rs::{Algorithm, TOTP};
-//! 
+//!
 //! let totp = TOTP::new(
 //!     Algorithm::SHA1,
 //!     6,
@@ -36,6 +36,7 @@
 //! println!("{}", code);
 //! ```
 
+#[cfg(feature = "serde_support")]
 use serde::{Deserialize, Serialize};
 
 use base32;
@@ -55,7 +56,8 @@ type HmacSha256 = Hmac<Sha256>;
 type HmacSha512 = Hmac<Sha512>;
 
 /// Algorithm enum holds the three standards algorithms for TOTP as per the [reference implementation](https://tools.ietf.org/html/rfc6238#appendix-A)
-#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
+#[derive(Debug, Copy, Clone)]
+#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
 pub enum Algorithm {
     SHA1,
     SHA256,
@@ -63,7 +65,8 @@ pub enum Algorithm {
 }
 
 /// TOTP holds informations as to how to generate an auth code and validate it. Its [secret](struct.TOTP.html#structfield.secret) field is sensitive data, treat it accordingly
-#[derive(Debug, Serialize, Deserialize, Clone)]
+#[derive(Debug, Clone)]
+#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
 pub struct TOTP {
     /// SHA-1 is the most widespread algorithm used, and for totp pursposes, SHA-1 hash collisions are [not a problem](https://tools.ietf.org/html/rfc4226#appendix-B.2) as HMAC-SHA-1 is not impacted. It's also the main one cited in [rfc-6238](https://tools.ietf.org/html/rfc6238#section-3) even though the [reference implementation](https://tools.ietf.org/html/rfc6238#appendix-A) permits the use of SHA-1, SHA-256 and SHA-512. Not all clients support other algorithms then SHA-1
     pub algorithm: Algorithm,
@@ -245,4 +248,4 @@ mod tests {
         let hash_digest = Sha1::digest(qr.as_bytes());
         assert_eq!(format!("{:x}", hash_digest).as_str(), "3abc0127e7a2b1013fb25c97ef14422c1fe9e878");
     }
-}
\ No newline at end of file
+}