Skip to content

Commit

Permalink
Impl From<errors> for RegistrarError
Browse files Browse the repository at this point in the history
  • Loading branch information
thespooler committed Apr 13, 2020
1 parent bc2440a commit 2c72eb0
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions oxide-auth/src/primitives/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use std::cmp;
use std::collections::HashMap;
use std::fmt;
use std::iter::{Extend, FromIterator};
use std::sync::{Arc, MutexGuard, RwLockWriteGuard};
use std::rc::Rc;
use std::string::FromUtf8Error;
use std::sync::{Arc, MutexGuard, RwLockWriteGuard};

use argon2::{self, Config};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -295,6 +296,18 @@ pub struct Argon2 {
_private: ()
}

impl From<FromUtf8Error> for RegistrarError {
fn from(_: FromUtf8Error) -> Self {
RegistrarError::PrimitiveError
}
}

impl From<argon2::Error> for RegistrarError {
fn from(_: argon2::Error) -> Self {
RegistrarError::PrimitiveError
}
}

impl PasswordPolicy for Argon2 {
fn store(&self, client_id: &str, passphrase: &[u8]) -> Vec<u8> {
let mut config = Config::default();
Expand All @@ -312,18 +325,9 @@ impl PasswordPolicy for Argon2 {
fn check(&self, client_id: &str, passphrase: &[u8], stored: &[u8])
-> Result<(), RegistrarError>
{
let hash = String::from_utf8(stored.to_vec());
let valid = match hash {
Ok(hash) => argon2::verify_encoded_ext(&hash, passphrase, &[], client_id.as_bytes())
.map_err(|_| RegistrarError::PrimitiveError),
_ => Err(RegistrarError::PrimitiveError),
};

match valid {
Ok(true) => Ok(()),
Ok(false) => Err(RegistrarError::Unspecified),
Err(err) => Err(err),
}
let hash = String::from_utf8(stored.to_vec())?;
let valid = argon2::verify_encoded_ext(&hash, passphrase, &[], client_id.as_bytes())?;
if valid { Ok(()) } else { Err(RegistrarError::Unspecified) }
}
}

Expand Down

0 comments on commit 2c72eb0

Please sign in to comment.