#totp #service #api-bindings #authentication #2fa #version #2fas

lib2fas

Unofficial implementation of 2fas for Rust (as a library)

7 releases

0.1.2 Jan 13, 2025
0.1.1 Jan 13, 2025
0.0.4 Jan 10, 2025

#676 in Cryptography

44 downloads per month

MIT license

72KB
902 lines

lib2fas.rs

lib2fas is an unofficial implementation of the 2fas platform for Rust, providing tools for managing and interacting with two-factor authentication (2FA) services as a library.

Installation

Add lib2fas to your Cargo.toml:

cargo add lib2fas

Or add it manually with a specific version:

# Cargo.toml
[dependencies]
lib2fas = "x.y.z"

Usage

Loading and Querying 2FA Services

use lib2fas::load_services;

#[tokio::main]
async fn main() {
    // or `load_services_blocking` if you can't use async.
    let maybe_storage = load_services("path/to/services.2fas", Some("your-passphrase")).await;

    match maybe_storage {
        Ok(storage) => {
            println!("Loaded {} services", storage.len());

            if let Some(service) = storage.find_first("example-service") {
                println!("Found service: {}", service.name);

                if let Some(otp) = service.totp() {
                    println!("Current OTP: {}", otp);
                }

                if let Some(next_otp) = service.totp_next() {
                    println!("Next OTP: {}", next_otp);
                }

                if let Some(current_otp_u32) = service.totp_u32() {
                    println!("Current OTP (as number): {}", current_otp_u32);
                }
            } else {
                println!("Service not found.");
            }
        }
        Err(err) => {
            eprintln!("Failed to load services: {}", err);
        }
    }
}

Testing

Run tests with output to stdout:

cargo test -- --nocapture

Run tests with coverage output:

cargo tarpaulin --out Html

Documentation

For more detailed information, visit docs.rs/lib2fas.

Source Code

The source code is available on GitHub.

Dependencies

~14–24MB
~426K SLoC