aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-02-05 13:05:59 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-07-20 08:20:07 +0000
commite31f1bca97fd8ee36bd933fa54c53980fc5084a7 (patch)
tree7213cff0b366d72f306653fdbdece604dd9679c8
parent678f0b700666a4ba86db2180078d79a730dc82e0 (diff)
downloadnitrokey-rs-e31f1bca97fd8ee36bd933fa54c53980fc5084a7.tar.gz
nitrokey-rs-e31f1bca97fd8ee36bd933fa54c53980fc5084a7.tar.bz2
Add AuthenticationError
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/error.rs56
-rw-r--r--src/lib.rs4
3 files changed, 61 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5d049c..e256fc6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -52,6 +52,8 @@ SPDX-License-Identifier: MIT
- Add the `force_take` function that ignores a `PoisonError` when accessing
the manager instance.
- Internally refactor the `device` module into submodules.
+- Refactor authentication:
+ - Add `AuthenticationError`.
# v0.3.4 (2019-01-20)
- Fix authentication methods that assumed that `char` is signed.
diff --git a/src/error.rs b/src/error.rs
index 9e6adc0..1b0edd4 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -3,6 +3,7 @@
use std::error;
use std::fmt;
+use std::marker;
use std::os::raw;
use std::str;
use std::sync;
@@ -46,6 +47,12 @@ impl From<raw::c_int> for Error {
}
}
+impl<'a, T: crate::Device<'a>> From<AuthenticationError<'a, T>> for Error {
+ fn from(err: AuthenticationError<'a, T>) -> Self {
+ err.error
+ }
+}
+
impl From<CommandError> for Error {
fn from(err: CommandError) -> Self {
Error::CommandError(err)
@@ -267,3 +274,52 @@ impl fmt::Display for LibraryError {
})
}
}
+
+#[derive(Debug)]
+pub struct AuthenticationError<'a, T: device::Device<'a>> {
+ error: Error,
+ device: T,
+ marker: marker::PhantomData<dyn device::Device<'a>>,
+}
+
+impl<'a, T: device::Device<'a>> AuthenticationError<'a, T> {
+ pub fn new(error: Error, device: T) -> Self {
+ AuthenticationError {
+ error,
+ device,
+ marker: marker::PhantomData,
+ }
+ }
+
+ pub fn as_error(&self) -> &Error {
+ &self.error
+ }
+
+ pub fn into_device(self) -> T {
+ self.device
+ }
+
+ pub fn map_device<'b, U, F>(self, op: F) -> AuthenticationError<'b, U>
+ where
+ U: device::Device<'b>,
+ F: Fn(T) -> U,
+ {
+ AuthenticationError {
+ error: self.error,
+ device: op(self.device),
+ marker: marker::PhantomData,
+ }
+ }
+}
+
+impl<'a, T: device::Device<'a>> error::Error for AuthenticationError<'a, T> {
+ fn source(&self) -> Option<&(dyn error::Error + 'static)> {
+ Some(&self.error)
+ }
+}
+
+impl<'a, T: device::Device<'a>> fmt::Display for AuthenticationError<'a, T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.error.fmt(f)
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index a4402c5..e3a2bc3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -121,7 +121,9 @@ pub use crate::device::{
Device, DeviceWrapper, Model, Pro, SdCardData, Storage, StorageProductionInfo, StorageStatus,
VolumeMode, VolumeStatus,
};
-pub use crate::error::{CommandError, CommunicationError, Error, LibraryError};
+pub use crate::error::{
+ AuthenticationError, CommandError, CommunicationError, Error, LibraryError,
+};
pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
pub use crate::util::LogLevel;