Skip to content

Commit e523ddb

Browse files
committed
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
1 parent 797f289 commit e523ddb

3 files changed

Lines changed: 46 additions & 26 deletions

File tree

src/mako/api/lib/mbuild.mako

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -665,16 +665,20 @@ else {
665665
666666
loop {
667667
% if default_scope:
668-
let mut token = ${auth_call}.token(self.${api.properties.scopes}.keys());
669-
if token.is_none() {
670-
token = dlg.token();
671-
}
672-
if token.is_none() {
673-
${delegate_finish}(false);
674-
return Err(Error::MissingToken)
675-
}
668+
let token = match ${auth_call}.token(self.${api.properties.scopes}.keys()) {
669+
Ok(token) => token,
670+
Err(err) => {
671+
match dlg.token(&*err) {
672+
Some(token) => token,
673+
None => {
674+
${delegate_finish}(false);
675+
return Err(Error::MissingToken(err))
676+
}
677+
}
678+
}
679+
};
676680
let auth_header = Authorization(oauth2::Scheme { token_type: oauth2::TokenType::Bearer,
677-
access_token: token.unwrap().access_token });
681+
access_token: token.access_token });
678682
% endif
679683
% if request_value:
680684
request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();

src/rust/api/cmn.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ pub trait Delegate {
125125
}
126126

127127
/// Called whenever the Authenticator didn't yield a token. The delegate
128-
/// may attempt to provide one, or just take is a general information about the
129-
/// pending impending failure
130-
fn token(&mut self) -> Option<oauth2::Token> {
128+
/// may attempt to provide one, or just take it as a general information about the
129+
/// impending failure.
130+
/// The given Error provides information about why the token couldn't be acquired in the
131+
/// first place
132+
fn token(&mut self, err: &error::Error) -> Option<oauth2::Token> {
133+
let _ = err;
131134
None
132135
}
133136

@@ -230,7 +233,7 @@ pub enum Error {
230233
MissingAPIKey,
231234

232235
/// We required a Token, but didn't get one from the Authenticator
233-
MissingToken,
236+
MissingToken(Box<error::Error>),
234237

235238
/// The delgate instructed to cancel the operation
236239
Cancelled,
@@ -258,8 +261,8 @@ impl Display for Error {
258261
writeln!(f, "The application's API key was not found in the configuration").ok();
259262
writeln!(f, "It is used as there are no Scopes defined for this method.")
260263
},
261-
Error::MissingToken =>
262-
writeln!(f, "Didn't obtain authentication token from authenticator"),
264+
Error::MissingToken(ref err) =>
265+
writeln!(f, "Token retrieval failed with error: {}", err),
263266
Error::Cancelled =>
264267
writeln!(f, "Operation cancelled by delegate"),
265268
Error::FieldClash(field) =>

src/rust/cli/cmn.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,38 @@ impl JsonTokenStorage {
169169
}
170170

171171
impl TokenStorage for JsonTokenStorage {
172+
type Error = io::Error;
173+
172174
// NOTE: logging might be interesting, currently we swallow all errors
173-
fn set(&mut self, scope_hash: u64, token: Option<Token>) {
175+
fn set(&mut self, scope_hash: u64, _: &Vec<&str>, token: Option<Token>) -> Option<io::Error> {
174176
let json_token = json::encode(&token).unwrap();
175-
let res = fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash));
176-
if let Ok(mut f) = res {
177-
f.write(json_token.as_bytes()).ok();
177+
match fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash)) {
178+
Ok(mut f) => {
179+
match f.write(json_token.as_bytes()) {
180+
Ok(_) => None,
181+
Err(io_err) => Some(io_err),
182+
}
183+
},
184+
Err(io_err) => Some(io_err)
178185
}
179186
}
180187

181-
fn get(&self, scope_hash: u64) -> Option<Token> {
182-
if let Ok(mut f) = fs::File::open(&self.path(scope_hash)) {
183-
let mut json_string = String::new();
184-
if let Ok(_) = f.read_to_string(&mut json_string) {
185-
if let Ok(token) = json::decode::<Token>(&json_string) {
186-
return Some(token)
188+
fn get(&self, scope_hash: u64, _: &Vec<&str>) -> Result<Option<Token>, io::Error> {
189+
match fs::File::open(&self.path(scope_hash)) {
190+
Ok(mut f) => {
191+
let mut json_string = String::new();
192+
match f.read_to_string(&mut json_string) {
193+
Ok(_) => Ok(Some(json::decode::<Token>(&json_string).unwrap())),
194+
Err(io_err) => Err(io_err),
195+
}
196+
},
197+
Err(io_err) => {
198+
match io_err.kind() {
199+
io::ErrorKind::NotFound => Ok(None),
200+
_ => Err(io_err)
187201
}
188202
}
189203
}
190-
None
191204
}
192205
}
193206

0 commit comments

Comments
 (0)