diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-10-16 01:36:26 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-10-16 01:36:26 -0400 |
commit | f1cb4b1ea5dc268077d8d7be1c335208105ddcc5 (patch) | |
tree | 1584ac5853f9117f73e1c9fe78e775475c2bcb52 | |
parent | 407f5b472fbd69c86f49d4fc0c45406411477d53 (diff) | |
download | teleterm-f1cb4b1ea5dc268077d8d7be1c335208105ddcc5.tar.gz teleterm-f1cb4b1ea5dc268077d8d7be1c335208105ddcc5.zip |
further generalize the oauth flow
-rw-r--r-- | src/protocol.rs | 16 | ||||
-rw-r--r-- | src/server.rs | 88 |
2 files changed, 69 insertions, 35 deletions
diff --git a/src/protocol.rs b/src/protocol.rs index 5aa6c3c..df35949 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -58,6 +58,22 @@ pub enum Auth { RecurseCenter { id: Option<String> }, } +impl Auth { + pub fn is_oauth(&self) -> bool { + match self { + Self::Plain { .. } => false, + Self::RecurseCenter { .. } => true, + } + } + + pub fn name(&self) -> &str { + match self { + Self::Plain { .. } => "plain", + Self::RecurseCenter { .. } => "recurse_center", + } + } +} + const AUTH_PLAIN: u32 = 0; const AUTH_RECURSE_CENTER: u32 = 1; diff --git a/src/server.rs b/src/server.rs index 6a85d74..5b82f3c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -337,7 +337,7 @@ impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static> fn handle_message_login( &mut self, conn: &mut Connection<S>, - auth: crate::protocol::Auth, + auth: &crate::protocol::Auth, term_type: &str, size: crate::term::Size, ) -> Result<()> { @@ -345,48 +345,65 @@ impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static> return Err(Error::TermTooBig { size }); } - match auth { + match &auth { crate::protocol::Auth::Plain { username } => { - log::info!("{}: login(plain, {})", conn.id, username); - conn.state.login_plain(&username, term_type, &size); + log::info!( + "{}: login({}, {})", + auth.name(), + conn.id, + username + ); + conn.state.login_plain(username, term_type, &size); conn.send_message(crate::protocol::Message::logged_in( - &username, + username, )); } - crate::protocol::Auth::RecurseCenter { id } => { - // XXX this needs some kind of real configuration system - let client_id = - std::env::var("TT_RECURSE_CENTER_CLIENT_ID").unwrap(); - let client_secret = - std::env::var("TT_RECURSE_CENTER_CLIENT_SECRET").unwrap(); - let redirect_url = - std::env::var("TT_RECURSE_CENTER_REDIRECT_URL").unwrap(); - let redirect_url = url::Url::parse(&redirect_url).unwrap(); - - conn.oauth_client = - Some(Box::new(crate::oauth::recurse_center::Oauth::new( - crate::oauth::recurse_center::config( - &client_id, - &client_secret, - redirect_url, - ), - ))); + oauth if oauth.is_oauth() => { + let (id, client) = match oauth { + crate::protocol::Auth::RecurseCenter { id } => { + // XXX this needs some kind of real configuration + // system + let client_id = + std::env::var("TT_RECURSE_CENTER_CLIENT_ID") + .unwrap(); + let client_secret = + std::env::var("TT_RECURSE_CENTER_CLIENT_SECRET") + .unwrap(); + let redirect_url = + std::env::var("TT_RECURSE_CENTER_REDIRECT_URL") + .unwrap(); + let redirect_url = + url::Url::parse(&redirect_url).unwrap(); + + ( + id, + Box::new( + crate::oauth::recurse_center::Oauth::new( + crate::oauth::recurse_center::config( + &client_id, + &client_secret, + redirect_url, + ), + ), + ), + ) + } + _ => unreachable!(), + }; + + log::info!( + "{}: login(oauth({}), {:?})", + conn.id, + auth.name(), + id + ); + conn.oauth_client = Some(client); - if let Some(id) = id { - log::info!( - "{}: login(recurse_center, {:?})", - conn.id, - id - ); + if let Some(_id) = id { // refresh unimplemented!() } else { let id = format!("{}", uuid::Uuid::new_v4()); - log::info!( - "{}: login(recurse_center, {:?})", - conn.id, - id - ); conn.state.login_oauth_start(term_type, &size); conn.send_message( @@ -401,6 +418,7 @@ impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static> ); } } + _ => unreachable!(), } Ok(()) @@ -540,7 +558,7 @@ impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static> term_type, size, .. - } => self.handle_message_login(conn, auth, &term_type, size), + } => self.handle_message_login(conn, &auth, &term_type, size), m => Err(Error::UnauthenticatedMessage { message: m }), } } |