From e523ddb6ec9f1e9e8bcc51fbec02e364dbddaa72 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 23 Apr 2015 17:07:28 +0200 Subject: [PATCH] fix(API): adapt to changed yup-oauth2 API The latter changed a lot, to the better, and we handle the new return types accordingly. Related to #74 --- src/mako/api/lib/mbuild.mako | 22 +++++++++++++--------- src/rust/api/cmn.rs | 15 +++++++++------ src/rust/cli/cmn.rs | 35 ++++++++++++++++++++++++----------- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/mako/api/lib/mbuild.mako b/src/mako/api/lib/mbuild.mako index e66712d052..3e830a5d19 100644 --- a/src/mako/api/lib/mbuild.mako +++ b/src/mako/api/lib/mbuild.mako @@ -665,16 +665,20 @@ else { loop { % if default_scope: - let mut token = ${auth_call}.token(self.${api.properties.scopes}.keys()); - if token.is_none() { - token = dlg.token(); - } - if token.is_none() { - ${delegate_finish}(false); - return Err(Error::MissingToken) - } + let token = match ${auth_call}.token(self.${api.properties.scopes}.keys()) { + Ok(token) => token, + Err(err) => { + match dlg.token(&*err) { + Some(token) => token, + None => { + ${delegate_finish}(false); + return Err(Error::MissingToken(err)) + } + } + } + }; let auth_header = Authorization(oauth2::Scheme { token_type: oauth2::TokenType::Bearer, - access_token: token.unwrap().access_token }); + access_token: token.access_token }); % endif % if request_value: request_value_reader.seek(io::SeekFrom::Start(0)).unwrap(); diff --git a/src/rust/api/cmn.rs b/src/rust/api/cmn.rs index 6913284b82..3cdad1301a 100644 --- a/src/rust/api/cmn.rs +++ b/src/rust/api/cmn.rs @@ -125,9 +125,12 @@ pub trait Delegate { } /// Called whenever the Authenticator didn't yield a token. The delegate - /// may attempt to provide one, or just take is a general information about the - /// pending impending failure - fn token(&mut self) -> Option { + /// may attempt to provide one, or just take it as a general information about the + /// impending failure. + /// The given Error provides information about why the token couldn't be acquired in the + /// first place + fn token(&mut self, err: &error::Error) -> Option { + let _ = err; None } @@ -230,7 +233,7 @@ pub enum Error { MissingAPIKey, /// We required a Token, but didn't get one from the Authenticator - MissingToken, + MissingToken(Box), /// The delgate instructed to cancel the operation Cancelled, @@ -258,8 +261,8 @@ impl Display for Error { writeln!(f, "The application's API key was not found in the configuration").ok(); writeln!(f, "It is used as there are no Scopes defined for this method.") }, - Error::MissingToken => - writeln!(f, "Didn't obtain authentication token from authenticator"), + Error::MissingToken(ref err) => + writeln!(f, "Token retrieval failed with error: {}", err), Error::Cancelled => writeln!(f, "Operation cancelled by delegate"), Error::FieldClash(field) => diff --git a/src/rust/cli/cmn.rs b/src/rust/cli/cmn.rs index d0d50a860e..0cd3d189af 100644 --- a/src/rust/cli/cmn.rs +++ b/src/rust/cli/cmn.rs @@ -169,25 +169,38 @@ impl JsonTokenStorage { } impl TokenStorage for JsonTokenStorage { + type Error = io::Error; + // NOTE: logging might be interesting, currently we swallow all errors - fn set(&mut self, scope_hash: u64, token: Option) { + fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option) -> Option { let json_token = json::encode(&token).unwrap(); - let res = fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash)); - if let Ok(mut f) = res { - f.write(json_token.as_bytes()).ok(); + match fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash)) { + Ok(mut f) => { + match f.write(json_token.as_bytes()) { + Ok(_) => None, + Err(io_err) => Some(io_err), + } + }, + Err(io_err) => Some(io_err) } } - fn get(&self, scope_hash: u64) -> Option { - if let Ok(mut f) = fs::File::open(&self.path(scope_hash)) { - let mut json_string = String::new(); - if let Ok(_) = f.read_to_string(&mut json_string) { - if let Ok(token) = json::decode::(&json_string) { - return Some(token) + fn get(&self, scope_hash: u64, _: &Vec<&str>) -> Result, io::Error> { + match fs::File::open(&self.path(scope_hash)) { + Ok(mut f) => { + let mut json_string = String::new(); + match f.read_to_string(&mut json_string) { + Ok(_) => Ok(Some(json::decode::(&json_string).unwrap())), + Err(io_err) => Err(io_err), + } + }, + Err(io_err) => { + match io_err.kind() { + io::ErrorKind::NotFound => Ok(None), + _ => Err(io_err) } } } - None } }