From 41dfe16047cd9f1a2accaacc3eff4c7e672f1e0d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 29 May 2018 21:10:52 +0000 Subject: Fix implementation of Authenticate for DeviceWrapper Previously, the Authenticate implementation for DeviceWrapper paniced if the wrapped device is a Nitrokey Storage. This patch implements authentication for wrapped Storage devices. --- src/auth.rs | 58 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'src') 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( + device: T, + constructor: C, + password: &str, +) -> Result, (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( + device: T, + constructor: C, + password: &str, +) -> Result, (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 User { /// 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 AuthenticatedDevice for Admin { impl Authenticate for DeviceWrapper { fn authenticate_user(self, password: &str) -> Result, (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, (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) } } } -- cgit v1.2.3