From ba0b8f366ad3d972379f8a18892670a80fcf1a34 Mon Sep 17 00:00:00 2001 From: Glenn Griffin Date: Wed, 13 Nov 2019 13:55:32 -0800 Subject: [PATCH] Rename RequestError to Error RequestError is the error value that encompasses all errors from the authenticators. Their is an established convention of using Error as the name for those types. --- src/authenticator.rs | 8 ++--- src/authenticator_delegate.rs | 16 ++++----- src/device.rs | 21 +++++------ src/error.rs | 68 +++++++++++++++++------------------ src/installed.rs | 18 +++++----- src/lib.rs | 2 +- src/refresh.rs | 2 +- src/service_account.rs | 21 +++++------ 8 files changed, 74 insertions(+), 82 deletions(-) diff --git a/src/authenticator.rs b/src/authenticator.rs index b1824e4..a54cede 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -2,7 +2,7 @@ use crate::authenticator_delegate::{ AuthenticatorDelegate, DefaultAuthenticatorDelegate, FlowDelegate, }; use crate::device::DeviceFlow; -use crate::error::RequestError; +use crate::error::Error; use crate::installed::{InstalledFlow, InstalledFlowReturnMethod}; use crate::refresh::RefreshFlow; use crate::storage::{self, Storage}; @@ -27,7 +27,7 @@ impl Authenticator where C: hyper::client::connect::Connect + 'static, { - pub async fn token<'a, T>(&'a self, scopes: &'a [T]) -> Result + pub async fn token<'a, T>(&'a self, scopes: &'a [T]) -> Result where T: AsRef, { @@ -239,7 +239,7 @@ impl AuthenticatorBuilder { mod private { use crate::device::DeviceFlow; - use crate::error::RequestError; + use crate::error::Error; use crate::installed::InstalledFlow; use crate::types::{ApplicationSecret, Token}; @@ -266,7 +266,7 @@ mod private { hyper_client: &'a hyper::Client, app_secret: &'a ApplicationSecret, scopes: &'a [T], - ) -> Result + ) -> Result where T: AsRef, C: hyper::client::connect::Connect + 'static, diff --git a/src/authenticator_delegate.rs b/src/authenticator_delegate.rs index 92f5329..86c00dc 100644 --- a/src/authenticator_delegate.rs +++ b/src/authenticator_delegate.rs @@ -1,10 +1,10 @@ use hyper; -use std::error::Error; +use std::error::Error as StdError; use std::fmt; use std::pin::Pin; -use crate::error::{PollError, RefreshError, RequestError}; +use crate::error::{Error, PollError, RefreshError}; use chrono::{DateTime, Local, Utc}; use std::time::Duration; @@ -58,8 +58,8 @@ impl fmt::Display for PollError { } } -impl Error for PollError { - fn source(&self) -> Option<&(dyn Error + 'static)> { +impl StdError for PollError { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match *self { PollError::HttpError(ref e) => Some(e), _ => None, @@ -80,7 +80,7 @@ pub trait AuthenticatorDelegate: Send + Sync { } /// The server denied the attempt to obtain a request code - fn request_failure(&self, _: RequestError) {} + fn request_failure(&self, _: Error) {} /// Called if we could not acquire a refresh token for a reason possibly specified /// by the server. @@ -140,7 +140,7 @@ pub trait FlowDelegate: Send + Sync { &'a self, url: &'a str, need_code: bool, - ) -> Pin>> + Send + 'a>> + ) -> Pin>> + Send + 'a>> { Box::pin(present_user_url(url, need_code)) } @@ -149,7 +149,7 @@ pub trait FlowDelegate: Send + Sync { async fn present_user_url( url: &str, need_code: bool, -) -> Result> { +) -> Result> { if need_code { println!( "Please direct your browser to {}, follow the instructions and enter the \ @@ -163,7 +163,7 @@ async fn present_user_url( { Err(err) => { println!("{:?}", err); - Err(Box::new(err) as Box) + Err(Box::new(err) as Box) } Ok(_) => Ok(user_input), } diff --git a/src/device.rs b/src/device.rs index e1bdff6..1943d4e 100644 --- a/src/device.rs +++ b/src/device.rs @@ -10,7 +10,7 @@ use serde_json as json; use url::form_urlencoded; use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate, PollInformation, Retry}; -use crate::error::{JsonErrorOr, PollError, RequestError}; +use crate::error::{Error, JsonErrorOr, PollError}; use crate::types::{ApplicationSecret, Token}; pub const GOOGLE_DEVICE_CODE_URL: &str = "https://accounts.google.com/o/oauth2/device/code"; @@ -46,7 +46,7 @@ impl DeviceFlow { hyper_client: &hyper::Client, app_secret: &ApplicationSecret, scopes: &[T], - ) -> Result + ) -> Result where T: AsRef, C: hyper::client::connect::Connect + 'static, @@ -65,7 +65,7 @@ impl DeviceFlow { self.wait_duration, ) .await - .map_err(|_| RequestError::Poll(PollError::TimedOut))? + .map_err(|_| Error::Poll(PollError::TimedOut))? } async fn wait_for_device_token( @@ -75,7 +75,7 @@ impl DeviceFlow { pollinf: &PollInformation, device_code: &str, grant_type: &str, - ) -> Result + ) -> Result where C: hyper::client::connect::Connect + 'static, { @@ -93,15 +93,13 @@ impl DeviceFlow { .await { Ok(None) => match self.flow_delegate.pending(&pollinf) { - Retry::Abort | Retry::Skip => { - return Err(RequestError::Poll(PollError::TimedOut)) - } + Retry::Abort | Retry::Skip => return Err(Error::Poll(PollError::TimedOut)), Retry::After(d) => d, }, Ok(Some(tok)) => return Ok(tok), Err(e @ PollError::AccessDenied) | Err(e @ PollError::TimedOut) - | Err(e @ PollError::Expired(_)) => return Err(RequestError::Poll(e)), + | Err(e @ PollError::Expired(_)) => return Err(Error::Poll(e)), Err(ref e) => { error!("Unknown error from poll token api: {}", e); pollinf.interval @@ -130,7 +128,7 @@ impl DeviceFlow { client: &hyper::Client, device_code_url: &str, scopes: &[T], - ) -> Result<(PollInformation, String), RequestError> + ) -> Result<(PollInformation, String), Error> where T: AsRef, C: hyper::client::connect::Connect + 'static, @@ -148,10 +146,7 @@ impl DeviceFlow { .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded") .body(hyper::Body::from(req)) .unwrap(); - let resp = client - .request(req) - .await - .map_err(RequestError::ClientError)?; + let resp = client.request(req).await.map_err(Error::ClientError)?; // This return type is defined in https://tools.ietf.org/html/draft-ietf-oauth-device-flow-15#section-3.2 // The alias is present as Google use a non-standard name for verification_uri. // According to the standard interval is optional, however, all tested implementations provide it. diff --git a/src/error.rs b/src/error.rs index 191007a..d1f1aa9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use std::error::Error as StdError; use std::fmt; use std::io; @@ -45,7 +45,7 @@ pub enum PollError { /// Encapsulates all possible results of the `token(...)` operation #[derive(Debug)] -pub enum RequestError { +pub enum Error { /// Indicates connection failure ClientError(hyper::Error), /// The OAuth client was not found @@ -69,75 +69,75 @@ pub enum RequestError { /// An error occurred while refreshing tokens. Refresh(RefreshError), /// Error in token cache layer - Cache(Box), + Cache(Box), } -impl From for RequestError { - fn from(error: hyper::Error) -> RequestError { - RequestError::ClientError(error) +impl From for Error { + fn from(error: hyper::Error) -> Error { + Error::ClientError(error) } } -impl From for RequestError { - fn from(value: JsonError) -> RequestError { +impl From for Error { + fn from(value: JsonError) -> Error { match &*value.error { - "invalid_client" => RequestError::InvalidClient, - "invalid_scope" => RequestError::InvalidScope( + "invalid_client" => Error::InvalidClient, + "invalid_scope" => Error::InvalidScope( value .error_description .unwrap_or_else(|| "no description provided".to_string()), ), - _ => RequestError::NegativeServerResponse(value.error, value.error_description), + _ => Error::NegativeServerResponse(value.error, value.error_description), } } } -impl From for RequestError { - fn from(value: serde_json::Error) -> RequestError { - RequestError::JSONError(value) +impl From for Error { + fn from(value: serde_json::Error) -> Error { + Error::JSONError(value) } } -impl From for RequestError { - fn from(value: RefreshError) -> RequestError { - RequestError::Refresh(value) +impl From for Error { + fn from(value: RefreshError) -> Error { + Error::Refresh(value) } } -impl fmt::Display for RequestError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { - RequestError::ClientError(ref err) => err.fmt(f), - RequestError::InvalidClient => "Invalid Client".fmt(f), - RequestError::InvalidScope(ref scope) => writeln!(f, "Invalid Scope: '{}'", scope), - RequestError::NegativeServerResponse(ref error, ref desc) => { + Error::ClientError(ref err) => err.fmt(f), + Error::InvalidClient => "Invalid Client".fmt(f), + Error::InvalidScope(ref scope) => writeln!(f, "Invalid Scope: '{}'", scope), + Error::NegativeServerResponse(ref error, ref desc) => { error.fmt(f)?; if let Some(ref desc) = *desc { write!(f, ": {}", desc)?; } "\n".fmt(f) } - RequestError::BadServerResponse(ref s) => s.fmt(f), - RequestError::JSONError(ref e) => format!( + Error::BadServerResponse(ref s) => s.fmt(f), + Error::JSONError(ref e) => format!( "JSON Error; this might be a bug with unexpected server responses! {}", e ) .fmt(f), - RequestError::UserError(ref s) => s.fmt(f), - RequestError::LowLevelError(ref e) => e.fmt(f), - RequestError::Poll(ref pe) => pe.fmt(f), - RequestError::Refresh(ref rr) => format!("{:?}", rr).fmt(f), - RequestError::Cache(ref e) => e.fmt(f), + Error::UserError(ref s) => s.fmt(f), + Error::LowLevelError(ref e) => e.fmt(f), + Error::Poll(ref pe) => pe.fmt(f), + Error::Refresh(ref rr) => format!("{:?}", rr).fmt(f), + Error::Cache(ref e) => e.fmt(f), } } } -impl Error for RequestError { - fn source(&self) -> Option<&(dyn Error + 'static)> { +impl StdError for Error { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match *self { - RequestError::ClientError(ref err) => Some(err), - RequestError::LowLevelError(ref err) => Some(err), - RequestError::JSONError(ref err) => Some(err), + Error::ClientError(ref err) => Some(err), + Error::LowLevelError(ref err) => Some(err), + Error::JSONError(ref err) => Some(err), _ => None, } } diff --git a/src/installed.rs b/src/installed.rs index b70979f..4fbdfb6 100644 --- a/src/installed.rs +++ b/src/installed.rs @@ -17,7 +17,7 @@ use url::form_urlencoded; use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET}; use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate}; -use crate::error::{JsonErrorOr, RequestError}; +use crate::error::{Error, JsonErrorOr}; use crate::types::{ApplicationSecret, Token}; const OOB_REDIRECT_URI: &str = "urn:ietf:wg:oauth:2.0:oob"; @@ -90,7 +90,7 @@ impl InstalledFlow { hyper_client: &hyper::Client, app_secret: &ApplicationSecret, scopes: &[T], - ) -> Result + ) -> Result where T: AsRef, C: hyper::client::connect::Connect + 'static, @@ -112,7 +112,7 @@ impl InstalledFlow { hyper_client: &hyper::Client, app_secret: &ApplicationSecret, scopes: &[T], - ) -> Result + ) -> Result where T: AsRef, C: hyper::client::connect::Connect + 'static, @@ -136,7 +136,7 @@ impl InstalledFlow { } code } - _ => return Err(RequestError::UserError("couldn't read code".to_string())), + _ => return Err(Error::UserError("couldn't read code".to_string())), }; self.exchange_auth_code(&authcode, hyper_client, app_secret, None) .await @@ -147,7 +147,7 @@ impl InstalledFlow { hyper_client: &hyper::Client, app_secret: &ApplicationSecret, scopes: &[T], - ) -> Result + ) -> Result where T: AsRef, C: hyper::client::connect::Connect + 'static, @@ -185,7 +185,7 @@ impl InstalledFlow { hyper_client: &hyper::Client, app_secret: &ApplicationSecret, server_addr: Option, - ) -> Result + ) -> Result where C: hyper::client::connect::Connect + 'static, { @@ -194,12 +194,12 @@ impl InstalledFlow { let resp = hyper_client .request(request) .await - .map_err(RequestError::ClientError)?; + .map_err(Error::ClientError)?; let body = resp .into_body() .try_concat() .await - .map_err(RequestError::ClientError)?; + .map_err(Error::ClientError)?; #[derive(Deserialize)] struct JSONTokenResponse { @@ -276,7 +276,7 @@ struct InstalledFlowServer { } impl InstalledFlowServer { - fn run() -> Result { + fn run() -> Result { use hyper::service::{make_service_fn, service_fn}; let (auth_code_tx, auth_code_rx) = oneshot::channel::(); let (trigger_shutdown_tx, trigger_shutdown_rx) = oneshot::channel::<()>(); diff --git a/src/lib.rs b/src/lib.rs index 1e63c14..bb82e6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,5 +92,5 @@ pub use crate::installed::InstalledFlowReturnMethod; pub use crate::service_account::{ServiceAccountAuthenticator, ServiceAccountKey}; #[doc(inline)] -pub use crate::error::RequestError; +pub use crate::error::Error; pub use crate::types::{ApplicationSecret, ConsoleApplicationSecret, Token}; diff --git a/src/refresh.rs b/src/refresh.rs index f6826be..03847e2 100644 --- a/src/refresh.rs +++ b/src/refresh.rs @@ -35,7 +35,7 @@ impl RefreshFlow { client_secret: &ApplicationSecret, refresh_token: &str, ) -> Result { - // TODO: Does this function ever return RequestError? Maybe have it just return RefreshResult. + // TODO: Does this function ever return Error? Maybe have it just return RefreshResult. let req = form_urlencoded::Serializer::new(String::new()) .extend_pairs(&[ ("client_id", client_secret.client_id.as_str()), diff --git a/src/service_account.rs b/src/service_account.rs index c2732a2..66ab1df 100644 --- a/src/service_account.rs +++ b/src/service_account.rs @@ -14,7 +14,7 @@ use std::sync::Mutex; use crate::authenticator::{DefaultHyperClient, HyperClientBuilder}; -use crate::error::{JsonErrorOr, RequestError}; +use crate::error::{Error, JsonErrorOr}; use crate::storage::{self, Storage}; use crate::types::Token; @@ -228,7 +228,7 @@ where }) } - pub async fn token(&self, scopes: &[T]) -> Result + pub async fn token(&self, scopes: &[T]) -> Result where T: AsRef, { @@ -256,13 +256,13 @@ where subject: Option<&str>, key: &ServiceAccountKey, scopes: &[T], - ) -> Result + ) -> Result where T: AsRef, { let claims = Claims::new(key, scopes, subject); let signed = signer.sign_claims(&claims).map_err(|_| { - RequestError::LowLevelError(io::Error::new( + Error::LowLevelError(io::Error::new( io::ErrorKind::Other, "unable to sign claims", )) @@ -274,15 +274,12 @@ where .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded") .body(hyper::Body::from(rqbody)) .unwrap(); - let response = client - .request(request) - .await - .map_err(RequestError::ClientError)?; + let response = client.request(request).await.map_err(Error::ClientError)?; let body = response .into_body() .try_concat() .await - .map_err(RequestError::ClientError)?; + .map_err(Error::ClientError)?; /// This is the schema of the server's response. #[derive(Deserialize, Debug)] @@ -308,7 +305,7 @@ where expires_in_timestamp: Some(expires_ts), }) } - token => Err(RequestError::BadServerResponse(format!( + token => Err(Error::BadServerResponse(format!( "Token response lacks fields: {:?}", token ))), @@ -380,7 +377,7 @@ mod tests { .await?; assert!(tok.access_token.contains("ya29.c.ElouBywiys0Ly")); assert_eq!(Some(3600), tok.expires_in); - Ok(()) as Result<(), RequestError> + Ok(()) as Result<(), Error> }; rt.block_on(fut).expect("block_on"); @@ -400,7 +397,7 @@ mod tests { .await?; assert!(tok.access_token.contains("ya29.c.ElouBywiys0Ly")); assert_eq!(Some(3600), tok.expires_in); - Ok(()) as Result<(), RequestError> + Ok(()) as Result<(), Error> }; rt.block_on(fut).expect("block_on 2");