aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/auth.rs58
1 files changed, 39 insertions, 19 deletions
diff --git a/src/auth.rs b/src/auth.rs
index c772faf..8d97063 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -164,6 +164,38 @@ where
};
}
+fn authenticate_user_wrapper<T, C>(
+ device: T,
+ constructor: C,
+ password: &str,
+) -> Result<User<DeviceWrapper>, (DeviceWrapper, CommandError)>
+where
+ T: Device,
+ C: Fn(T) -> DeviceWrapper,
+{
+ let result = device.authenticate_user(password);
+ match result {
+ Ok(user) => Ok(User::new(constructor(user.device), user.temp_password)),
+ Err((device, err)) => Err((constructor(device), err)),
+ }
+}
+
+fn authenticate_admin_wrapper<T, C>(
+ device: T,
+ constructor: C,
+ password: &str,
+) -> Result<Admin<DeviceWrapper>, (DeviceWrapper, CommandError)>
+where
+ T: Device,
+ C: Fn(T) -> DeviceWrapper,
+{
+ let result = device.authenticate_admin(password);
+ match result {
+ Ok(user) => Ok(Admin::new(constructor(user.device), user.temp_password)),
+ Err((device, err)) => Err((constructor(device), err)),
+ }
+}
+
impl<T: Device> User<T> {
/// Forgets the user authentication and returns an unauthenticated device. This method
/// consumes the authenticated device. It does not perform any actual commands on the
@@ -343,32 +375,20 @@ impl<T: Device> AuthenticatedDevice<T> for Admin<T> {
impl Authenticate for DeviceWrapper {
fn authenticate_user(self, password: &str) -> Result<User<Self>, (Self, CommandError)> {
match self {
- DeviceWrapper::Storage(_) => panic!("..."),
- DeviceWrapper::Pro(pro) => {
- let result = pro.authenticate_user(password);
- match result {
- Ok(user) => Ok(User::new(
- DeviceWrapper::Pro(user.device),
- user.temp_password,
- )),
- Err((pro, err)) => Err((DeviceWrapper::Pro(pro), err)),
- }
+ DeviceWrapper::Storage(storage) => {
+ authenticate_user_wrapper(storage, DeviceWrapper::Storage, password)
}
+ DeviceWrapper::Pro(pro) => authenticate_user_wrapper(pro, DeviceWrapper::Pro, password),
}
}
fn authenticate_admin(self, password: &str) -> Result<Admin<Self>, (Self, CommandError)> {
match self {
- DeviceWrapper::Storage(_) => panic!("..."),
+ DeviceWrapper::Storage(storage) => {
+ authenticate_admin_wrapper(storage, DeviceWrapper::Storage, password)
+ }
DeviceWrapper::Pro(pro) => {
- let result = pro.authenticate_admin(password);
- match result {
- Ok(admin) => Ok(Admin::new(
- DeviceWrapper::Pro(admin.device),
- admin.temp_password,
- )),
- Err((pro, err)) => Err((DeviceWrapper::Pro(pro), err)),
- }
+ authenticate_admin_wrapper(pro, DeviceWrapper::Pro, password)
}
}
}