chore(FLOW-2123): Add examples

Signed-off-by: Cléo REBERT <cleo.rebert-ext@treezor.com>
This commit is contained in:
Cléo REBERT 2023-01-06 14:35:18 +01:00
parent cc1474a7d8
commit d54bd9e447
No known key found for this signature in database
GPG Key ID: 0FA097951CF65367
4 changed files with 70 additions and 1 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "totp-rs"
version = "4.0.0"
version = "4.1.0"
authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
rust-version = "1.59"
edition = "2021"

View File

@ -34,6 +34,7 @@ Add support for Steam TOTP tokens.
4. [Enable otpauth url support](#with-otpauth-url-support)
5. [Enable gen_secret support](#with-gensecret)
6. [With RFC-6238 compliant default](#with-rfc-6238-compliant-default)
7. [New TOTP from steam secret](#new-totp-from-steam-secret)
### Understanding Secret
---
@ -216,3 +217,24 @@ fn main() {
println!("code: {}", code);
}
```
### New TOTP from steam secret
---
Add it to your `Cargo.toml`:
```toml
[dependencies.totp-rs]
version = "^4.1"
features = ["qr"]
```
You can then do something like:
```Rust
use totp_rs::{TOTP, Secret};
fn main() {
let totp = TOTP::new_steam(
Secret::Encoded("KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ".to_string()).to_bytes().unwrap(),
).unwrap();
let code = totp.get_qr()?;
println!("{}", code);
}
```

46
examples/steam.rs Normal file
View File

@ -0,0 +1,46 @@
#[cfg(feature = "steam")]
use totp_rs::{Secret, TOTP};
#[cfg(feature = "steam")]
#[cfg(feature = "otpauth")]
fn main() {
// create TOTP from base32 secret
let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
let totp_b32 = TOTP::new_steam(
secret_b32.to_bytes().unwrap(),
"user-account".to_string(),
);
println!(
"base32 {} ; raw {}",
secret_b32,
secret_b32.to_raw().unwrap()
);
println!(
"code from base32:\t{}",
totp_b32.generate_current().unwrap()
);
}
#[cfg(feature = "steam")]
#[cfg(not(feature = "otpauth"))]
fn main() {
// create TOTP from base32 secret
let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
let totp_b32 = TOTP::new_steam(secret_b32.to_bytes().unwrap());
println!(
"base32 {} ; raw {}",
secret_b32,
secret_b32.to_raw().unwrap()
);
println!(
"code from base32:\t{}",
totp_b32.generate_current().unwrap()
);
}
#[cfg(not(feature = "steam"))]
fn main() {
}

View File

@ -44,6 +44,7 @@ impl TOTP {
#[cfg(all(test, feature = "steam"))]
mod test {
#[cfg(feature = "otpauth")]
use super::*;
#[test]