diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-10-17 01:48:21 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-10-17 02:21:08 -0400 |
commit | 09d39d819f187eafe8962e75d8ce70b31d3b72de (patch) | |
tree | 9ba63d6640ab9764f4fe01cd9fbb9d662c0445eb /src | |
parent | eced41252fcb7983c75ed6fe53d8c804365deecb (diff) | |
download | teleterm-09d39d819f187eafe8962e75d8ce70b31d3b72de.tar.gz teleterm-09d39d819f187eafe8962e75d8ce70b31d3b72de.zip |
factor out caching of the client oauth id
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/stream.rs | 14 | ||||
-rw-r--r-- | src/cmd/watch.rs | 15 | ||||
-rw-r--r-- | src/oauth.rs | 18 |
3 files changed, 25 insertions, 22 deletions
diff --git a/src/cmd/stream.rs b/src/cmd/stream.rs index 6e7f4e3..24e2a41 100644 --- a/src/cmd/stream.rs +++ b/src/cmd/stream.rs @@ -1,5 +1,4 @@ use crate::prelude::*; -use std::io::Read as _; use tokio::io::AsyncWrite as _; pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { @@ -31,16 +30,9 @@ 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 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() - }) - }); + let id = crate::oauth::load_client_auth_id( + crate::protocol::AuthType::RecurseCenter, + ); crate::protocol::Auth::recurse_center( id.as_ref().map(std::string::String::as_str), ) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 6841b4b..0e300b2 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use std::io::{Read as _, Write as _}; +use std::io::Write as _; pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { app.about("Watch teleterm streams") @@ -23,16 +23,9 @@ 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 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() - }) - }); + let id = crate::oauth::load_client_auth_id( + crate::protocol::AuthType::RecurseCenter, + ); crate::protocol::Auth::recurse_center( id.as_ref().map(std::string::String::as_str), ) diff --git a/src/oauth.rs b/src/oauth.rs index 3817b7d..3504af8 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use oauth2::TokenResponse as _; +use std::io::Read as _; mod recurse_center; pub use recurse_center::RecurseCenter; @@ -71,6 +72,23 @@ pub trait Oauth { ) -> Box<dyn futures::future::Future<Item = String, Error = Error> + Send>; } +pub fn load_client_auth_id( + auth: crate::protocol::AuthType, +) -> Option<String> { + let id_file = client_id_file(auth); + 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) -> std::path::PathBuf { + let filename = format!("client-oauth-{}", auth.name()); + crate::dirs::Dirs::new().data_file(&filename) +} + fn cache_refresh_token( token_cache_file: std::path::PathBuf, token: &oauth2::basic::BasicTokenResponse, |