Simple2FA
Simple2FA is a library to easily add two-factor authentication to your app.
use simple2fa;
use simple2fa::create_url_encoded_qrcode;
struct User {
pub otp_secret: String,
pub name: String,
}
fn main() {
let user = User {
name: "Marie Curie".to_string(),
otp_secret: simple2fa::generate_secret()
};
let qrcode_data_url = create_urlencoded_qrcode("My web app", user.name, user.otp_secret);
println!(r#"<!-- index.html -->
<img src="{}"/>
"#, qrcode_data_url);
if simple2fa.check_2fa_code(user.otp_secret, "<otp_code>") {
} else {
}
if simple2fa.check_2fa_code(user.otp_secret, "<otp_code>") {
} else {
}
}
Installation
Add simple2fa
to your Cargo.toml
:
[dependencies]
simple2fa = "0.1.0"
If you have cargo-edit
, you can use the command line:
cargo add simple2fa
Simple2FA is also available in other languages:
Development
Development commands are described in the Justfile
.
Appreciation
Thank you to:
- @fosskers for the Rust totp-lite library,
which this library depends on.
totp-lite
beat similar Rust libraries in benchmarking.
- The neon project, which makes creating Node libraries painless.
- The pyo3 project, which makes creating Python libraries painless.