mirror of
https://github.com/OMGeeky/yup-oauth2.git
synced 2026-01-05 19:00:29 +01:00
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.
This commit is contained in:
@@ -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<C> Authenticator<C>
|
||||
where
|
||||
C: hyper::client::connect::Connect + 'static,
|
||||
{
|
||||
pub async fn token<'a, T>(&'a self, scopes: &'a [T]) -> Result<Token, RequestError>
|
||||
pub async fn token<'a, T>(&'a self, scopes: &'a [T]) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
{
|
||||
@@ -239,7 +239,7 @@ impl<C> AuthenticatorBuilder<C, InstalledFlow> {
|
||||
|
||||
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<C>,
|
||||
app_secret: &'a ApplicationSecret,
|
||||
scopes: &'a [T],
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
C: hyper::client::connect::Connect + 'static,
|
||||
|
||||
@@ -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<Box<dyn Future<Output = Result<String, Box<dyn Error + Send + Sync>>> + Send + 'a>>
|
||||
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn StdError + Send + Sync>>> + 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<String, Box<dyn Error + Send + Sync>> {
|
||||
) -> Result<String, Box<dyn StdError + Send + Sync>> {
|
||||
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<dyn Error + Send + Sync>)
|
||||
Err(Box::new(err) as Box<dyn StdError + Send + Sync>)
|
||||
}
|
||||
Ok(_) => Ok(user_input),
|
||||
}
|
||||
|
||||
@@ -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<C>,
|
||||
app_secret: &ApplicationSecret,
|
||||
scopes: &[T],
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
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<C>(
|
||||
@@ -75,7 +75,7 @@ impl DeviceFlow {
|
||||
pollinf: &PollInformation,
|
||||
device_code: &str,
|
||||
grant_type: &str,
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
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<C>,
|
||||
device_code_url: &str,
|
||||
scopes: &[T],
|
||||
) -> Result<(PollInformation, String), RequestError>
|
||||
) -> Result<(PollInformation, String), Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
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.
|
||||
|
||||
68
src/error.rs
68
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<dyn Error + Send + Sync>),
|
||||
Cache(Box<dyn StdError + Send + Sync>),
|
||||
}
|
||||
|
||||
impl From<hyper::Error> for RequestError {
|
||||
fn from(error: hyper::Error) -> RequestError {
|
||||
RequestError::ClientError(error)
|
||||
impl From<hyper::Error> for Error {
|
||||
fn from(error: hyper::Error) -> Error {
|
||||
Error::ClientError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<JsonError> for RequestError {
|
||||
fn from(value: JsonError) -> RequestError {
|
||||
impl From<JsonError> 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<serde_json::Error> for RequestError {
|
||||
fn from(value: serde_json::Error) -> RequestError {
|
||||
RequestError::JSONError(value)
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(value: serde_json::Error) -> Error {
|
||||
Error::JSONError(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RefreshError> for RequestError {
|
||||
fn from(value: RefreshError) -> RequestError {
|
||||
RequestError::Refresh(value)
|
||||
impl From<RefreshError> 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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<C>,
|
||||
app_secret: &ApplicationSecret,
|
||||
scopes: &[T],
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
C: hyper::client::connect::Connect + 'static,
|
||||
@@ -112,7 +112,7 @@ impl InstalledFlow {
|
||||
hyper_client: &hyper::Client<C>,
|
||||
app_secret: &ApplicationSecret,
|
||||
scopes: &[T],
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
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<C>,
|
||||
app_secret: &ApplicationSecret,
|
||||
scopes: &[T],
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
C: hyper::client::connect::Connect + 'static,
|
||||
@@ -185,7 +185,7 @@ impl InstalledFlow {
|
||||
hyper_client: &hyper::Client<C>,
|
||||
app_secret: &ApplicationSecret,
|
||||
server_addr: Option<SocketAddr>,
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
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<Self, RequestError> {
|
||||
fn run() -> Result<Self, Error> {
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
let (auth_code_tx, auth_code_rx) = oneshot::channel::<String>();
|
||||
let (trigger_shutdown_tx, trigger_shutdown_rx) = oneshot::channel::<()>();
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -35,7 +35,7 @@ impl RefreshFlow {
|
||||
client_secret: &ApplicationSecret,
|
||||
refresh_token: &str,
|
||||
) -> Result<Token, RefreshError> {
|
||||
// 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()),
|
||||
|
||||
@@ -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<T>(&self, scopes: &[T]) -> Result<Token, RequestError>
|
||||
pub async fn token<T>(&self, scopes: &[T]) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
{
|
||||
@@ -256,13 +256,13 @@ where
|
||||
subject: Option<&str>,
|
||||
key: &ServiceAccountKey,
|
||||
scopes: &[T],
|
||||
) -> Result<Token, RequestError>
|
||||
) -> Result<Token, Error>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
{
|
||||
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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user