Skip to content

Commit 8afc76a

Browse files
committed
feat(CLI): Implementation of JsonTokenStorage
It's also used by the code, replacing the previous standing, MemoryStorage. Fixes #58
1 parent f71c286 commit 8afc76a

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

src/mako/cli/lib/engine.mako

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
hub_type_name = 'api::' + hub_type(c.schemas, util.canonical_name())
1313
%>\
1414
mod cmn;
15-
use cmn::InvalidOptionsError;
16-
use std::default::Default;
15+
use cmn::{InvalidOptionsError, JsonTokenStorage};
1716
18-
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, MemoryStorage};
17+
use oauth2::{Authenticator, DefaultAuthenticatorDelegate};
1918
2019
struct Engine {
2120
opt: Options,
2221
config_dir: String,
23-
hub: ${hub_type_name}<hyper::Client, Authenticator<DefaultAuthenticatorDelegate, MemoryStorage, hyper::Client>>,
22+
hub: ${hub_type_name}<hyper::Client, Authenticator<DefaultAuthenticatorDelegate, JsonTokenStorage, hyper::Client>>,
2423
}
2524
2625
@@ -83,7 +82,10 @@ self.opt.${cmd_ident(method)} {
8382
8483
let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
8584
hyper::Client::new(),
86-
<MemoryStorage as Default>::default(), None);
85+
JsonTokenStorage {
86+
program_name: "${util.program_name()}",
87+
db_dir: config_dir.clone(),
88+
}, None);
8789
let engine = Engine {
8890
opt: opt,
8991
config_dir: config_dir,

src/rust/cli/cmn.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
use oauth2::{ApplicationSecret, ConsoleApplicationSecret};
1+
use oauth2::{ApplicationSecret, ConsoleApplicationSecret, TokenStorage, Token};
22
use rustc_serialize::json;
33

44
use std::fs;
55
use std::env;
66
use std::io;
77
use std::fmt;
8-
use std::path::Path;
8+
use std::path::{Path, PathBuf};
99

1010
use std::io::{Write, Read};
1111

1212
use std::default::Default;
1313

14+
15+
pub struct JsonTokenStorage {
16+
pub program_name: &'static str,
17+
pub db_dir: String,
18+
}
19+
20+
impl JsonTokenStorage {
21+
fn path(&self, scope_hash: u64) -> PathBuf {
22+
Path::new(&self.db_dir).join(&format!("{}-token-{}.json", self.program_name, scope_hash))
23+
}
24+
}
25+
26+
impl TokenStorage for JsonTokenStorage {
27+
// NOTE: logging might be interesting, currently we swallow all errors
28+
fn set(&mut self, scope_hash: u64, token: Option<Token>) {
29+
let json_token = json::encode(&token).unwrap();
30+
let res = fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash));
31+
if let Ok(mut f) = res {
32+
f.write(json_token.as_bytes()).ok();
33+
}
34+
}
35+
36+
fn get(&self, scope_hash: u64) -> Option<Token> {
37+
if let Ok(mut f) = fs::File::open(&self.path(scope_hash)) {
38+
let mut json_string = String::new();
39+
if let Ok(_) = f.read_to_string(&mut json_string) {
40+
if let Ok(token) = json::decode::<Token>(&json_string) {
41+
return Some(token)
42+
}
43+
}
44+
}
45+
None
46+
}
47+
}
48+
49+
1450
#[derive(Debug)]
1551
pub enum ApplicationSecretError {
1652
DecoderError((String, json::DecoderError)),

0 commit comments

Comments
 (0)