diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-10-17 01:27:59 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-10-17 02:21:08 -0400 |
commit | eced41252fcb7983c75ed6fe53d8c804365deecb (patch) | |
tree | 1c4f1acdebb7ef7477691d247e52a7811fd0977d | |
parent | 8fdd26b8cf2bd9f0821f7fbffcaa9e5fa9803607 (diff) | |
download | teleterm-eced41252fcb7983c75ed6fe53d8c804365deecb.tar.gz teleterm-eced41252fcb7983c75ed6fe53d8c804365deecb.zip |
use a real enum for auth types
-rw-r--r-- | src/cmd/stream.rs | 13 | ||||
-rw-r--r-- | src/cmd/watch.rs | 13 | ||||
-rw-r--r-- | src/oauth/recurse_center.rs | 2 | ||||
-rw-r--r-- | src/protocol.rs | 74 |
4 files changed, 76 insertions, 26 deletions
diff --git a/src/cmd/stream.rs b/src/cmd/stream.rs index 42a4296..6e7f4e3 100644 --- a/src/cmd/stream.rs +++ b/src/cmd/stream.rs @@ -31,23 +31,26 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { pub fn run<'a>(matches: &clap::ArgMatches<'a>) -> super::Result<()> { let auth = if matches.is_present("login-recurse-center") { - let auth = crate::protocol::Auth::RecurseCenter { id: None }; - let id_file = crate::dirs::Dirs::new() - .data_file(&format!("client-oauth-{}", auth.name())); + let id_file = crate::dirs::Dirs::new().data_file(&format!( + "client-oauth-{}", + crate::protocol::AuthType::RecurseCenter.name() + )); let id = std::fs::File::open(id_file).ok().and_then(|mut file| { let mut id = vec![]; file.read_to_end(&mut id).ok().map(|_| { std::string::String::from_utf8_lossy(&id).to_string() }) }); - crate::protocol::Auth::RecurseCenter { id } + crate::protocol::Auth::recurse_center( + id.as_ref().map(std::string::String::as_str), + ) } else { let username = matches .value_of("login-plain") .map(std::string::ToString::to_string) .or_else(|| std::env::var("USER").ok()) .context(crate::error::CouldntFindUsername)?; - crate::protocol::Auth::Plain { username } + crate::protocol::Auth::plain(&username) }; let address = matches.value_of("address").unwrap_or("127.0.0.1:4144"); let (host, address) = crate::util::resolve_address(address)?; diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 2a7676e..6841b4b 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -23,23 +23,26 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { pub fn run<'a>(matches: &clap::ArgMatches<'a>) -> super::Result<()> { let auth = if matches.is_present("login-recurse-center") { - let auth = crate::protocol::Auth::RecurseCenter { id: None }; - let id_file = crate::dirs::Dirs::new() - .data_file(&format!("client-oauth-{}", auth.name())); + let id_file = crate::dirs::Dirs::new().data_file(&format!( + "client-oauth-{}", + crate::protocol::AuthType::RecurseCenter.name() + )); let id = std::fs::File::open(id_file).ok().and_then(|mut file| { let mut id = vec![]; file.read_to_end(&mut id).ok().map(|_| { std::string::String::from_utf8_lossy(&id).to_string() }) }); - crate::protocol::Auth::RecurseCenter { id } + crate::protocol::Auth::recurse_center( + id.as_ref().map(std::string::String::as_str), + ) } else { let username = matches .value_of("login-plain") .map(std::string::ToString::to_string) .or_else(|| std::env::var("USER").ok()) .context(crate::error::CouldntFindUsername)?; - crate::protocol::Auth::Plain { username } + crate::protocol::Auth::plain(&username) }; let address = matches.value_of("address").unwrap_or("127.0.0.1:4144"); let (host, address) = crate::util::resolve_address(address)?; diff --git a/src/oauth/recurse_center.rs b/src/oauth/recurse_center.rs index a83ab40..273de66 100644 --- a/src/oauth/recurse_center.rs +++ b/src/oauth/recurse_center.rs @@ -41,7 +41,7 @@ impl super::Oauth for RecurseCenter { } fn name(&self) -> &str { - "recurse_center" + crate::protocol::AuthType::RecurseCenter.name() } fn get_username_from_access_token( diff --git a/src/protocol.rs b/src/protocol.rs index 381883e..059abd0 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -53,6 +53,41 @@ impl<T: tokio::io::AsyncWrite> FramedWriter<T> { pub const PROTO_VERSION: u8 = 1; +#[repr(u8)] +#[derive(Copy, Clone, Debug)] +pub enum AuthType { + Plain = 0, + RecurseCenter, +} + +impl AuthType { + pub fn name(self) -> &'static str { + match self { + Self::Plain => "plain", + Self::RecurseCenter => "recurse_center", + } + } + + pub fn is_oauth(self) -> bool { + match self { + Self::Plain => false, + Self::RecurseCenter => true, + } + } +} + +impl std::convert::TryFrom<u8> for AuthType { + type Error = Error; + + fn try_from(n: u8) -> Result<Self> { + Ok(match n { + 0 => Self::Plain, + 1 => Self::RecurseCenter, + _ => return Err(Error::InvalidAuthType { ty: n }), + }) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Auth { Plain { username: String }, @@ -60,24 +95,34 @@ pub enum Auth { } impl Auth { - pub fn is_oauth(&self) -> bool { - match self { - Self::Plain { .. } => false, - Self::RecurseCenter { .. } => true, + pub fn plain(username: &str) -> Self { + Self::Plain { + username: username.to_string(), + } + } + + pub fn recurse_center(id: Option<&str>) -> Self { + Self::RecurseCenter { + id: id.map(std::string::ToString::to_string), } } - pub fn name(&self) -> &str { + pub fn is_oauth(&self) -> bool { + self.auth_type().is_oauth() + } + + pub fn name(&self) -> &'static str { + self.auth_type().name() + } + + fn auth_type(&self) -> AuthType { match self { - Self::Plain { .. } => "plain", - Self::RecurseCenter { .. } => "recurse_center", + Self::Plain { .. } => AuthType::Plain, + Self::RecurseCenter { .. } => AuthType::RecurseCenter, } } } -const AUTH_PLAIN: u8 = 0; -const AUTH_RECURSE_CENTER: u8 = 1; - // XXX https://github.com/rust-lang/rust/issues/64362 #[allow(dead_code)] #[derive(Debug, Clone, PartialEq, Eq)] @@ -385,14 +430,13 @@ impl From<&Message> for Packet { } } fn write_auth(val: &Auth, data: &mut Vec<u8>) { + write_u8(val.auth_type() as u8, data); match val { Auth::Plain { username } => { - write_u8(AUTH_PLAIN, data); write_str(username, data); } Auth::RecurseCenter { id } => { let id = id.as_ref().map_or("", |s| s.as_str()); - write_u8(AUTH_RECURSE_CENTER, data); write_str(id, data); } } @@ -617,19 +661,19 @@ impl std::convert::TryFrom<Packet> for Message { } fn read_auth(data: &[u8]) -> Result<(Auth, &[u8])> { let (ty, data) = read_u8(data)?; + let ty = AuthType::try_from(ty)?; let (auth, data) = match ty { - AUTH_PLAIN => { + AuthType::Plain => { let (username, data) = read_str(data)?; let auth = Auth::Plain { username }; (auth, data) } - AUTH_RECURSE_CENTER => { + AuthType::RecurseCenter => { let (id, data) = read_str(data)?; let id = if id == "" { None } else { Some(id) }; let auth = Auth::RecurseCenter { id }; (auth, data) } - _ => return Err(Error::InvalidAuthType { ty }), }; Ok((auth, data)) } |