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
This commit is contained in:
Sebastian Thiel
2015-04-23 17:07:28 +02:00
parent 797f289886
commit e523ddb6ec
3 changed files with 46 additions and 26 deletions

View File

@@ -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();

View File

@@ -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<oauth2::Token> {
/// 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<oauth2::Token> {
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<error::Error>),
/// 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) =>

View File

@@ -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<Token>) {
fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option<Token>) -> Option<io::Error> {
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<Token> {
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::<Token>(&json_string) {
return Some(token)
fn get(&self, scope_hash: u64, _: &Vec<&str>) -> Result<Option<Token>, 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::<Token>(&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
}
}