From e05e5553e3bbfb0b8bebf3da785f4d1a16e353f3 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 30 Apr 2015 14:18:44 +0200 Subject: [PATCH] feat(serde): use serde instead of rustc_serialize That way, we can pretty-print the respective application secret strucures. This is primiarily of interest for the main client of this library, namely Google APIs RS. Version incremented. Fixes #2 --- Cargo.toml | 5 +++-- src/common.rs | 12 ++++++------ src/device.rs | 21 +++++++++++---------- src/helper.rs | 4 ++-- src/lib.rs | 12 ++++++++---- src/refresh.rs | 10 +++++----- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ec8a866..6ccb6e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "yup-oauth2" -version = "0.3.10" +version = "0.4.0" authors = ["Sebastian Thiel "] repository = "https://github.com/Byron/yup-oauth2" description = "A partial oauth2 implementation, providing the 'device' authorization flow" @@ -17,7 +17,8 @@ log = "*" mime = "*" url = "*" itertools = "*" -rustc-serialize = "*" +serde = "*" +serde_macros = "*" [dev-dependencies] getopts = "*" diff --git a/src/common.rs b/src/common.rs index f3e89c1..f3f21be 100644 --- a/src/common.rs +++ b/src/common.rs @@ -8,7 +8,7 @@ pub trait Flow { fn type_id() -> FlowType; } -#[derive(RustcDecodable)] +#[derive(Deserialize)] pub struct JsonError { pub error: String, pub error_description: Option, @@ -85,7 +85,7 @@ impl FromStr for Scheme { /// absolute terms. /// /// Utility methods make common queries easier, see `expired()`. -#[derive(Clone, PartialEq, Debug, RustcDecodable, RustcEncodable)] +#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] pub struct Token { /// used when authenticating calls to oauth2 enabled services. pub access_token: String, @@ -150,7 +150,7 @@ impl AsRef for FlowType { /// Represents either 'installed' or 'web' applications in a json secrets file. /// See `ConsoleApplicationSecret` for more information -#[derive(RustcDecodable, RustcEncodable, Clone, Default)] +#[derive(Deserialize, Serialize, Clone, Default)] pub struct ApplicationSecret { /// The client ID. pub client_id: String, @@ -173,7 +173,7 @@ pub struct ApplicationSecret { /// A type to facilitate reading and writing the json secret file /// as returned by the [google developer console](https://code.google.com/apis/console) -#[derive(RustcDecodable, RustcEncodable, Default)] +#[derive(Deserialize, Serialize, Default)] pub struct ConsoleApplicationSecret { pub web: Option, pub installed: Option @@ -189,8 +189,8 @@ pub mod tests { #[test] fn console_secret() { - use rustc_serialize::json; - match json::decode::(SECRET) { + use serde::json; + match json::from_str::(SECRET) { Ok(s) => assert!(s.installed.is_some() && s.web.is_none()), Err(err) => panic!(err), } diff --git a/src/device.rs b/src/device.rs index 41e69fe..5fb99c6 100644 --- a/src/device.rs +++ b/src/device.rs @@ -7,7 +7,7 @@ use hyper; use hyper::header::ContentType; use url::form_urlencoded; use itertools::Itertools; -use rustc_serialize::json; +use serde::json; use chrono::{DateTime,UTC}; use std::borrow::BorrowMut; use std::io::Read; @@ -202,7 +202,7 @@ impl DeviceFlow Ok(mut res) => { - #[derive(RustcDecodable)] + #[derive(Deserialize)] struct JsonData { device_code: String, user_code: String, @@ -212,17 +212,18 @@ impl DeviceFlow } let mut json_str = String::new(); - res.read_to_string(&mut json_str).ok().expect("string decode must work"); + res.read_to_string(&mut json_str).unwrap(); // check for error - match json::decode::(&json_str) { + match json::from_str::(&json_str) { Err(_) => {}, // ignore, move on Ok(res) => { return Err(RequestError::from(res)) } } - let decoded: JsonData = json::decode(&json_str).ok().expect("valid reply thanks to valid client_id and client_secret"); + println!("{:?}", json_str); + let decoded: JsonData = json::from_str(&json_str).unwrap(); self.device_code = decoded.device_code; let pi = PollInformation { @@ -294,17 +295,17 @@ impl DeviceFlow } Ok(mut res) => { let mut json_str = String::new(); - res.read_to_string(&mut json_str).ok().expect("string decode must work"); + res.read_to_string(&mut json_str).unwrap(); json_str } }; - #[derive(RustcDecodable)] + #[derive(Deserialize)] struct JsonError { error: String } - match json::decode::(&json_str) { + match json::from_str::(&json_str) { Err(_) => {}, // ignore, move on, it's not an error Ok(res) => { match res.error.as_ref() { @@ -320,7 +321,7 @@ impl DeviceFlow } // yes, we expect that ! - let mut t: Token = json::decode(&json_str).unwrap(); + let mut t: Token = json::from_str(&json_str).unwrap(); t.set_expiry_absolute(); let res = Ok(Some(t.clone())); @@ -345,7 +346,7 @@ pub mod tests { \"device_code\" : \"4/L9fTtLrhY96442SEuf1Rl3KLFg3y\",\r\n\ \"user_code\" : \"a9xfwk9c\",\r\n\ \"verification_url\" : \"http://www.google.com/device\",\r\n\ - \"expires_in\" : \"1800\",\r\n\ + \"expires_in\" : 1800,\r\n\ \"interval\" : 0\r\n\ }" "HTTP/1.1 200 OK\r\n\ diff --git a/src/helper.rs b/src/helper.rs index 58618cb..154bf51 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -481,9 +481,9 @@ mod tests { #[test] fn flow() { - use rustc_serialize::json; + use serde::json; - let secret = json::decode::(SECRET).unwrap().installed.unwrap(); + let secret = json::from_str::(SECRET).unwrap().installed.unwrap(); let res = Authenticator::new(&secret, DefaultAuthenticatorDelegate, hyper::Client::with_connector(::default()), ::default(), None) diff --git a/src/lib.rs b/src/lib.rs index 46cc0fe..876b1af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,17 +14,19 @@ //! The returned `Token` should be stored permanently to authorize future API requests. //! //! ```test_harness,no_run +//! #![feature(custom_derive, plugin)] +//! #![plugin(serde_macros)] //! extern crate hyper; //! extern crate yup_oauth2 as oauth2; -//! extern crate rustc_serialize; +//! extern crate serde; //! //! use oauth2::{Authenticator, DefaultAuthenticatorDelegate, PollInformation, ConsoleApplicationSecret, MemoryStorage, GetToken}; -//! use rustc_serialize::json; +//! use serde::json; //! use std::default::Default; //! # const SECRET: &'static str = "{\"installed\":{\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\",\"client_secret\":\"UqkDJd5RFwnHoiG5x5Rub8SI\",\"token_uri\":\"https://accounts.google.com/o/oauth2/token\",\"client_email\":\"\",\"redirect_uris\":[\"urn:ietf:wg:oauth:2.0:oob\",\"oob\"],\"client_x509_cert_url\":\"\",\"client_id\":\"14070749909-vgip2f1okm7bkvajhi9jugan6126io9v.apps.googleusercontent.com\",\"auth_provider_x509_cert_url\":\"https://www.googleapis.com/oauth2/v1/certs\"}}"; //! //! # #[test] fn device() { -//! let secret = json::decode::(SECRET).unwrap().installed.unwrap(); +//! let secret = json::from_str::(SECRET).unwrap().installed.unwrap(); //! let res = Authenticator::new(&secret, DefaultAuthenticatorDelegate, //! hyper::Client::new(), //! ::default(), None) @@ -59,6 +61,8 @@ //! }; //! # } //! ``` +#![feature(custom_derive, plugin)] +#![plugin(serde_macros)] extern crate chrono; #[macro_use] @@ -71,7 +75,7 @@ extern crate mime; extern crate url; extern crate time; extern crate itertools; -extern crate rustc_serialize as rustc_serialize; +extern crate serde; mod device; diff --git a/src/refresh.rs b/src/refresh.rs index 6d6bce9..aed0837 100644 --- a/src/refresh.rs +++ b/src/refresh.rs @@ -4,7 +4,7 @@ use device::GOOGLE_TOKEN_URL; use chrono::UTC; use hyper; use hyper::header::ContentType; -use rustc_serialize::json; +use serde::json; use url::form_urlencoded; use super::Token; use std::borrow::BorrowMut; @@ -83,19 +83,19 @@ impl RefreshFlow } Ok(mut res) => { let mut json_str = String::new(); - res.read_to_string(&mut json_str).ok().expect("string decode must work"); + res.read_to_string(&mut json_str).unwrap(); json_str } }; - #[derive(RustcDecodable)] + #[derive(Deserialize)] struct JsonToken { access_token: String, token_type: String, expires_in: i64, } - match json::decode::(&json_str) { + match json::from_str::(&json_str) { Err(_) => {}, Ok(res) => { self.result = RefreshResult::RefreshError(res.error, res.error_description); @@ -103,7 +103,7 @@ impl RefreshFlow } } - let t: JsonToken = json::decode(&json_str).unwrap(); + let t: JsonToken = json::from_str(&json_str).unwrap(); self.result = RefreshResult::Success(Token { access_token: t.access_token, token_type: t.token_type,