From e5aa32b3cf83ce282f2fa916d71ac31b179db0b8 Mon Sep 17 00:00:00 2001 From: Glenn Griffin Date: Wed, 13 Nov 2019 14:32:39 -0800 Subject: [PATCH] Tidy up some imports. No more need to macro_use serde. Order the imports consistently (albeit somewhat arbitrary), starting with items from this crate, followed by std, followed by external crates. --- Cargo.toml | 3 +-- src/authenticator_delegate.rs | 12 ++++-------- src/device.rs | 18 +++++++++--------- src/error.rs | 1 + src/helper.rs | 9 ++------- src/installed.rs | 10 +++++----- src/lib.rs | 3 --- src/refresh.rs | 5 ++--- src/service_account.rs | 15 +++++---------- src/storage.rs | 3 ++- src/types.rs | 1 + 11 files changed, 32 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 88618be..7f0ed0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,9 +18,8 @@ hyper = {version = "0.13.0-alpha.4", features = ["unstable-stream"]} hyper-rustls = "=0.18.0-alpha.2" log = "0.4" rustls = "0.16" -serde = "1.0" +serde = {version = "1.0", features = ["derive"]} serde_json = "1.0" -serde_derive = "1.0" url = "1" futures-preview = "=0.3.0-alpha.19" tokio = "=0.2.0-alpha.6" diff --git a/src/authenticator_delegate.rs b/src/authenticator_delegate.rs index 86c00dc..e5ad5e3 100644 --- a/src/authenticator_delegate.rs +++ b/src/authenticator_delegate.rs @@ -1,17 +1,12 @@ -use hyper; +use crate::error::{Error, PollError, RefreshError}; use std::error::Error as StdError; use std::fmt; use std::pin::Pin; - -use crate::error::{Error, PollError, RefreshError}; - -use chrono::{DateTime, Local, Utc}; use std::time::Duration; +use chrono::{DateTime, Local, Utc}; use futures::prelude::*; -use tio::AsyncBufReadExt; -use tokio::io as tio; /// A utility type to indicate how operations DeviceFlowHelper operations should be retried pub enum Retry { @@ -150,6 +145,7 @@ async fn present_user_url( url: &str, need_code: bool, ) -> Result> { + use tokio::io::AsyncBufReadExt; if need_code { println!( "Please direct your browser to {}, follow the instructions and enter the \ @@ -157,7 +153,7 @@ async fn present_user_url( url ); let mut user_input = String::new(); - match tio::BufReader::new(tio::stdin()) + match tokio::io::BufReader::new(tokio::io::stdin()) .read_line(&mut user_input) .await { diff --git a/src/device.rs b/src/device.rs index 1943d4e..90ec9f3 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,18 +1,17 @@ +use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate, PollInformation, Retry}; +use crate::error::{Error, JsonErrorOr, PollError}; +use crate::types::{ApplicationSecret, Token}; + use std::borrow::Cow; use std::time::Duration; use ::log::error; use chrono::{DateTime, Utc}; use futures::prelude::*; -use hyper; use hyper::header; -use serde_json as json; +use serde::Deserialize; use url::form_urlencoded; -use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate, PollInformation, Retry}; -use crate::error::{Error, JsonErrorOr, PollError}; -use crate::types::{ApplicationSecret, Token}; - pub const GOOGLE_DEVICE_CODE_URL: &str = "https://accounts.google.com/o/oauth2/device/code"; // https://developers.google.com/identity/protocols/OAuth2ForDevices#step-4:-poll-googles-authorization-server @@ -162,7 +161,8 @@ impl DeviceFlow { } let json_bytes = resp.into_body().try_concat().await?; - let decoded: JsonData = json::from_slice::>(&json_bytes)?.into_result()?; + let decoded: JsonData = + serde_json::from_slice::>(&json_bytes)?.into_result()?; let expires_in = decoded.expires_in.unwrap_or(60 * 60); let pi = PollInformation { user_code: decoded.user_code, @@ -235,7 +235,7 @@ impl DeviceFlow { error: String, } - match json::from_slice::(&body) { + match serde_json::from_slice::(&body) { Err(_) => {} // ignore, move on, it's not an error Ok(res) => { match res.error.as_ref() { @@ -255,7 +255,7 @@ impl DeviceFlow { } // yes, we expect that ! - let mut t: Token = json::from_slice(&body).unwrap(); + let mut t: Token = serde_json::from_slice(&body).unwrap(); t.set_expiry_absolute(); Ok(Some(t)) diff --git a/src/error.rs b/src/error.rs index d1f1aa9..a8b66e1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ use std::fmt; use std::io; use chrono::{DateTime, Utc}; +use serde::Deserialize; #[derive(Deserialize, Debug)] pub(crate) struct JsonError { diff --git a/src/helper.rs b/src/helper.rs index cd3e3e2..e5465ac 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,20 +1,15 @@ -#![allow(dead_code)] - //! Helper functions allowing you to avoid writing boilerplate code for common operations, such as //! parsing JSON or reading files. // Copyright (c) 2016 Google Inc (lewinb@google.com). // // Refer to the project root for licensing information. - -use serde_json; +use crate::service_account::ServiceAccountKey; +use crate::types::{ApplicationSecret, ConsoleApplicationSecret}; use std::io; use std::path::Path; -use crate::service_account::ServiceAccountKey; -use crate::types::{ApplicationSecret, ConsoleApplicationSecret}; - /// Read an application secret from a file. pub fn read_application_secret>(path: P) -> io::Result { parse_application_secret(std::fs::read_to_string(path)?) diff --git a/src/installed.rs b/src/installed.rs index 4fbdfb6..8fd64ae 100644 --- a/src/installed.rs +++ b/src/installed.rs @@ -2,6 +2,10 @@ // // Refer to the project root for licensing information. // +use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate}; +use crate::error::{Error, JsonErrorOr}; +use crate::types::{ApplicationSecret, Token}; + use std::convert::AsRef; use std::future::Future; use std::net::SocketAddr; @@ -10,16 +14,12 @@ use std::sync::{Arc, Mutex}; use futures::future::FutureExt; use futures_util::try_stream::TryStreamExt; -use hyper; use hyper::header; +use serde::Deserialize; use tokio::sync::oneshot; use url::form_urlencoded; use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET}; -use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate}; -use crate::error::{Error, JsonErrorOr}; -use crate::types::{ApplicationSecret, Token}; - const OOB_REDIRECT_URI: &str = "urn:ietf:wg:oauth:2.0:oob"; /// Assembles a URL to request an authorization token (with user interaction). diff --git a/src/lib.rs b/src/lib.rs index bb82e6c..79670c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,9 +68,6 @@ //! } //! ``` //! -#[macro_use] -extern crate serde_derive; - pub mod authenticator; pub mod authenticator_delegate; mod device; diff --git a/src/refresh.rs b/src/refresh.rs index 03847e2..0a4a225 100644 --- a/src/refresh.rs +++ b/src/refresh.rs @@ -1,11 +1,10 @@ use crate::error::{JsonErrorOr, RefreshError}; -use crate::types::ApplicationSecret; +use crate::types::{ApplicationSecret, Token}; -use super::Token; use chrono::Utc; use futures_util::try_stream::TryStreamExt; -use hyper; use hyper::header; +use serde::Deserialize; use url::form_urlencoded; /// Implements the [OAuth2 Refresh Token Flow](https://developers.google.com/youtube/v3/guides/authentication#devices). diff --git a/src/service_account.rs b/src/service_account.rs index 66ab1df..2327d1f 100644 --- a/src/service_account.rs +++ b/src/service_account.rs @@ -11,29 +11,24 @@ //! Copyright (c) 2016 Google Inc (lewinb@google.com). //! -use std::sync::Mutex; - use crate::authenticator::{DefaultHyperClient, HyperClientBuilder}; use crate::error::{Error, JsonErrorOr}; use crate::storage::{self, Storage}; use crate::types::Token; +use std::io; +use std::sync::Mutex; + use futures::prelude::*; use hyper::header; -use url::form_urlencoded; - use rustls::{ self, internal::pemfile, sign::{self, SigningKey}, PrivateKey, }; -use std::io; - -use base64; -use chrono; -use hyper; -use serde_json; +use serde::{Deserialize, Serialize}; +use url::form_urlencoded; const GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:jwt-bearer"; const GOOGLE_RS256_HEAD: &str = r#"{"alg":"RS256","typ":"JWT"}"#; diff --git a/src/storage.rs b/src/storage.rs index 17cf2db..7192f77 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -2,6 +2,7 @@ // // See project root for licensing information. // +use crate::types::Token; use std::cmp::Ordering; use std::collections::hash_map::DefaultHasher; @@ -10,7 +11,7 @@ use std::io; use std::path::{Path, PathBuf}; use std::sync::Mutex; -use crate::types::Token; +use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct ScopeHash(u64); diff --git a/src/types.rs b/src/types.rs index cb044a1..c8d5510 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,5 @@ use chrono::{DateTime, TimeZone, Utc}; +use serde::{Deserialize, Serialize}; /// Represents a token as returned by OAuth2 servers. ///