diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2018-05-29 21:10:52 +0000 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2018-05-29 23:12:25 +0200 | 
| commit | 41dfe16047cd9f1a2accaacc3eff4c7e672f1e0d (patch) | |
| tree | 0b181b1c41b79dd05576e3dca5136b4550733699 /src | |
| parent | 1a0947f882505a15ab1d9d2d7b51db1cf85ea8b6 (diff) | |
| download | nitrokey-rs-41dfe16047cd9f1a2accaacc3eff4c7e672f1e0d.tar.gz nitrokey-rs-41dfe16047cd9f1a2accaacc3eff4c7e672f1e0d.tar.bz2 | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/auth.rs | 58 | 
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)              }          }      } | 
