Optionnal serde support for smaller builds

This commit is contained in:
Cleo Rebert 2020-07-03 14:34:58 +02:00
parent 83c0ded335
commit d7f8790243
3 changed files with 31 additions and 12 deletions

View File

@ -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"

View File

@ -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
## 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"]
```

View File

@ -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");
}
}
}