Crate easy_totp

Crate easy_totp 

Source
Expand description

§easy_totp

TOTP in Rust. Made easy.

A lightweight library for generating and verifying Time‑Based One‑Time Passwords (TOTP) in Rust.

§Warnings

§Handle secrets with caution

It is crucial to handle these secrets with care. Exposing your TOTP secrets can compromise the security of your accounts. Always ensure that your secrets are stored securely and are not shared or logged inappropriately. Remember, if you use the QR code for onboarding, anyone with access to that QR code can generate valid TOTP codes for your account.

§Never send TOTP codes from the server to the client.

It’s okay to send a code from the client to the server for verification, but never the other way around.

§Consider rate limiting

To enhance security, consider implementing rate limiting on TOTP verification attempts. This can help prevent brute-force attacks.

§Features

  • Create TOTP instances with random secret keys.
  • Create TOTP onboarding QR codes.
    • PNG format.
    • Terminal display.
  • Generate/verify TOTP codes.

§Documentation

The documentation for easy_totp can be found at docs.rs/easy_totp.

§Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

By contributing to this project, you agree to have your contributions licensed under the project’s overall license: MIT. easy_totp is a crate designed to make it easy to integrate TOTP into your Rust apps.

BEWARE: handle secrets with caution

§Creating a QR code for TOTP setup

use easy_totp::EasyTotp;

let issuer = Some(String::from("McCormick"));
let account_name = String::from("test@test-email.com");

let et = EasyTotp::new(issuer, account_name).unwrap();

let my_qr_code = et.create_qr_png();

§Saving that QR code to a file

use easy_totp::EasyTotp;
use std::fs;
use std::io::Write;

let issuer = Some(String::from("McCormick"));
let account_name = String::from("test@test-email.com");
let filename = "./test_images/qr_code.png";

let et = EasyTotp::new(issuer, account_name).unwrap();

let my_qr_code = et.create_qr_png();

match my_qr_code {
    Ok(png_data) => {
        let mut file = fs::File::create(filename).unwrap();
        file.write_all(&png_data).unwrap();
        println!("QR code saved as 'qr_code.png'");
    }
    Err(e) => {
        panic!("Error creating QR code: {:?}", e);
    }
}

§Generating TOTP codes for authentication

use easy_totp::EasyTotp;

let issuer = Some(String::from("McCormick"));
let account_name = String::from("test@test-email.com");

let et = EasyTotp::new(issuer, account_name).unwrap();

let token = et.generate_token().unwrap();

Structs§

EasyTotp
EasyTotp is a unit-struct to keep track of externally-implemented code.

Enums§

QRColorMode
QRColorMode defines whether the QR code is rendered in direct or inverted colors For light mode, use Direct; for dark mode, use Inverted. Some QR scanners may still be able to read either way.
TerminalQRSize
TerminalQRSize defines whether the QR code is rendered in full size or mini size for terminal display Full size uses standard block characters, while mini size uses half-block characters to reduce height