Skip to content

Commit

Permalink
cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
therealbstern committed Dec 22, 2019
1 parent 37d3080 commit 2356559
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
41 changes: 21 additions & 20 deletions src/backingstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub trait BackingStore : Debug {
/// Only one of `users` or `users_iter` needs to be implemented, as the default implementations will take care of
/// the other. However, there may be performance reasons to implement both.
fn users(&self) -> Result<Vec<String>, BackingStoreError> {
self.users_iter().map(|v| v.map(|u| u.clone()).collect())
self.users_iter().map(|v| v.collect())
}

/// Return an Iterator over the user names. `users` may be more convenient when there are small numbers of users.
Expand Down Expand Up @@ -158,7 +158,7 @@ impl FileBackingStore {
pub fn new(filename: &str) -> FileBackingStore {
let fname = filename.to_string();
FileBackingStore {
filename: Mutex::new(fname.clone()),
filename: Mutex::new(fname),
}
}

Expand Down Expand Up @@ -193,7 +193,7 @@ impl FileBackingStore {
}

fn hash_is_locked(hash: &str) -> bool {
hash.starts_with("!")
hash.starts_with('!')
}

// Fix usernames so that no illegal characters or strings enter the backing store. I'd like all `BackingStore`
Expand Down Expand Up @@ -253,7 +253,7 @@ impl FileBackingStore {
{ // In its own block because I want to drop the backup file once it was written.
let mut backupf = FileBackingStore::create_safe(&oldfn)?;

backupf.write(all.as_bytes())?;
backupf.write_all(all.as_bytes())?;
backupf.flush()?;
}

Expand Down Expand Up @@ -379,7 +379,7 @@ impl BackingStore for FileBackingStore {
let pwfile = self.load_file()?;
for line in pwfile.lines() {
let v: Vec<&str> = line.split(':').collect();
if v.len() == 0 {
if v.is_empty() {
continue;
} else {
users.push(v[0].to_string());
Expand All @@ -393,12 +393,11 @@ impl BackingStore for FileBackingStore {
let fixeduser = FileBackingStore::fix_username(user);
for line in pwfile.lines() {
let v: Vec<&str> = line.split(':').collect();
if v.len() > 1 {
if v[0] == fixeduser {
return match FileBackingStore::hash_is_locked(v[1]) {
true => Err(BackingStoreError::Locked),
false => Ok(true),
};
if (v.len() > 1) && (v[0] == fixeduser) {
if FileBackingStore::hash_is_locked(v[1]) {
return Err(BackingStoreError::Locked);
} else {
return Ok(true);
}
}
}
Expand All @@ -414,7 +413,7 @@ struct MemoryEntry {

/// In memory backing store. Does not persist across restarts. Mostly
/// useful for testing.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct MemoryBackingStore {
users: Mutex<HashMap<String, MemoryEntry>>,
}
Expand Down Expand Up @@ -476,12 +475,13 @@ impl BackingStore for MemoryBackingStore {
let mut hashmap =
self.users.lock().map_err(|_| BackingStoreError::Mutex)?;
match hashmap.get_mut(user) {
Some(entry) => match entry.locked {
true => Err(BackingStoreError::Locked),
false => {
Some(entry) => {
if entry.locked {
Err(BackingStoreError::Locked)
} else {
entry.credentials = enc_cred.to_string();
Ok(())
},
}
},
None => Err(BackingStoreError::NoSuchUser),
}
Expand Down Expand Up @@ -541,15 +541,16 @@ impl BackingStore for MemoryBackingStore {

fn users(&self) -> Result<Vec<String>, BackingStoreError> {
let hashmap = self.users.lock().map_err(|_| BackingStoreError::Mutex)?;
Ok(hashmap.keys().map(|k| k.clone()).collect::<Vec<String>>())
Ok(hashmap.keys().cloned().collect::<Vec<String>>())
}

fn check_user(&self, user: &str) -> Result<bool, BackingStoreError> {
let hashmap = self.users.lock().map_err(|_| BackingStoreError::Mutex)?;
if let Some(u) = hashmap.get(user) {
match u.locked {
true => Err(BackingStoreError::Locked),
false => Ok(true),
if u.locked {
Err(BackingStoreError::Locked)
} else {
Ok(true)
}
} else {
Ok(false)
Expand Down
25 changes: 12 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Authenticator {
pub fn new(backing_store: Box<dyn BackingStore + Send + Sync>, expiration: Duration, policy: SessionPolicy) -> Authenticator {
Authenticator {
sess_mgr: SessionManager::new(expiration, policy),
backing_store: backing_store,
backing_store,
// cookie_name: cookie_name.to_string(),
mapping: Mutex::new(HashMap::new()),
}
Expand All @@ -124,7 +124,7 @@ impl Authenticator {
/// `user`. Does not change any signatures associated with the
/// user.
pub fn verify(&self, user: &str, credentials: &str) -> Result<bool, AuthError> {
self.backing_store.verify(user, credentials).map_err(|e| AuthError::from(e))
self.backing_store.verify(user, credentials).map_err(AuthError::from)
}

// should check policy
Expand All @@ -147,7 +147,7 @@ impl Authenticator {
Ok(false) => Err(AuthError::Unauthorized),
Err(e) => Err(e),
},
Err(e) => Err(From::from(e)),
Err(e) => Err(e),
}
}

Expand All @@ -167,8 +167,7 @@ impl Authenticator {
match self.sess_mgr.is_expired(signature) {
Ok(true) => Err(AuthError::Expired),
Ok(false) => match self.mapping.lock() {
Ok(hashmap) => Ok(hashmap.get(&signature.token.to_string())
.map(|s| s.clone())), // this is to unborrow the username
Ok(hashmap) => Ok(hashmap.get(&signature.token.to_string()).cloned()),
Err(_) => Err(AuthError::Mutex),
},
Err(e) => Err(e),
Expand Down Expand Up @@ -203,17 +202,17 @@ impl Authenticator {
Ok(mut hashmap) => hashmap.retain(|_, ref v| user != *v),
Err(mut poisoned) => poisoned.get_mut().retain(|_, ref v| user != *v),
};
self.backing_store.lock(user).map_err(|e| AuthError::from(e))
self.backing_store.lock(user).map_err(AuthError::from)
}

/// Check if the user's account is locked.
pub fn is_locked(&self, user: &str) -> Result<bool, AuthError> {
self.backing_store.is_locked(user).map_err(|e| AuthError::from(e))
self.backing_store.is_locked(user).map_err(AuthError::from)
}

/// Re-enable the user's account. The old password will remain valid.
pub fn unlock(&self, user: &str) -> Result<(), AuthError> {
self.backing_store.unlock(user).map_err(|e| AuthError::from(e))
self.backing_store.unlock(user).map_err(AuthError::from)
}

/// Create a new user with the given credentials. Credentials should
Expand All @@ -233,30 +232,30 @@ impl Authenticator {
/// Delete the given user. Any stored credentials will be deleted too, and
/// will need to be provided again if the user is later re-created.
pub fn delete(&self, user: &str) -> Result<(), AuthError> {
self.backing_store.delete(user).map_err(|e| AuthError::from(e))
self.backing_store.delete(user).map_err(AuthError::from)
}

/// This is the main driver. It returns a signature that contains the
/// current value for the cookie, or an error if something went wrong. The
/// returned signature may be different from the one provided.
pub fn run(&self, signature: ConnectionSignature) -> Result<ConnectionSignature, AuthError> {
self.sess_mgr.start(signature).map_err(|err| AuthError::from(err))
self.sess_mgr.start(signature).map_err(AuthError::from)
}

/// Return a Vec of usernames.
pub fn users(&self) -> Result<Vec<String>, AuthError> {
self.backing_store.users().map_err(|e| AuthError::from(e))
self.backing_store.users().map_err(AuthError::from)
}

/// Return an iterator over users.
pub fn users_iter(&self) -> Result<IntoIter<String>, AuthError> {
self.backing_store.users_iter().map_err(|e| AuthError::from(e))
self.backing_store.users_iter().map_err(AuthError::from)
}

/// Identify whether or not the user already exists in the backing store.
/// May return an `AuthError````, in particular, `AuthError::Locked`, which
/// means that the user exists but the account is locked.
pub fn check_user(&self, user: &str) -> Result<bool, AuthError> {
self.backing_store.check_user(user).map_err(|e| AuthError::from(e))
self.backing_store.check_user(user).map_err(AuthError::from)
}
}
4 changes: 2 additions & 2 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub struct SessionManager {
impl SessionManager {
pub fn new(expiration: Duration, policy: SessionPolicy) -> SessionManager {
SessionManager {
expiration: expiration,
policy: policy,
expiration,
policy,
// cookie_dir: "cookies".to_string(),
sessions: Mutex::new(HashMap::new()),
}
Expand Down
14 changes: 7 additions & 7 deletions src/ws_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use websession::{Authenticator, SessionPolicy};
use websession::backingstore::FileBackingStore;
use clap::{App, Arg, SubCommand, AppSettings};

const ADDUSER: &'static str = "adduser";
const DELUSER: &'static str = "deluser";
const LOCK: &'static str = "lock";
const UNLOCK: &'static str = "unlock";
const PASSWD: &'static str = "passwd";
const INFO: &'static str = "info";
const ID: &'static str = "id";
const ADDUSER: & str = "adduser";
const DELUSER: & str = "deluser";
const LOCK: & str = "lock";
const UNLOCK: & str = "unlock";
const PASSWD: & str = "passwd";
const INFO: & str = "info";
const ID: & str = "id";

fn get_password() -> String {
let mut pw;
Expand Down

0 comments on commit 2356559

Please sign in to comment.