Additional feature!

This commit is contained in:
Cleo Rebert 2020-06-21 15:57:16 +02:00
parent bba8e818da
commit 32b618c827
4 changed files with 99 additions and 19 deletions

View File

@ -1,16 +1,21 @@
[package]
name = "totp-rs"
version = "0.2.6"
version = "0.3.0"
authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
edition = "2018"
readme = "README.md"
license = "MIT"
description = "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."
description = "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."
repository = "https://github.com/constantoine/totp-rs"
homepage = "https://github.com/constantoine/totp-rs"
keywords = ["authentification", "2fa", "totp", "hmac"]
categories = ["authentication", "web-programming"]
[features]
default = []
qr = ["qrcode", "image", "base64"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.9.0"
@ -18,6 +23,6 @@ sha-1 = "0.9.0"
hmac = "0.8.0"
byteorder = ">= 1.3"
base32 = ">= 0.4"
qrcode = ">= 0.12"
image = ">= 0.23"
base64 = ">= 0.12"
qrcode = { version = ">= 0.12", optional = true }
image = { version = ">= 0.23", optional = true}
base64 = { version = ">= 0.12", optional = true }

View File

@ -1,24 +1,56 @@
# totp-rs
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!
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.
## How to use
Add it to your `Cargo.toml`:
```toml
[dependencies]
totp-rs = "~0.2"
totp-rs = "~0.3"
```
You can then do something like:
```Rust
use totp_rs::{TOTP, Algorithm};
use std::time::SystemTime;
use totp_rs::{Algorithm, TOTP};
let username = "example".to_string();
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "supersecret".to_string().into_bytes());
let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
let totp = TOTP::new(
Algorithm::SHA1,
6,
1,
30,
"supersecret".to_string().into_bytes(),
);
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH).unwrap()
.as_secs();
let url = totp.get_url(format!("account:{}", username), "my-org.com".to_string());
println!("{}", url);
let token = totp.generate(time);
println!("{}", token);
```
### With qrcode generation
Add it to your `Cargo.toml`:
```toml
[dependencies.totp-rs]
version = "~0.3"
features = ["qr"]
```
You can then do something like:
```Rust
use totp_rs::{Algorithm, TOTP};
let username = "example".to_string();
let totp = TOTP::new(
Algorithm::SHA1,
6,
1,
30,
"supersecret".to_string().into_bytes(),
);
let code = totp.get_qr(format!("account:{}", username), "my-org.com".to_string())?;
println!("{}", code);
```

View File

@ -1,30 +1,52 @@
//! 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!
//! 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.
//!
//! # Examples
//!
//! ```
//! use totp_rs::{TOTP, Algorithm};
//! use std::time::SystemTime;
//!
//! use totp_rs::{Algorithm, TOTP};
//!
//! let username = "example".to_string();
//! let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "supersecret".to_string().into_bytes());
//! let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
//! let totp = TOTP::new(
//! Algorithm::SHA1,
//! 6,
//! 1,
//! 30,
//! "supersecret".to_string().into_bytes(),
//! );
//! let time = SystemTime::now()
//! .duration_since(SystemTime::UNIX_EPOCH).unwrap()
//! .as_secs();
//! let url = totp.get_url(format!("account:{}", username), "my-org.com".to_string());
//! println!("{}", url);
//! let token = totp.generate(time);
//! println!("{}", token);
//! ```
//!
//! ```
//! use totp_rs::{Algorithm, TOTP};
//!
//! let username = "example".to_string();
//! let totp = TOTP::new(
//! Algorithm::SHA1,
//! 6,
//! 1,
//! 30,
//! "supersecret".to_string().into_bytes(),
//! );
//! let code = totp.get_qr(format!("account:{}", username), "my-org.com".to_string())?;
//! println!("{}", code);
//! ```
use serde::{Deserialize, Serialize};
use base32;
use base64;
use byteorder::{BigEndian, ReadBytesExt};
use std::io::Cursor;
use image::Luma;
use qrcode::QrCode;
#[cfg(feature = "qr")]
use {base64, image::Luma, qrcode::QrCode};
use hmac::{Hmac, Mac, NewMac};
use sha1::Sha1;
@ -135,13 +157,14 @@ impl TOTP {
)
}
/// Will return a qrcode to automatically add a TOTP as a base64 string
/// Will return a qrcode to automatically add a TOTP as a base64 string. Needs feature "qr" to be set. It is by default
///
/// # Errors
///
/// This will return an error in case the URL gets too long to encode into a QR code
///
/// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly
#[cfg(feature = "qr")]
pub fn get_qr(
&self,
label: String,

20
src/main.rs Normal file
View File

@ -0,0 +1,20 @@
use std::time::SystemTime;
use totp_rs::{Algorithm, TOTP};
fn main() {
let username = "example".to_string();
let totp = TOTP::new(
Algorithm::SHA1,
6,
1,
30,
"supersecret".to_string().into_bytes(),
);
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH).unwrap()
.as_secs();
let url = totp.get_url(format!("account:{}", username), "my-org.com".to_string());
println!("{}", url);
let token = totp.generate(time);
println!("{}", token);
}