1 个不稳定版本
0.1.0 | 2021年11月29日 |
---|
#9 in #urlencoded
13KB
89 行
Simple2FA
Simple2FA 是一个库,可轻松地将双因素认证添加到您的应用程序中。
use simple2fa;
use simple2fa::create_url_encoded_qrcode;
// This represnts a user in your database.
struct User {
pub otp_secret: String,
pub name: String,
}
fn main() {
// Save this to your database.
let user = User {
name: "Marie Curie".to_string(),
otp_secret: simple2fa::generate_secret()
};
// Use this function or `create_png_qrcode` to generate a png file.
let qrcode_data_url = create_urlencoded_qrcode("My web app", user.name, user.otp_secret);
// The QR code is a data url, so you can render it inline on a web page.
println!(r#"<!-- index.html -->
<img src="{}"/>
"#, qrcode_data_url);
// Ask the user to scan the QR code with an authenticator app,
// and request a code to confirm they have setup 2FA.
if simple2fa.check_2fa_code(user.otp_secret, "<otp_code>") {
// 2FA is setup!
} else {
// Something went wrong setting up 2FA. Have the user submit a code again.
}
// When the user logs in, validate their password, then respond with
// the user_id as a hidden input field and request a 2FA code.
if simple2fa.check_2fa_code(user.otp_secret, "<otp_code>") {
// Log in successful
} else {
// Log in failed. Ask user re-enter an otp code or reject their login attempt.
}
}
安装
将 simple2fa
添加到您的 Cargo.toml
[dependencies]
simple2fa = "0.1.0"
如果您有 cargo-edit
,您可以使用命令行
cargo add simple2fa
Simple2FA 也可用在其他语言中
开发
开发命令在 Justfile
中描述。
感谢
感谢
依赖项
~14MB
~83K SLoC