From b70d07aac2c088f9259a860802a89dea1b78d6c3 Mon Sep 17 00:00:00 2001 From: Glenn Griffin Date: Fri, 15 Nov 2019 09:48:18 -0800 Subject: [PATCH] storage set method should just accept a Token rather than Option. No caller ever provided a None value. Presumably a None value should delete the token, but it didn't do that and that would be more clearly done with a remove or delete method. --- src/authenticator.rs | 4 ++-- src/service_account.rs | 2 +- src/storage.rs | 33 ++++++++++++++------------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/authenticator.rs b/src/authenticator.rs index 75f6888..67b2295 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -59,7 +59,7 @@ where } Ok(token) => token, }; - self.storage.set(hashed_scopes, Some(token.clone())).await; + self.storage.set(hashed_scopes, token.clone()).await; Ok(token) } None @@ -72,7 +72,7 @@ where .auth_flow .token(&self.hyper_client, &self.app_secret, scopes) .await?; - self.storage.set(hashed_scopes, Some(t.clone())).await; + self.storage.set(hashed_scopes, t.clone()).await; Ok(t) } } diff --git a/src/service_account.rs b/src/service_account.rs index c81b6cc..c672357 100644 --- a/src/service_account.rs +++ b/src/service_account.rs @@ -277,7 +277,7 @@ where scopes, ) .await?; - cache.set(hashed_scopes, Some(token.clone())).await; + cache.set(hashed_scopes, token.clone()).await; Ok(token) } /// Send a request for a new Bearer token to the OAuth provider. diff --git a/src/storage.rs b/src/storage.rs index 0dd8720..5481700 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -69,7 +69,7 @@ pub(crate) enum Storage { } impl Storage { - pub(crate) async fn set(&self, scopes: HashedScopes<'_, T>, token: Option) + pub(crate) async fn set(&self, scopes: HashedScopes<'_, T>, token: Token) where T: AsRef, { @@ -156,29 +156,24 @@ impl JSONTokens { None } - fn set(&mut self, scopes: HashedScopes, token: Option) + fn set(&mut self, scopes: HashedScopes, token: Token) where T: AsRef, { eprintln!("setting: {:?}, {:?}", scopes.hash, token); self.tokens.retain(|x| x.hash != scopes.hash); - match token { - None => (), - Some(t) => { - self.tokens.push(JSONToken { - hash: scopes.hash, - scopes: Some( - scopes - .scopes - .iter() - .map(|x| x.as_ref().to_string()) - .collect(), - ), - token: t, - }); - } - } + self.tokens.push(JSONToken { + hash: scopes.hash, + scopes: Some( + scopes + .scopes + .iter() + .map(|x| x.as_ref().to_string()) + .collect(), + ), + token, + }); } // TODO: ideally this function would accept &Path, but tokio requires the @@ -219,7 +214,7 @@ impl DiskStorage { }) } - async fn set(&self, scopes: HashedScopes<'_, T>, token: Option) + async fn set(&self, scopes: HashedScopes<'_, T>, token: Token) where T: AsRef, {