Skip to content

Commit

Permalink
chore(FLOW-2123): Add examples
Browse files Browse the repository at this point in the history
Signed-off-by: Cléo REBERT <[email protected]>
  • Loading branch information
constantoine committed Jan 6, 2023
1 parent cc1474a commit d54bd9e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "totp-rs"
version = "4.0.0"
version = "4.1.0"
authors = ["Cleo Rebert <[email protected]>"]
rust-version = "1.59"
edition = "2021"
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down Expand Up @@ -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 changes: 46 additions & 0 deletions examples/steam.rs
Original file line number Diff line number Diff line change
@@ -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() {

}
1 change: 1 addition & 0 deletions src/custom_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl TOTP {

#[cfg(all(test, feature = "steam"))]
mod test {
#[cfg(feature = "otpauth")]
use super::*;

#[test]
Expand Down

0 comments on commit d54bd9e

Please sign in to comment.