Require trait implementations to be Send + Sync.

Tidy up some of the trait bounds on types and methods.
This commit is contained in:
Glenn Griffin
2019-11-07 15:21:38 -08:00
parent 93cbd91341
commit a4c9b6034e
6 changed files with 45 additions and 54 deletions

View File

@@ -167,9 +167,9 @@ where
/// Create the authenticator.
pub fn build(self) -> io::Result<impl GetToken>
where
T::TokenGetter: 'static + GetToken + Send,
T::TokenGetter: 'static + GetToken,
S: 'static + Send,
AD: 'static + Send + Sync,
AD: 'static,
C::Connector: 'static + Clone + Send,
{
let client = self.client.build_hyper_client();
@@ -189,12 +189,12 @@ impl<GT, S, AD, C> AuthenticatorImpl<GT, S, AD, C>
where
GT: 'static + GetToken,
S: 'static + TokenStorage,
AD: 'static + AuthenticatorDelegate + Send + Sync,
AD: 'static + AuthenticatorDelegate,
C: 'static + hyper::client::connect::Connect + Clone + Send,
{
async fn get_token(&self, scope_key: u64, scopes: Vec<String>) -> Result<Token, RequestError> {
let store = self.store.clone();
let mut delegate = self.delegate.clone();
let delegate = &self.delegate;
let client = self.client.clone();
let appsecret = self.inner.application_secret();
let gettoken = self.inner.clone();
@@ -209,7 +209,6 @@ where
}
// Implement refresh flow.
let refresh_token = t.refresh_token.clone();
let mut delegate = delegate.clone();
let store = store.clone();
let scopes = scopes.clone();
let rr = RefreshFlow::refresh_token(
@@ -254,7 +253,6 @@ where
Ok(None) => {
let store = store.clone();
let scopes = scopes.clone();
let mut delegate = delegate.clone();
let t = gettoken.token(scopes.clone()).await?;
if let Err(e) = store.set(
scope_key,
@@ -282,7 +280,7 @@ where
impl<
GT: 'static + GetToken,
S: 'static + TokenStorage,
AD: 'static + AuthenticatorDelegate + Send + Sync,
AD: 'static + AuthenticatorDelegate,
C: 'static + hyper::client::connect::Connect + Clone + Send,
> GetToken for AuthenticatorImpl<GT, S, AD, C>
{

View File

@@ -71,11 +71,11 @@ impl Error for PollError {
///
/// The only method that needs to be implemented manually is `present_user_code(...)`,
/// as no assumptions are made on how this presentation should happen.
pub trait AuthenticatorDelegate: Clone {
pub trait AuthenticatorDelegate: Clone + Send + Sync {
/// Called whenever there is an client, usually if there are network problems.
///
/// Return retry information.
fn client_error(&mut self, _: &hyper::Error) -> Retry {
fn client_error(&self, _: &hyper::Error) -> Retry {
Retry::Abort
}
@@ -84,19 +84,19 @@ pub trait AuthenticatorDelegate: Clone {
/// This can be useful if the underlying `TokenStorage` may fail occasionally.
/// if `is_set` is true, the failure resulted from `TokenStorage.set(...)`. Otherwise,
/// it was `TokenStorage.get(...)`
fn token_storage_failure(&mut self, is_set: bool, _: &(dyn Error + Send + Sync)) -> Retry {
fn token_storage_failure(&self, is_set: bool, _: &(dyn Error + Send + Sync)) -> Retry {
let _ = is_set;
Retry::Abort
}
/// The server denied the attempt to obtain a request code
fn request_failure(&mut self, _: RequestError) {}
fn request_failure(&self, _: RequestError) {}
/// 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<S: AsRef<str>>(
&mut self,
&self,
error: S,
error_description: &Option<String>,
) {
@@ -111,7 +111,7 @@ pub trait AuthenticatorDelegate: Clone {
/// FlowDelegate methods are called when an OAuth flow needs to ask the application what to do in
/// certain cases.
pub trait FlowDelegate: Clone {
pub trait FlowDelegate: Clone + Send + Sync {
/// Called if the request code is expired. You will have to start over in this case.
/// This will be the last call the delegate receives.
/// Given `DateTime` is the expiration date

View File

@@ -73,7 +73,7 @@ impl<FD> DeviceFlow<FD> {
impl<FD, C> crate::authenticator::AuthFlow<C> for DeviceFlow<FD>
where
FD: FlowDelegate + Send + Sync + 'static,
FD: FlowDelegate + 'static,
C: hyper::client::connect::Connect + 'static,
{
type TokenGetter = DeviceFlowImpl<FD, C>;
@@ -105,10 +105,10 @@ impl<FD, C> Flow for DeviceFlowImpl<FD, C> {
}
}
impl<
FD: FlowDelegate + Clone + Send + Sync + 'static,
C: hyper::client::connect::Connect + Sync + 'static,
> GetToken for DeviceFlowImpl<FD, C>
impl<FD, C> GetToken for DeviceFlowImpl<FD, C>
where
FD: FlowDelegate + 'static,
C: hyper::client::connect::Connect + 'static,
{
fn token<'a, I, T>(
&'a self,
@@ -130,10 +130,10 @@ impl<
impl<FD, C> DeviceFlowImpl<FD, C>
where
C: hyper::client::connect::Connect + Sync + 'static,
C: hyper::client::connect::Connect + 'static,
C::Transport: 'static,
C::Future: 'static,
FD: FlowDelegate + Clone + Send + 'static,
FD: FlowDelegate + 'static,
{
/// Essentially what `GetToken::token` does: Retrieve a token for the given scopes without
/// caching.

View File

@@ -61,10 +61,10 @@ where
})
}
impl<
FD: FlowDelegate + 'static + Send + Sync + Clone,
C: hyper::client::connect::Connect + 'static,
> GetToken for InstalledFlowImpl<FD, C>
impl<FD, C> GetToken for InstalledFlowImpl<FD, C>
where
FD: FlowDelegate + 'static,
C: hyper::client::connect::Connect + 'static,
{
fn token<'a, I, T>(
&'a self,
@@ -85,7 +85,11 @@ impl<
}
/// The InstalledFlow implementation.
pub struct InstalledFlowImpl<FD: FlowDelegate, C: hyper::client::connect::Connect + 'static> {
pub struct InstalledFlowImpl<FD, C>
where
FD: FlowDelegate + 'static,
C: hyper::client::connect::Connect + 'static,
{
method: InstalledFlowReturnMethod,
client: hyper::client::Client<C, hyper::Body>,
fd: FD,
@@ -109,7 +113,7 @@ pub enum InstalledFlowReturnMethod {
/// InstalledFlowImpl provides tokens for services that follow the "Installed" OAuth flow. (See
/// https://www.oauth.com/oauth2-servers/authorization/,
/// https://developers.google.com/identity/protocols/OAuth2InstalledApp).
pub struct InstalledFlow<FD: FlowDelegate> {
pub struct InstalledFlow<FD: FlowDelegate + 'static> {
method: InstalledFlowReturnMethod,
flow_delegate: FD,
appsecret: ApplicationSecret,
@@ -145,7 +149,7 @@ where
impl<FD, C> crate::authenticator::AuthFlow<C> for InstalledFlow<FD>
where
FD: FlowDelegate + Send + Sync + 'static,
FD: FlowDelegate + 'static,
C: hyper::client::connect::Connect + 'static,
{
type TokenGetter = InstalledFlowImpl<FD, C>;
@@ -160,8 +164,10 @@ where
}
}
impl<'c, FD: 'static + FlowDelegate + Clone + Send, C: 'c + hyper::client::connect::Connect>
InstalledFlowImpl<FD, C>
impl<FD, C> InstalledFlowImpl<FD, C>
where
FD: FlowDelegate + 'static,
C: hyper::client::connect::Connect + 'static,
{
/// Handles the token request flow; it consists of the following steps:
/// . Obtain a authorization code with user cooperation or internal redirect.

View File

@@ -266,7 +266,10 @@ struct TokenResponse {
expires_in: Option<i64>,
}
impl<'a, C: 'static + hyper::client::connect::Connect> ServiceAccountAccessImpl<C> {
impl<C> ServiceAccountAccessImpl<C>
where
C: hyper::client::connect::Connect + 'static,
{
/// Send a request for a new Bearer token to the OAuth provider.
async fn request_token(
client: hyper::client::Client<C>,
@@ -358,9 +361,9 @@ impl<'a, C: 'static + hyper::client::connect::Connect> ServiceAccountAccessImpl<
}
}
impl<C: 'static> GetToken for ServiceAccountAccessImpl<C>
impl<C> GetToken for ServiceAccountAccessImpl<C>
where
C: hyper::client::connect::Connect,
C: hyper::client::connect::Connect + 'static,
{
fn token<'a, I, T>(
&'a self,

View File

@@ -6,7 +6,6 @@
use std::cmp::Ordering;
use std::collections::hash_map::DefaultHasher;
use std::error::Error;
use std::fmt;
use std::fs;
use std::hash::{Hash, Hasher};
use std::io;
@@ -54,27 +53,12 @@ where
#[derive(Default)]
pub struct NullStorage;
#[derive(Debug)]
pub struct NullError;
impl Error for NullError {
fn description(&self) -> &str {
"NULL"
}
}
impl fmt::Display for NullError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
"NULL-ERROR".fmt(f)
}
}
impl TokenStorage for NullStorage {
type Error = NullError;
fn set(&self, _: u64, _: &Vec<&str>, _: Option<Token>) -> Result<(), NullError> {
type Error = std::convert::Infallible;
fn set(&self, _: u64, _: &Vec<&str>, _: Option<Token>) -> Result<(), Self::Error> {
Ok(())
}
fn get(&self, _: u64, _: &Vec<&str>) -> Result<Option<Token>, NullError> {
fn get(&self, _: u64, _: &Vec<&str>) -> Result<Option<Token>, Self::Error> {
Ok(None)
}
}
@@ -92,14 +76,14 @@ impl MemoryStorage {
}
impl TokenStorage for MemoryStorage {
type Error = NullError;
type Error = std::convert::Infallible;
fn set(
&self,
scope_hash: u64,
scopes: &Vec<&str>,
token: Option<Token>,
) -> Result<(), NullError> {
) -> Result<(), Self::Error> {
let mut tokens = self.tokens.lock().expect("poisoned mutex");
let matched = tokens.iter().find_position(|x| x.hash == scope_hash);
if let Some((idx, _)) = matched {
@@ -120,7 +104,7 @@ impl TokenStorage for MemoryStorage {
Ok(())
}
fn get(&self, scope_hash: u64, scopes: &Vec<&str>) -> Result<Option<Token>, NullError> {
fn get(&self, scope_hash: u64, scopes: &Vec<&str>) -> Result<Option<Token>, Self::Error> {
let scopes: Vec<_> = scopes.iter().sorted().unique().collect();
let tokens = self.tokens.lock().expect("poisoned mutex");