Revert mutable authenticator interface change

Instead, suggest using interior mutability (and RwLock in the example) to manage storage of token states. This makes it easier to share authenticators between threads.
This commit is contained in:
Daniel Rodgers-Pryor
2021-02-06 22:46:50 +11:00
parent 5ef498f801
commit 384963e091
5 changed files with 40 additions and 29 deletions

View File

@@ -47,7 +47,7 @@ where
C: hyper::client::connect::Connect + Clone + Send + Sync + 'static,
{
/// Return the current token for the provided scopes.
pub async fn token<'a, T>(&'a mut self, scopes: &'a [T]) -> Result<AccessToken, Error>
pub async fn token<'a, T>(&'a self, scopes: &'a [T]) -> Result<AccessToken, Error>
where
T: AsRef<str>,
{
@@ -57,7 +57,7 @@ where
/// Return a token for the provided scopes, but don't reuse cached tokens. Instead,
/// always fetch a new token from the OAuth server.
pub async fn force_refreshed_token<'a, T>(
&'a mut self,
&'a self,
scopes: &'a [T],
) -> Result<AccessToken, Error>
where
@@ -68,7 +68,7 @@ where
/// Return a cached token or fetch a new one from the server.
async fn find_token<'a, T>(
&'a mut self,
&'a self,
scopes: &'a [T],
force_refresh: bool,
) -> Result<AccessToken, Error>

View File

@@ -137,7 +137,7 @@ pub trait TokenStorage: Send + Sync {
/// Store a token for the given set of scopes so that it can be retrieved later by get()
/// ScopeSet implements Hash so that you can easily serialize and store it.
/// TokenInfo can be serialized with serde.
async fn set(&mut self, scopes: ScopeSet<'_, &str>, token: TokenInfo) -> anyhow::Result<()>;
async fn set(&self, scopes: ScopeSet<'_, &str>, token: TokenInfo) -> anyhow::Result<()>;
/// Retrieve a token stored by set for the given set of scopes
async fn get(&self, scopes: ScopeSet<'_, &str>) -> Option<TokenInfo>;
@@ -151,7 +151,7 @@ pub(crate) enum Storage {
impl Storage {
pub(crate) async fn set<T>(
&mut self,
&self,
scopes: ScopeSet<'_, T>,
token: TokenInfo,
) -> anyhow::Result<()>