diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 18:11:02 +0100 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 18:11:02 +0100 |
commit | 62d37c8d4e7d1caca21f23978198b721efc5973b (patch) | |
tree | cae2b5f81bb1245caef37cba80f934878978878c /examples/otp.rs | |
parent | 528e56a0ff759ea81b61eea368cf53b5540dc89a (diff) | |
parent | 43664ca3c449008e2859feb94e3142db3fa98625 (diff) | |
download | nitrokey-rs-62d37c8d4e7d1caca21f23978198b721efc5973b.tar.gz nitrokey-rs-62d37c8d4e7d1caca21f23978198b721efc5973b.tar.bz2 |
Merge branch 'release-0.5.0'
Diffstat (limited to 'examples/otp.rs')
-rw-r--r-- | examples/otp.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/otp.rs b/examples/otp.rs new file mode 100644 index 0000000..819de28 --- /dev/null +++ b/examples/otp.rs @@ -0,0 +1,43 @@ +// Copyright (C) 2020 Robin Krahl <robin.krahl@ireas.org> +// SPDX-License-Identifier: CC-0 + +//! Connects to a Nitrokey device, configures an TOTP slot and generates a one-time password from +//! it. + +use std::time; + +use nitrokey::{Authenticate, ConfigureOtp, Device, GenerateOtp}; + +fn main() -> Result<(), nitrokey::Error> { + let mut manager = nitrokey::take()?; + let device = manager.connect()?; + + // Configure the OTP slot (requires admin PIN) + let data = nitrokey::OtpSlotData::new( + 1, + "test", + "3132333435363738393031323334353637383930", + nitrokey::OtpMode::SixDigits, + ); + let mut admin = device.authenticate_admin("12345678")?; + admin.write_totp_slot(data, 30)?; + let mut device = admin.device(); + + // Set the time for the OTP generation + let time = time::SystemTime::now() + .duration_since(time::UNIX_EPOCH) + .expect("Invalid system time"); + device.set_time(time.as_secs(), true)?; + + // Generate a one-time password -- depending on the configuration, we have to set the user PIN + let config = device.get_config()?; + let otp = if config.user_password { + let user = device.authenticate_user("123456")?; + user.get_totp_code(1) + } else { + device.get_totp_code(1) + }?; + println!("Generated OTP code: {}", otp); + + Ok(()) +} |