Optionnal serde support for smaller builds
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
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
+}