From ee5de447d977999e7f5b4b41685d74ce28d63535 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 16 Dec 2019 00:40:30 -0500 Subject: move client id file manipulation out of oauth it doesn't actually have anything to do with the oauth flow --- teleterm/src/client.rs | 41 +++++++++++++++++++++++++++++++++++++++-- teleterm/src/cmd/stream.rs | 2 +- teleterm/src/cmd/watch.rs | 2 +- teleterm/src/oauth.rs | 38 -------------------------------------- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/teleterm/src/client.rs b/teleterm/src/client.rs index 67420f0..250ca1c 100644 --- a/teleterm/src/client.rs +++ b/teleterm/src/client.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use rand::Rng as _; +use std::io::Read as _; const HEARTBEAT_DURATION: std::time::Duration = std::time::Duration::from_secs(30); @@ -388,8 +389,7 @@ impl )) }) .and_then(move |(msg, sock)| { - crate::oauth::save_client_auth_id(auth_type, &id) - .map(|_| (msg, sock)) + save_client_auth_id(auth_type, &id).map(|_| (msg, sock)) }) .and_then(|(msg, sock)| { let response = format!( @@ -648,3 +648,40 @@ impl component_future::poll_stream(self, Self::POLL_FNS) } } + +pub fn load_client_auth_id( + auth: crate::protocol::AuthType, +) -> Option { + client_id_file(auth, true).and_then(|id_file| { + 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() + }) + }) + }) +} + +fn save_client_auth_id( + auth: crate::protocol::AuthType, + id: &str, +) -> impl futures::Future { + let id_file = client_id_file(auth, false).unwrap(); + let id = id.to_string(); + tokio::fs::File::create(id_file.clone()) + .with_context(move || crate::error::CreateFile { + filename: id_file.to_string_lossy().to_string(), + }) + .and_then(|file| { + tokio::io::write_all(file, id).context(crate::error::WriteFile) + }) + .map(|_| ()) +} + +fn client_id_file( + auth: crate::protocol::AuthType, + must_exist: bool, +) -> Option { + let filename = format!("client-oauth-{}", auth.name()); + crate::dirs::Dirs::new().data_file(&filename, must_exist) +} diff --git a/teleterm/src/cmd/stream.rs b/teleterm/src/cmd/stream.rs index 4c3f229..c47804a 100644 --- a/teleterm/src/cmd/stream.rs +++ b/teleterm/src/cmd/stream.rs @@ -36,7 +36,7 @@ impl crate::config::Config for Config { } } crate::protocol::AuthType::RecurseCenter => { - let id = crate::oauth::load_client_auth_id(self.client.auth); + let id = crate::client::load_client_auth_id(self.client.auth); crate::protocol::Auth::recurse_center( id.as_ref().map(std::string::String::as_str), ) diff --git a/teleterm/src/cmd/watch.rs b/teleterm/src/cmd/watch.rs index 7e6cb2d..e2e43e2 100644 --- a/teleterm/src/cmd/watch.rs +++ b/teleterm/src/cmd/watch.rs @@ -34,7 +34,7 @@ impl crate::config::Config for Config { } } crate::protocol::AuthType::RecurseCenter => { - let id = crate::oauth::load_client_auth_id(self.client.auth); + let id = crate::client::load_client_auth_id(self.client.auth); crate::protocol::Auth::recurse_center( id.as_ref().map(std::string::String::as_str), ) diff --git a/teleterm/src/oauth.rs b/teleterm/src/oauth.rs index ee914b4..d76a1b7 100644 --- a/teleterm/src/oauth.rs +++ b/teleterm/src/oauth.rs @@ -1,6 +1,5 @@ use crate::prelude::*; use oauth2::TokenResponse as _; -use std::io::Read as _; mod recurse_center; pub use recurse_center::RecurseCenter; @@ -77,43 +76,6 @@ pub trait Oauth { ) -> Box + Send>; } -pub fn save_client_auth_id( - auth: crate::protocol::AuthType, - id: &str, -) -> impl futures::Future { - let id_file = client_id_file(auth, false).unwrap(); - let id = id.to_string(); - tokio::fs::File::create(id_file.clone()) - .with_context(move || crate::error::CreateFile { - filename: id_file.to_string_lossy().to_string(), - }) - .and_then(|file| { - tokio::io::write_all(file, id).context(crate::error::WriteFile) - }) - .map(|_| ()) -} - -pub fn load_client_auth_id( - auth: crate::protocol::AuthType, -) -> Option { - client_id_file(auth, true).and_then(|id_file| { - 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() - }) - }) - }) -} - -fn client_id_file( - auth: crate::protocol::AuthType, - must_exist: bool, -) -> Option { - let filename = format!("client-oauth-{}", auth.name()); - crate::dirs::Dirs::new().data_file(&filename, must_exist) -} - fn cache_refresh_token( token_cache_file: std::path::PathBuf, token: &oauth2::basic::BasicTokenResponse, -- cgit v1.2.3