diff --git a/src/authenticator.rs b/src/authenticator.rs index 6c21771..1beaddb 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -159,7 +159,7 @@ where client: self.client, token_getter: self.token_getter, store: self.store, - delegate: delegate, + delegate, } } @@ -217,15 +217,15 @@ where match rr { RefreshResult::Error(ref e) => { delegate.token_refresh_failed( - format!("{}", e.description().to_string()), - &Some("the request has likely timed out".to_string()), + e.description(), + Some("the request has likely timed out"), ); return Err(RequestError::Refresh(rr)); } RefreshResult::RefreshError(ref s, ref ss) => { delegate.token_refresh_failed( - format!("{} {}", s, ss.clone().map(|s| format!("({})", s)).unwrap_or("".to_string())), - &Some("the refresh token is likely invalid and your authorization has been revoked".to_string()), + &format!("{}{}", s, ss.as_ref().map(|s| format!(" ({})", s)).unwrap_or_else(String::new)), + Some("the refresh token is likely invalid and your authorization has been revoked"), ); return Err(RequestError::Refresh(rr)); } diff --git a/src/authenticator_delegate.rs b/src/authenticator_delegate.rs index 245cc54..ad3b37c 100644 --- a/src/authenticator_delegate.rs +++ b/src/authenticator_delegate.rs @@ -95,7 +95,7 @@ pub trait AuthenticatorDelegate: Clone + Send + Sync { /// Called if we could not acquire a refresh token for a reason possibly specified /// by the server. /// This call is made for the delegate's information only. - fn token_refresh_failed>(&self, error: S, error_description: &Option) { + fn token_refresh_failed(&self, error: &str, error_description: Option<&str>) { { let _ = error; } diff --git a/src/device.rs b/src/device.rs index a5bbb60..8afc1f3 100644 --- a/src/device.rs +++ b/src/device.rs @@ -12,7 +12,7 @@ use url::form_urlencoded; use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate, PollInformation, Retry}; use crate::types::{ApplicationSecret, GetToken, JsonErrorOr, PollError, RequestError, Token}; -pub const GOOGLE_DEVICE_CODE_URL: &'static str = "https://accounts.google.com/o/oauth2/device/code"; +pub const GOOGLE_DEVICE_CODE_URL: &str = "https://accounts.google.com/o/oauth2/device/code"; /// Implements the [Oauth2 Device Flow](https://developers.google.com/youtube/v3/guides/authentication#devices) /// It operates in two steps: @@ -215,7 +215,7 @@ where let resp = client .request(req) .await - .map_err(|e| RequestError::ClientError(e))?; + .map_err(RequestError::ClientError)?; // This return type is defined in https://tools.ietf.org/html/draft-ietf-oauth-device-flow-15#section-3.2 // The alias is present as Google use a non-standard name for verification_uri. // According to the standard interval is optional, however, all tested implementations provide it. @@ -294,12 +294,12 @@ where let res = client .request(request) .await - .map_err(|e| PollError::HttpError(e))?; + .map_err(PollError::HttpError)?; let body = res .into_body() .try_concat() .await - .map_err(|e| PollError::HttpError(e))?; + .map_err(PollError::HttpError)?; #[derive(Deserialize)] struct JsonError { error: String, diff --git a/src/installed.rs b/src/installed.rs index 88c90c0..8928261 100644 --- a/src/installed.rs +++ b/src/installed.rs @@ -19,7 +19,7 @@ use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET}; use crate::authenticator_delegate::{DefaultFlowDelegate, FlowDelegate}; use crate::types::{ApplicationSecret, GetToken, JsonErrorOr, RequestError, Token}; -const OOB_REDIRECT_URI: &'static str = "urn:ietf:wg:oauth:2.0:oob"; +const OOB_REDIRECT_URI: &str = "urn:ietf:wg:oauth:2.0:oob"; /// Assembles a URL to request an authorization token (with user interaction). /// Note that the redirect_uri here has to be either None or some variation of @@ -39,9 +39,9 @@ where url.push_str(auth_uri); vec![ format!("?scope={}", scopes_string), - format!("&access_type=offline"), + "&access_type=offline".to_string(), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI)), - format!("&response_type=code"), + "&response_type=code".to_string(), format!("&client_id={}", client_id), ] .into_iter() @@ -258,12 +258,12 @@ where .client .request(request) .await - .map_err(|e| RequestError::ClientError(e))?; + .map_err(RequestError::ClientError)?; let body = resp .into_body() .try_concat() .await - .map_err(|e| RequestError::ClientError(e))?; + .map_err(RequestError::ClientError)?; #[derive(Deserialize)] struct JSONTokenResponse { @@ -295,7 +295,7 @@ where } /// Sends the authorization code to the provider in order to obtain access and refresh tokens. - fn request_token<'a>( + fn request_token( appsecret: &ApplicationSecret, authcode: &str, custom_redirect_uri: Option<&str>, @@ -318,11 +318,10 @@ where ]) .finish(); - let request = hyper::Request::post(&appsecret.token_uri) + hyper::Request::post(&appsecret.token_uri) .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded") .body(hyper::Body::from(body)) - .unwrap(); // TODO: error check - request + .unwrap() // TODO: error check } } diff --git a/src/service_account.rs b/src/service_account.rs index b2c93aa..545d7f5 100644 --- a/src/service_account.rs +++ b/src/service_account.rs @@ -36,8 +36,8 @@ use chrono; use hyper; use serde_json; -const GRANT_TYPE: &'static str = "urn:ietf:params:oauth:grant-type:jwt-bearer"; -const GOOGLE_RS256_HEAD: &'static str = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; +const GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:jwt-bearer"; +const GOOGLE_RS256_HEAD: &str = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; /// Encodes s as Base64 fn encode_base64>(s: T) -> String { @@ -51,7 +51,7 @@ fn decode_rsa_key(pem_pkcs8: &str) -> Result { let private_keys = pemfile::pkcs8_private_keys(&mut private_reader); if let Ok(pk) = private_keys { - if pk.len() > 0 { + if !pk.is_empty() { Ok(pk[0].clone()) } else { Err(io::Error::new( @@ -112,7 +112,7 @@ impl JWT { fn new(claims: Claims) -> JWT { JWT { header: GOOGLE_RS256_HEAD.to_string(), - claims: claims, + claims, } } @@ -141,10 +141,9 @@ impl JWT { .map_err(|_| io::Error::new(io::ErrorKind::Other, "Couldn't initialize signer"))?; let signer = signing_key .choose_scheme(&[rustls::SignatureScheme::RSA_PKCS1_SHA256]) - .ok_or(io::Error::new( - io::ErrorKind::Other, - "Couldn't choose signing scheme", - ))?; + .ok_or_else(|| { + io::Error::new(io::ErrorKind::Other, "Couldn't choose signing scheme") + })?; let signature = signer .sign(jwt_head.as_bytes()) .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, format!("{}", e)))?; @@ -176,7 +175,7 @@ where iss: key.client_email.clone().unwrap(), aud: key.token_uri.clone().unwrap(), exp: expiry, - iat: iat, + iat, sub: None, scope: scopes_string, } diff --git a/src/storage.rs b/src/storage.rs index 8d9df5c..fef3c0a 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -103,17 +103,13 @@ impl TokenStorage for MemoryStorage { self.tokens.retain(|x| x.hash != scope_hash); } - match token { - Some(t) => { - tokens.push(JSONToken { - hash: scope_hash, - scopes: Some(scopes.into_iter().map(|x| x.as_ref().to_string()).collect()), - token: t, - }); - () - } - None => {} - }; + if let Some(t) = token { + tokens.push(JSONToken { + hash: scope_hash, + scopes: Some(scopes.iter().map(|x| x.as_ref().to_string()).collect()), + token: t, + }); + } Ok(()) } @@ -233,10 +229,9 @@ impl TokenStorage for DiskTokenStorage { Some(t) => { tokens.push(JSONToken { hash: scope_hash, - scopes: Some(scopes.into_iter().map(|x| x.as_ref().to_string()).collect()), + scopes: Some(scopes.iter().map(|x| x.as_ref().to_string()).collect()), token: t, }); - () } } } diff --git a/src/types.rs b/src/types.rs index 35ab2a4..a9d371c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -91,7 +91,7 @@ impl From for RequestError { "invalid_scope" => RequestError::InvalidScope( value .error_description - .unwrap_or("no description provided".to_string()), + .unwrap_or_else(|| "no description provided".to_string()), ), _ => RequestError::NegativeServerResponse(value.error, value.error_description), } @@ -112,7 +112,7 @@ impl fmt::Display for RequestError { RequestError::InvalidScope(ref scope) => writeln!(f, "Invalid Scope: '{}'", scope), RequestError::NegativeServerResponse(ref error, ref desc) => { error.fmt(f)?; - if let &Some(ref desc) = desc { + if let Some(ref desc) = *desc { write!(f, ": {}", desc)?; } "\n".fmt(f) @@ -162,7 +162,7 @@ impl StringError { error.push_str(d.as_ref()); } - StringError { error: error } + StringError { error } } } @@ -298,7 +298,7 @@ impl Token { /// # Panics /// * if our access_token is unset pub fn expired(&self) -> bool { - if self.access_token.len() == 0 { + if self.access_token.is_empty() { panic!("called expired() on unset token"); } if let Some(expiry_date) = self.expiry_date() {