aboutsummaryrefslogtreecommitdiff
path: root/nitrokey/examples
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-14 19:21:26 +0100
committerDaniel Mueller <deso@posteo.net>2020-01-15 12:08:15 -0800
commit1e07212370806f3ea4ff7a6f66e5716b60ab1f77 (patch)
treec562f40b9888b9daf62a9d9a42eeef8bfa35413f /nitrokey/examples
parent5251aa2d1665ee12245d5d5fe09c1f207272978f (diff)
downloadnitrocli-1e07212370806f3ea4ff7a6f66e5716b60ab1f77.tar.gz
nitrocli-1e07212370806f3ea4ff7a6f66e5716b60ab1f77.tar.bz2
Bump nitrokey dependency to version 0.5.1
This change updates the version of the nitrokey crate that we use to 0.5.1. As part of that, it replaces occurrences of Storage::get_status with Storage::get_storage_status as the method has been renamed. Import subrepo nitrokey/:nitrokey at 817409140a8778215d2d65d614d3672166fff576
Diffstat (limited to 'nitrokey/examples')
-rw-r--r--nitrokey/examples/list-devices.rs26
-rw-r--r--nitrokey/examples/otp.rs43
2 files changed, 69 insertions, 0 deletions
diff --git a/nitrokey/examples/list-devices.rs b/nitrokey/examples/list-devices.rs
new file mode 100644
index 0000000..47fa054
--- /dev/null
+++ b/nitrokey/examples/list-devices.rs
@@ -0,0 +1,26 @@
+// Copyright (C) 2020 Robin Krahl <robin.krahl@ireas.org>
+// SPDX-License-Identifier: CC0-1.0
+
+//! Enumerates all connected Nitrokey devices and prints some information about them.
+
+use nitrokey::Device as _;
+
+fn main() -> Result<(), nitrokey::Error> {
+ let mut manager = nitrokey::take()?;
+ let device_infos = nitrokey::list_devices()?;
+ if device_infos.is_empty() {
+ println!("No Nitrokey device found");
+ } else {
+ println!("path\t\tmodel\tfirmware version\tserial number");
+ for device_info in device_infos {
+ let device = manager.connect_path(device_info.path.clone())?;
+ let model = device.get_model();
+ let status = device.get_status()?;
+ println!(
+ "{}\t{}\t{}\t\t\t{:08x}",
+ device_info.path, model, status.firmware_version, status.serial_number
+ );
+ }
+ }
+ Ok(())
+}
diff --git a/nitrokey/examples/otp.rs b/nitrokey/examples/otp.rs
new file mode 100644
index 0000000..f2c6f3c
--- /dev/null
+++ b/nitrokey/examples/otp.rs
@@ -0,0 +1,43 @@
+// Copyright (C) 2020 Robin Krahl <robin.krahl@ireas.org>
+// SPDX-License-Identifier: CC0-1.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(())
+}