From 17449283628c76c0cbaa8c723750ff80aafdd7da Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 27 Nov 2019 14:42:46 -0500 Subject: pull auth_client out into its own field it doesn't hurt anything even though it's not super necessary for plain logins, and it's more convenient to access without needing to deconstruct the enum value --- teleterm/src/client.rs | 14 ++++++++++++-- teleterm/src/cmd/stream.rs | 8 ++++++-- teleterm/src/cmd/watch.rs | 10 +++++++--- teleterm/src/protocol.rs | 43 ++++++++++++++++++------------------------- teleterm/src/web/list.rs | 7 ++++++- teleterm/src/web/login.rs | 7 ++++++- teleterm/src/web/watch.rs | 8 ++++++-- 7 files changed, 61 insertions(+), 36 deletions(-) diff --git a/teleterm/src/client.rs b/teleterm/src/client.rs index 0672811..67420f0 100644 --- a/teleterm/src/client.rs +++ b/teleterm/src/client.rs @@ -76,6 +76,7 @@ pub struct Client< > { connect: Connector, auth: crate::protocol::Auth, + auth_client: crate::protocol::AuthClient, term_type: String, @@ -104,11 +105,13 @@ impl term_type: &str, connect: Connector, auth: &crate::protocol::Auth, + auth_client: crate::protocol::AuthClient, ) -> Self { Self::new( term_type, connect, auth, + auth_client, &[crate::protocol::Message::start_streaming()], false, ) @@ -118,12 +121,14 @@ impl term_type: &str, connect: Connector, auth: &crate::protocol::Auth, + auth_client: crate::protocol::AuthClient, id: &str, ) -> Self { Self::new( term_type, connect, auth, + auth_client, &[crate::protocol::Message::start_watching(id)], false, ) @@ -133,22 +138,25 @@ impl term_type: &str, connect: Connector, auth: &crate::protocol::Auth, + auth_client: crate::protocol::AuthClient, ) -> Self { - Self::new(term_type, connect, auth, &[], false) + Self::new(term_type, connect, auth, auth_client, &[], false) } pub fn raw( term_type: &str, connect: Connector, auth: &crate::protocol::Auth, + auth_client: crate::protocol::AuthClient, ) -> Self { - Self::new(term_type, connect, auth, &[], true) + Self::new(term_type, connect, auth, auth_client, &[], true) } fn new( term_type: &str, connect: Connector, auth: &crate::protocol::Auth, + auth_client: crate::protocol::AuthClient, on_login: &[crate::protocol::Message], raw: bool, ) -> Self { @@ -158,6 +166,7 @@ impl Self { connect, auth: auth.clone(), + auth_client, term_type: term_type.to_string(), @@ -234,6 +243,7 @@ impl self.to_send.clear(); self.send_message(crate::protocol::Message::login( &self.auth, + self.auth_client, &self.term_type, crate::term::Size::get()?, )); diff --git a/teleterm/src/cmd/stream.rs b/teleterm/src/cmd/stream.rs index 92026f1..4c3f229 100644 --- a/teleterm/src/cmd/stream.rs +++ b/teleterm/src/cmd/stream.rs @@ -38,7 +38,6 @@ impl crate::config::Config for Config { crate::protocol::AuthType::RecurseCenter => { let id = crate::oauth::load_client_auth_id(self.client.auth); crate::protocol::Auth::recurse_center( - crate::protocol::AuthClient::Cli, id.as_ref().map(std::string::String::as_str), ) } @@ -144,7 +143,12 @@ impl ) -> Self { let term_type = std::env::var("TERM").unwrap_or_else(|_| "".to_string()); - let client = crate::client::Client::stream(&term_type, connect, auth); + let client = crate::client::Client::stream( + &term_type, + connect, + auth, + crate::protocol::AuthClient::Cli, + ); // TODO: tokio::io::stdin is broken (it's blocking) // see https://github.com/tokio-rs/tokio/issues/589 diff --git a/teleterm/src/cmd/watch.rs b/teleterm/src/cmd/watch.rs index dc80d47..7e6cb2d 100644 --- a/teleterm/src/cmd/watch.rs +++ b/teleterm/src/cmd/watch.rs @@ -36,7 +36,6 @@ impl crate::config::Config for Config { crate::protocol::AuthType::RecurseCenter => { let id = crate::oauth::load_client_auth_id(self.client.auth); crate::protocol::Auth::recurse_center( - crate::protocol::AuthClient::Cli, id.as_ref().map(std::string::String::as_str), ) } @@ -212,8 +211,12 @@ impl ) -> Self { let term_type = std::env::var("TERM").unwrap_or_else(|_| "".to_string()); - let list_client = - crate::client::Client::list(&term_type, make_connector(), auth); + let list_client = crate::client::Client::list( + &term_type, + make_connector(), + auth, + crate::protocol::AuthClient::Cli, + ); Self { term_type, @@ -332,6 +335,7 @@ impl &self.term_type, (self.make_connector)(), &self.auth, + crate::protocol::AuthClient::Cli, id, ); self.state.watching(client); diff --git a/teleterm/src/protocol.rs b/teleterm/src/protocol.rs index 7da5e92..29f72e9 100644 --- a/teleterm/src/protocol.rs +++ b/teleterm/src/protocol.rs @@ -189,13 +189,8 @@ impl std::convert::TryFrom<&str> for AuthType { Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, )] pub enum Auth { - Plain { - username: String, - }, - RecurseCenter { - auth_client: AuthClient, - id: Option, - }, + Plain { username: String }, + RecurseCenter { id: Option }, } impl Auth { @@ -205,9 +200,8 @@ impl Auth { } } - pub fn recurse_center(auth_client: AuthClient, id: Option<&str>) -> Self { + pub fn recurse_center(id: Option<&str>) -> Self { Self::RecurseCenter { - auth_client, id: id.map(std::string::ToString::to_string), } } @@ -217,12 +211,7 @@ impl Auth { } pub fn name(&self) -> String { - match &self { - Self::RecurseCenter { auth_client, .. } => { - format!("{}.{}", self.auth_type().name(), auth_client.name()) - } - _ => self.auth_type().name().to_string(), - } + self.auth_type().name().to_string() } pub fn auth_type(&self) -> AuthType { @@ -285,6 +274,7 @@ pub enum Message { Login { proto_version: u8, auth: Auth, + auth_client: AuthClient, term_type: String, size: crate::term::Size, }, @@ -328,12 +318,14 @@ pub enum Message { impl Message { pub fn login( auth: &Auth, + auth_client: AuthClient, term_type: &str, size: crate::term::Size, ) -> Self { Self::Login { proto_version: PROTO_VERSION, auth: auth.clone(), + auth_client, term_type: term_type.to_string(), size, } @@ -612,9 +604,8 @@ impl From<&Message> for Packet { Auth::Plain { username } => { write_str(username, data); } - Auth::RecurseCenter { auth_client, id } => { + Auth::RecurseCenter { id } => { let id = id.as_ref().map_or("", |s| s.as_str()); - write_u8(*auth_client as u8, data); write_str(id, data); } } @@ -627,11 +618,13 @@ impl From<&Message> for Packet { Message::Login { proto_version, auth, + auth_client, term_type, size, } => { write_u8(*proto_version, &mut data); write_auth(auth, &mut data); + write_u8(*auth_client as u8, &mut data); write_str(term_type, &mut data); write_size(*size, &mut data); } @@ -786,11 +779,9 @@ impl std::convert::TryFrom for Message { (auth, data) } AuthType::RecurseCenter => { - let (auth_client, data) = read_u8(data)?; - let auth_client = AuthClient::try_from(auth_client)?; let (id, data) = read_str(data)?; let id = if id == "" { None } else { Some(id) }; - let auth = Auth::RecurseCenter { auth_client, id }; + let auth = Auth::RecurseCenter { id }; (auth, data) } }; @@ -803,6 +794,8 @@ impl std::convert::TryFrom for Message { MessageType::Login => { let (proto_version, data) = read_u8(data)?; let (auth, data) = read_auth(data)?; + let (auth_client, data) = read_u8(data)?; + let auth_client = AuthClient::try_from(auth_client)?; let (term_type, data) = read_str(data)?; let (size, data) = read_size(data)?; @@ -810,6 +803,7 @@ impl std::convert::TryFrom for Message { Self::Login { proto_version, auth, + auth_client, term_type, size, }, @@ -1013,22 +1007,21 @@ mod test { &Auth::Plain { username: "doy".to_string(), }, + AuthClient::Cli, "screen", crate::term::Size { rows: 24, cols: 80 }, ), Message::login( &Auth::RecurseCenter { - auth_client: AuthClient::Cli, id: Some("some-random-id".to_string()), }, + AuthClient::Cli, "screen", crate::term::Size { rows: 24, cols: 80 }, ), Message::login( - &Auth::RecurseCenter { - auth_client: AuthClient::Cli, - id: None, - }, + &Auth::RecurseCenter { id: None }, + AuthClient::Cli, "screen", crate::term::Size { rows: 24, cols: 80 }, ), diff --git a/teleterm/src/web/list.rs b/teleterm/src/web/list.rs index 2807b7f..d74ac83 100644 --- a/teleterm/src/web/list.rs +++ b/teleterm/src/web/list.rs @@ -29,7 +29,12 @@ pub fn run( .context(crate::error::Connect { address }), ) }); - let client = crate::client::Client::raw("teleterm-web", connector, auth); + let client = crate::client::Client::raw( + "teleterm-web", + connector, + auth, + crate::protocol::AuthClient::Web, + ); let (w_sessions, r_sessions) = tokio::sync::oneshot::channel(); diff --git a/teleterm/src/web/login.rs b/teleterm/src/web/login.rs index 2873887..0f082e1 100644 --- a/teleterm/src/web/login.rs +++ b/teleterm/src/web/login.rs @@ -34,7 +34,12 @@ pub fn run( ) }); let auth = crate::protocol::Auth::plain(&username); - let client = crate::client::Client::raw("teleterm-web", connector, &auth); + let client = crate::client::Client::raw( + "teleterm-web", + connector, + &auth, + crate::protocol::AuthClient::Web, + ); let (w_login, r_login) = tokio::sync::oneshot::channel(); diff --git a/teleterm/src/web/watch.rs b/teleterm/src/web/watch.rs index 2bce9e0..e972654 100644 --- a/teleterm/src/web/watch.rs +++ b/teleterm/src/web/watch.rs @@ -59,8 +59,12 @@ pub fn run( .context(crate::error::Connect { address }), ) }); - let client = - crate::client::Client::raw("teleterm-web", connector, &auth); + let client = crate::client::Client::raw( + "teleterm-web", + connector, + &auth, + crate::protocol::AuthClient::Web, + ); tokio::spawn( Connection::new( -- cgit v1.2.3