appservice: Rely on cfg-toggle in send_request

master
Johannes Becker 2021-05-10 07:56:00 +02:00
parent eece920953
commit 3b24d33822
2 changed files with 77 additions and 55 deletions

View File

@ -405,6 +405,7 @@ pub struct RequestConfig {
pub(crate) retry_limit: Option<u64>,
pub(crate) retry_timeout: Option<Duration>,
pub(crate) force_auth: bool,
#[cfg(feature = "appservice")]
pub(crate) assert_identity: bool,
}
@ -427,6 +428,7 @@ impl Default for RequestConfig {
retry_limit: Default::default(),
retry_timeout: Default::default(),
force_auth: false,
#[cfg(feature = "appservice")]
assert_identity: false,
}
}

View File

@ -115,68 +115,88 @@ impl HttpClient {
None => self.request_config,
};
#[cfg(not(feature = "appservice"))]
let request = self.try_into_http_request(request, session, config).await?;
#[cfg(feature = "appservice")]
let request = if !self.request_config.assert_identity {
let read_guard;
let access_token = if config.force_auth {
read_guard = session.read().await;
if let Some(session) = read_guard.as_ref() {
SendAccessToken::Always(session.access_token.as_str())
} else {
return Err(HttpError::ForcedAuthenticationWithoutAccessToken);
}
} else {
match Request::METADATA.authentication {
AuthScheme::AccessToken => {
read_guard = session.read().await;
if let Some(session) = read_guard.as_ref() {
SendAccessToken::IfRequired(session.access_token.as_str())
} else {
return Err(HttpError::AuthenticationRequired);
}
}
AuthScheme::None => SendAccessToken::None,
_ => return Err(HttpError::NotClientRequest),
}
};
request
.try_into_http_request::<BytesMut>(&self.homeserver.to_string(), access_token)?
.map(|body| body.freeze())
self.try_into_http_request(request, session, config).await?
} else {
// this should be unreachable since assert_identity on request_config can only be set
// with the appservice feature active
#[cfg(not(feature = "appservice"))]
return Err(HttpError::NeedsAppserviceFeature);
#[cfg(feature = "appservice")]
{
let read_guard = session.read().await;
let access_token = if let Some(session) = read_guard.as_ref() {
SendAccessToken::Always(session.access_token.as_str())
} else {
return Err(HttpError::AuthenticationRequired);
};
let user_id = if let Some(session) = read_guard.as_ref() {
session.user_id.clone()
} else {
return Err(HttpError::UserIdRequired);
};
request
.try_into_http_request_with_user_id::<BytesMut>(
&self.homeserver.to_string(),
access_token,
user_id,
)?
.map(|body| body.freeze())
}
self.try_into_http_request_with_identy_assertion(request, session, config)
.await?
};
self.inner.send_request(request, config).await
}
async fn try_into_http_request<Request: OutgoingRequest>(
&self,
request: Request,
session: Arc<RwLock<Option<Session>>>,
config: RequestConfig,
) -> Result<http::Request<Bytes>, HttpError> {
let read_guard;
let access_token = if config.force_auth {
read_guard = session.read().await;
if let Some(session) = read_guard.as_ref() {
SendAccessToken::Always(session.access_token.as_str())
} else {
return Err(HttpError::ForcedAuthenticationWithoutAccessToken);
}
} else {
match Request::METADATA.authentication {
AuthScheme::AccessToken => {
read_guard = session.read().await;
if let Some(session) = read_guard.as_ref() {
SendAccessToken::IfRequired(session.access_token.as_str())
} else {
return Err(HttpError::AuthenticationRequired);
}
}
AuthScheme::None => SendAccessToken::None,
_ => return Err(HttpError::NotClientRequest),
}
};
let http_request = request
.try_into_http_request::<BytesMut>(&self.homeserver.to_string(), access_token)?
.map(|body| body.freeze());
Ok(http_request)
}
#[cfg(feature = "appservice")]
async fn try_into_http_request_with_identy_assertion<Request: OutgoingRequest>(
&self,
request: Request,
session: Arc<RwLock<Option<Session>>>,
_: RequestConfig,
) -> Result<http::Request<Bytes>, HttpError> {
let read_guard = session.read().await;
let access_token = if let Some(session) = read_guard.as_ref() {
SendAccessToken::Always(session.access_token.as_str())
} else {
return Err(HttpError::AuthenticationRequired);
};
let user_id = if let Some(session) = read_guard.as_ref() {
session.user_id.clone()
} else {
return Err(HttpError::UserIdRequired);
};
let http_request = request
.try_into_http_request_with_user_id::<BytesMut>(
&self.homeserver.to_string(),
access_token,
user_id,
)?
.map(|body| body.freeze());
Ok(http_request)
}
pub async fn upload(
&self,
request: create_content::Request<'_>,